[Thiago Cafe] Programming is fun!

Creating a daemon in System D

Created by Thiago Guedes on 2024-02-16 22:14:00

Tags: #linux  

So, you created your awesome server-side application and you are ready to start using. However, you want it to start automatically with your server and restart if it crashes. Also, you're happy to have a system that uses System D

So, how do we do that?

(Spoiler alert: It's super easy)

Let's say your service name is Super Awesome Server

Step 1 - Create a user/group for your service

Create a user and group, optionally, or select an existing one.

Let's say, I am going to use the user and group aweserver, short for awesome server.

Step 2 - Create a file service configuration

File name: /etc/systemd/system/superawesome.service

And add this as a content of your file:

[Unit]
Description=Super Awesome Server

[Service]
ExecStart=/opt/myawesomeserver/bin/run_server
User=aweserver
Group=aweserver
Restart=always

[Install]
WantedBy=multi-user.target

Note:

Step 3 - Starting the service

As you created a new service, reload the daemon with:

systemctl daemon-reload

You're done!

Some other interesting commands

# Restart the service
systemctl restart superawesome.service

# Stop the service
systemctl stop superawesome.service

# Start the service
systemctl start superawesome.service

# Check service status (if it crashed or failed to start, you'll see the reason here)
systemctl status superawesome.service

Tell me your opinion!

Reach me on Twitter - @thiedri