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
[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
#!/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
If no errors occurred, then you can now run
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!