Systemd Example
Let's create our own systemd service.
Our service program
- We'll use a simple bash script that endlessly outputs text into a log file:
1for (( ; ; ))
2do
3 echo "still running:" $(date) >> /tmp/myprog.log
4 sleep 1
5done
- Save this in a file, make it executable by anyone (777) and copy to /usr/bin:
Service Definition
- Create a file called myprog.service
- Enter this text into it:
1[Unit]
2Description=myprog demo service
3
4[Service]
5User=root
6ExecStart=/bin/bash /usr/bin/myprog
7
8[Install]
9WantedBy=multi-user.target
- Copy this file to /etc/systemd/system
Activating the Service
- First, let's check if systemd knows about our service:
1sudo systemctl status myprog.service
- Let's start our service:
1systemctl start myprog.service
(requires using your password)
- Verify that the service is running now:
1sudo systemctl status myprog.service
- Validate that the log file is being written into:
1tail -f /tmp/myprog.log
- Now, let's enable it, so that it runs after the next reboot:
1systemctl enable myprog.service
(go ahead a list those symbolic links created)
- Clean everything:
- disable the service (symlinks will be removed):
1systemctl disable myprog.service
- remove myprog.service from /etc/systemd
- remove myprog shell script from /usr/bin:
1sudo rm /usr/bin/myprog
- Notify systemd about the change:
1systemctl daemon-reload