New website! Woo! [icon name="glass-cheers"]

Your Own Systemd Service


What Is Systemd?

Systemd is a common software in Linux based operating systems known for its ability to handle the bootstrapping and initialization of services across the user space. A systemd service is a process which runs at boot to serve some purpose. A good example of this would be a discord bot or a web page.

What Can a Systemd Service Do for Me?

With a systemd you can create processes that can automatically start upon boot. There are tons of ways to configure systemd but we’ll go over a basic model where you run a script which runs a binary.

This can be useful for running applications you don’t want to manually start every time the server boots up or restarts; these processes can also automatically restart if they fail, and can run as a certain user if necessary.

How Do I Set One Up?

In order to set up a systemd service you must first create a [NAME_HERE].service file in the etc/systemd/system directory and configure it roughly as follows:

[Unit]
Description=MyFirstService
# The name of the service

[Service]
User=username
# /!\ NOT REQUIRED
# Instructs systemd to run this process as a specified user

Restart=always
# Tells systemd to restart the service when it fails

ExecStart=/your/path/to/your/script.sh
# The script that runs when the service starts
# This can also be a command

Environment="DOTNET_BUNDLE_EXTRACT_BASE_DIR=%h/.net"
# /!\ NOT REQUIRED
# An example for setting environment variables
# The %h is the home directory (/dir/%h/ -> /dir/~/)
# This example fixes a bug where dotnet cannot
# find the home directory to unpack embedded libraries

[Install]
WantedBy=multi-user.target
# Tells systemd to wait until the non-gui login prompt before
# starting the process

After that, create a script at the path you selected near ExecStart= and add the following content to the file:

#!/bin/bash
# Tells the system to use bash to execute this file

cd /your/path/from/above
# Changes directory to your binary

./YourBinary
# Executes your binary (may have to enable execution permission)

Once you’re done, run systemctl daemon-reload to reload configuration. Then run systemctl start MyFirstService and systemctl status MyFirstService. If all things go well your service should be running now and you should see that reflected by the status command. The text “failed to start” or similar means that something has gone wrong in the process and some troubleshooting may be necessary.

If no errors occurred, then you can now run systemctl enable MyFirstService to make your service automatically start on boot. Conversely, you can also run systemctl disable MyFirstService to revert the effect.

Conclusion

And that’s it! By using this general format, you should now be able to create your own services in most Linux distributions. If you have any problems, questions, or concerns, don’t hesitate to reach out to us! We would love to help you out. 🙂

Also, if you’re itching for another tip try this article about how to tell if a link is safe!


Leave a Reply

Your email address will not be published. Required fields are marked *