Turn Your .NET Worker Service into an Awesome Linux Systemd Service - Here's How!

Are you tired of running your .NET worker service manually on a Linux machine? Want to add some cool features and make it more efficient? Look no further! Discover how to transform your .NET worker service into a Linux Systemd service with ease. Learn the secrets of hosting long-running background tasks, processing messages, and performing periodic maintenance like a pro. Get ready for a seamless experience with features like dependency injection, logging, configuration, and graceful shutdown. Say goodbye to manual labour and hello to automated excellence. Don’t miss out on this incredible opportunity to level up your service game!

Hosting .NET worker service as a Linux systemd service is useful if you want to run a long-running background task on a Linux machine, such as processing messages from a queue, performing periodic maintenance, or sending notifications.

A .NET worker service is a type of project template that lets you create a console application that can run as a service. It uses the Microsoft.Extensions.Hosting hosting model, which provides features like dependency injection, logging, configuration, and graceful shutdown.

I won’t bore you with the details of how to make the service, because you can find that stuff easily on the official docs. Let’s just assume you already have a working service and you want to add some cool features to it. Here are a few things you need to do to make your service awesome.

Reference Microsoft.Extensions.Hosting.Systemd

If you want to host your .NET application on a Linux system that uses systemd, you should check out the Microsoft.Extensions.Hosting.Systemd package. It makes your life easier by handling some common tasks for you, such as notifying systemd when your app is ready, requesting a clean shutdown, and logging to the journal. You can install it from NuGet and configure it in your Program.cs file. It’s a handy tool for developing cross-platform applications with .NET.

image-20230515115241451

Modify Program.cs

If you want your service to be aware of systemd, you need to modify the Program.cs file in your project. Here is an example:

IHost host = Host.CreateDefaultBuilder(args)
    .UseSystemd() // <-- this
    .ConfigureServices(services => {
        services.AddHostedService<Worker>();
    })
    .Build();

host.Run();

Publish as Linux Executable and create the service

I already have it published (again, boooring!) from Windows so it looks like this:

image-20230515115817056

I’ll just SSH copy it to a linux box.

Now in /etc/systemd/system create a file servicename.service and populate with:

[Unit]
Description=aloneguid analytics worker

[Service]
Type=notify
ExecStart=/opt/alt/alt

[Install]
WantedBy=multi-user.target

The only thing you need to change is path to your executable - /opt/alt/alt (wherever you are deploying).

If you want to run the awesome alt program on your system, you need to do a few simple steps. Don’t worry, it’s not rocket science, unless you are actually using alt for rocket science, in which case, good luck!

Next, you need to reload the services on your system. This will make sure that alt is recognized and ready to go. To do this, just type sudo systemctl daemon-reload in your terminal and hit enter.

Finally, you need to start alt and enjoy its amazing features. To start it, just type sudo systemctl start alt in your terminal and hit enter. That’s it! You’re done! Congratulations! You have successfully installed and launched alt on your system.

To check if everything is working fine, you can type sudo systemctl status alt in your terminal and hit enter. This will show you some output similar to this:

alg@mc:/etc/systemd/system$ sudo systemctl status alt
● alt.service - aloneguid analytics worker
     Loaded: loaded (/etc/systemd/system/alt.service; disabled; vendor preset: enabled)
     Active: active (running) since Mon 2023-05-15 11:44:43 BST; 15min ago
   Main PID: 2302342 (alt)
      Tasks: 17 (limit: 9295)
     Memory: 449.0M
        CPU: 6.654s
     CGroup: /system.slice/alt.service
             └─2302342 /opt/alt/alt

May 15 11:56:46 mc alt[2302342]: Alt.Service.Worker[0] no new messages, sleeping
May 15 11:57:46 mc alt[2302342]: Alt.Service.Worker[0] Worker running at: 05/15/2023 11:57:46 +01:00
May 15 11:57:46 mc alt[2302342]: Alt.Service.Worker[0] pushing 1 document(s)
May 15 11:57:46 mc alt[2302342]: Alt.Service.Worker[0] no new messages, sleeping
May 15 11:58:46 mc alt[2302342]: Alt.Service.Worker[0] Worker running at: 05/15/2023 11:58:46 +01:00
May 15 11:58:46 mc alt[2302342]: Alt.Service.Worker[0] pushing 2 document(s)
May 15 11:58:46 mc alt[2302342]: Alt.Service.Worker[0] no new messages, sleeping
May 15 11:59:46 mc alt[2302342]: Alt.Service.Worker[0] Worker running at: 05/15/2023 11:59:46 +01:00
May 15 11:59:46 mc alt[2302342]: Alt.Service.Worker[0] pushing 4 document(s)
May 15 11:59:46 mc alt[2302342]: Alt.Service.Worker[0] no new messages, sleeping

Final Words

By harnessing the capabilities of Linux systemd, you can elevate your .NET worker service to new heights. No longer will you need to manually manage your background tasks or worry about service interruptions. With the power of systemd, you can ensure the smooth execution of your long-running processes, while enjoying the benefits of dependency injection, logging, and graceful shutdown.

Embrace this opportunity to streamline your development workflow and optimize your application’s performance. Whether you’re processing messages, performing maintenance, or delivering notifications, hosting your .NET worker service as a Linux systemd service will empower you to achieve greater efficiency and reliability.

So, why settle for ordinary when you can make your service extraordinary? Follow the steps outlined in this guide, unleash the full potential of your .NET worker service, and take your background task handling to the next level. Embrace the power of Linux systemd and watch your service soar to new heights of excellence. It’s time to revolutionize your approach to background tasks and experience a whole new level of efficiency and automation.


To contact me, send an email anytime or leave a comment below.