How to create and run worker service C# in Linux?

[Phiên bản tiếng việt bên dưới]

English version

Running a background process on Linux is a common requirement for developers, especially when deploying worker services built with C#. This guide demonstrates how to configure and manage a systemd service unit to run your C# worker service reliably on Linux.

Step 1: Create a service unit file

Service unit files define the properties and behavior of a background process. Use the .service extension for these files. Here, we’ll create a file named mytool.service:

1
sudo vi /etc/systemd/system/mytool.service

Step 2: Grant execution permission to service

By default, the service does not have execution permissions, so we need to set the appropriate permissions.

1
sudo chmod +x /path/to/my-tool-folder/file-executable

Step 3: Configure service unit file

Add the following configuration to the file mytool.service:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[Unit]
Description=My Tool Process
After=network.target

[Service]
WorkingDirectory=/path/to/my-tool-folder
ExecStart=/path/to/my-tool-folder/file-executable
Type=simple
Restart=always
# Restart service after 30 seconds if the service crashes:
RestartSec=30
Environment=DOTNET_ENVIRONMENT=Production

[Install]
WantedBy=default.target

Save and exit the editor.

Step 4: Reload and start service

Reload systemd to apply the changes:

1
sudo systemctl daemon-reload

Step 5: Start the service

1
sudo systemctl start mytool.service

You can check its status, stop it using the following commands:

1
2
sudo systemctl status mytool.service
sudo systemctl stop mytool.service

Step 6: Enable service allow it to start on boot

1
sudo systemctl enable mytool.service

Step 7: View and clear Logs

To view logs for your service:

1
sudo journalctl -u mytool.service

To clear the logs:

1
sudo rm -rf /run/log/journal/*

By following these steps, you’ve successfully created a systemd service unit for your application.

Vietnamese version

Giả sử sau khi các bạn đã viết ra một worker service, và worker này không chỉ chạy trên Window, các bạn còn muốn nó chạy được trên Linux (cho xứng với cái danh .Net Core của nó chứ hả), thì các bạn có thể execute nó trực tiếp trên server, nhưng vấn đề là khi execute được rồi, nó không chạy ngầm, mà nó sẽ dừng khi bạn bấm Ctrl + C, vậy làm thế nào để nó chạy ngầm dưới background đây ta?

Bài viết giả sử bạn đã biết publish worker app sang môi trường Linux trên Visual Studio rồi nhá!

Bước 1: Tạo File Service Unit

Trong Linux để chạy một worker service được build từ .Net (C#) hoặc đơn giản là chạy một tác vụ ngầm dưới background thì chúng ta có thể sử dụng systemd. Với systemd nó cho phép chạy một tác vụ ngầm như một service, và có thể tự khởi động lại khi chúng ta reboot server.

Giả sử trong bài viết sẽ đặt tên service là mytool nhé, tên gì tùy các bạn, nhưng nên có .service đằng sau.

1
sudo vi /etc/systemd/system/mytool.service

Bước 2: Cấu hình file service

Thêm cấu hình sau vào file mytool.service:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[Unit]
Description=My Tool Process
After=network.target

[Service]
WorkingDirectory=/path/to/my-tool-folder
ExecStart=/path/to/my-tool-folder/file-executable
Type=simple
Restart=always
# Khởi động lại sau 30 giây nếu dịch vụ bị lỗi:
RestartSec=30
Environment=DOTNET_ENVIRONMENT=Production

[Install]
WantedBy=default.target

Lưu lại và thoát khỏi vi

Bước 3: Cấp quyền chạy cho service

Nếu để mặc định thì service sẽ không có quyền execute, nên ta cần set quyền

1
sudo chmod +x /path/to/my-tool-folder/file-executable

Bước 4: Reload và start service

Reload systemd để áp dụng thay đổi:

1
sudo systemctl daemon-reload

Bước 5: Start service

1
sudo systemctl start mytool.service

Bạn có thể kiểm tra trạng thái, dừng bằng 2 lệnh tương ứng sau:

1
2
sudo systemctl status mytool.service
sudo systemctl stop mytool.service

Bước 6: Setup service khởi động cùng hệ thống

1
sudo systemctl enable mytool.service

Bước 7: Xem và xóa Log

Để xem log của service tạo ra:

1
sudo journalctl -u mytool.service

Để xóa log:

1
sudo rm -rf /run/log/journal/*

Sau khi làm theo các bước trên, bạn đã tạo thành công một worker service chạy ngầm rồi đấy;.


 Comments
Comment plugin failed to load
Loading comment plugin