Creating a systemd Service in Debian
2024.01.28 | Yuki Rea
A systemd service is a great way do manage processes or scripts that you wish to run automatically when a system starts.
Creating a service is as easy as creating a .service file and enabling it.
Create a .service file
Create a .serivce file in your text editor of choice within /etc/systemd/system/
In this case, I created example.service
/etc/systemd/system/example.service
1 [Unit] 2 Description=This is just an example service 3 Wants=network-online.target 4 After=network-online.target 5 6 [Service] 7 User=username 8 Group=groupname 9 WorkingDirectory=/home/user/ 10 ExecStartPre=/usr/bin/pre-command 11 ExecStart=/usr/bin/command args 12 ExecStop=/usr/bin/command args 13 ExecReload=/usr/bin/command args /usr/bin/command 14 Restart=always 15 TimeoutSec=1800 16 17 [Install] 18 WantedBy=multi-user.target
Service File Parameters
- Description
- Wants
- After
- User
- Group
- WorkingDirectory
- ExecStartPre
- ExecStart
- ExecStop
- ExecReload
- Restart
- TimeoutSec
A short description of what the service does.
The service will be started once it reaches this target. If the target fails, the service will be started anyway.
Wait until after this target to start the service.
Specify which user the service should run as.
Specify which group the service should run as.
Specify the working directory of the service if any.
Commands or scripts to run to prepare for starting the service.
Commands or script to start the service.
Commands or script to run when the service is stopped.
Commands or script to run when the service is restarted.
Should the service restart if it fails?
How long in seconds should we wait for the service to start before aborting.
For additional parameters, please see the following article from RedHat.
Managing Services with systemd
Start a Service
Once you have created your .service file, you should start it in order to test the service before enabling it.
You can start your servic with the systemctl start example.service
Check the Status of a Service
You can check the status of a service using the following command. systemctl status example.service
systemctl status ssh.service
● ssh.service - OpenBSD Secure Shell server Loaded: loaded (/lib/systemd/system/ssh.service; enabled; preset: enabled) Active: active (running) since Sat 2024-01-27 19:35:03 CST; 17h ago Docs: man:sshd(8) man:sshd_config(5) Process: 682 ExecStartPre=/usr/sbin/sshd -t (code=exited, status=0/SUCCESS) Main PID: 701 (sshd) Tasks: 1 (limit: 9290) Memory: 1000.0K CPU: 44ms CGroup: /system.slice/ssh.service └─701 "sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 startups" Jan 27 19:35:02 T420s systemd[1]: Starting ssh.service - OpenBSD Secure Shell server... Jan 27 19:35:03 T420s sshd[701]: Server listening on 0.0.0.0 port 22. Jan 27 19:35:03 T420s sshd[701]: Server listening on :: port 22. Jan 27 19:35:03 T420s systemd[1]: Started ssh.service - OpenBSD Secure Shell server.
Enable a Service
Once you have verified that your service is working as intended, you can enable it.
Enabling a service allows it to start automaticlaly when the contitions in the .service file are met.
Enable a service using the systemctl enable example.serivce
Stop a Service
Stop a service using the systemctl stop example.serivce