SystemDο
Service Filesο
SystemD service files have 3 sections:
Unit: The information section - description and which services need to start before this one
Service: Contains commands for env variables, execution and command to gracefully shutdown service
Install: Which target requires this service (graphical target or multi-user target), can also define service alias
Note
You can use the man systemd.service and man systemd.unit commands to
view some more info
Unit Sectionο
Description: This is the description that gets printed when running the service, so make it sound cool!
After: You can specify the service runs after another service e.g.
After=network.target auditd.service
Documentation: Provides a link for user to view documentation on the service e.g. a https link
Before: Specify to run before another service
OnFailure: A space-seperated list of one or more units that are actived if this unit enters the βfailedβ state
ConditionPathExists: Only starts if file in given path exists (use the ! to do the opposite)
Service Sectionο
Type: simple,exec, forking, oneshot, dbus, notify, idle
ExecStart: Commands (with their arguments) that are executed when the service is started e.g.
ExecStart=/usr/bin/hellocmake --options
ExecStartPre/ExecStartPost: Commands to execute before/after ExecStart. Could be same program just with different arguments passed in
WatchdogSec: Configure a watchdog for the service, which fails if the service doesnβt call sd_notify(3) more frequently that the specified number of seconds
StandardOutput: Set where the standard output is sent. Set this to tty if you want the ouput on your terminal.
User: Create a user for this service, means that service is not run with root privileges
Environment: Specify environment variables for the service
EnvironmentFile: Specify location of file containing env variables
Restart: Lists situations where restart is triggered e.g. Restart=on-failure will restart if executable returns a non 0 value
Install Sectionο
WantedBy: Specifies the target that will want this service - multi-user / graphical
Alias: another name for starting/stopping a service
SystemD Managementο
systemctl enable name.service
systemctl disable name.service
systemctl status name.service
systemctl is-enabled name.service
systemctl list-unit-files --type service
Systemd services are classified by targets, formally known as runlevels in sysvinit. You can change these targets with systemctl. Examples of targets are multi-user and graphical. To view targets available, use:
systemctl list-units --type target
systemctl list-units --type target --all
If you want to edit a service file, and see the changes instantly, use:
systemctl daemon-reload
Example adding a custom serviceο
Here we give an example of how you would setup a service that runs a WSGI server
# Location: /etc/systemd/system/start-wsgi-server.service
[Unit]
Description = Start WSGI server
After = network.target
[Service]
User = user
RemainAfterExit = yes
WorkingDirectory = /home/user/
ExecStart = /home/user/start-wsgi-server
[Install]
WantedBy = multi-user.target
#!/bin/bash
# Location: $HOME/start-wsgi-server
set -euox pipefail
cd "$HOME/server"
source .venv/bin/activate
gunicorn -b 0.0.0.0:4000 flask_server:app --access-logfile flask_log.txt --daemon
You would also have to change some file permissions and enable the service:
chmod 644 /etc/systemd/system/start-wsgi-server.service
systemctl enable start-wsgi-server.service