How to Use Systemd to Keep Programs Running ?
·
204 words
·
1 minute read
Systemd manages these services in unit
files like this.
[Unit]
Description=Some Really Important Service
[Service]
Type=simple
WorkingDirectory=/root
ExecStart=/root/my_program.sh
[Install]
WantedBy=multi-user.target
The program that will start is.
#!/usr/bin/env bash
while true; do
echo 'service is working'
sleep 3
done
If you don’t want to use this line #!/usr/bin/env bash
, then you should make sure to specify what is the program is dedicated to execute the script like this.
ExecStart=/bin/bash /root/my_program.sh
If you want to execute this service, you should store it in /etc/systemd/system/
. You can copy the unit file to that directory, or symlink it.
Now you can start
or stop
or enable
or disable
the service.
command | meaning |
---|---|
systemctl start sample | start the service or run it |
systemctl stop sample | stop the service |
systemctl enable sample | make the service start automatically on the system boot |
systemctl disable sample | the serice will not start when the system boot |
To see what the services output, run this command.
journalctl -f
If we want to know the current status of the service, use this command.
systemctl status sample
If you want to list all services on your system, run this command.
systemctl list-unit-files
Want to know more information about services and their units, read more here .