msgbartop
MAC OS X, Linux, Windows and other IT Tips and Tricks
msgbarbottom

10 Sep 16 Adding a new service to Linux systemd

Since the System V is slowly being phased out one most likely needs to learn how to get along with SYSTEMD which is much more powerful. For example, one useful feature is to automatically restart services that stop on their own. Such features are found for example in BluePill etc. With Systemd there is no need to use such extra watchdog. Here are some very very basic information about how to create a new service called ‘unit’ under Systemd in Linux.
Systemd has its configuration files in: /etc/systemd/
In this example I will create a Systemd configuration file for a simple service called istatd which should start the single daemon with the command: /usr/local/bin/istatd -d
In order to create a service that only root can operate, its new configuration file should be created as: /etc/systemd/system/istatd.service
touch /etc/systemd/system/istatd.service
chmod 644 /etc/systemd/system/istatd.service

Content:
[Unit]
Description=IStad iPhone monitoring service
#
[Service]
Type=forking
ExecStart=/usr/local/bin/istatd -d
ExecStop=/usr/bin/killall istatd
Restart=on-failure
RestartSec=3
#
[Install]
WantedBy=default.target

This configuration file for the unit istatd will start/stop the daemon and restart it if it stops on its own 3 sec after it was detected by the watchdog of its disappearance from the process list.
To activate the new configuration and start the service run:
systemctl daemon-reload
service istatd start

Possible commands for start/stop/restart/status and debugging are:
systemctl {start|stop|restart|status} istatd
OR
service istatd {start|stop|restart|status}

For Systemd debugging use the command:
journalctl -xn
After any changes to any of the Systemd configuration file you should run the command:
systemctl enable istatd
systemctl daemon-reload

For more information on how Systemd works and how to build its configuration files see:
http://patrakov.blogspot.de/2011/01/writing-systemd-service-files.html
and
https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/7/html/System_Administrators_Guide/sect-Managing_Services_with_systemd-Unit_Files.html

Some other useful commands:

Completely delete a service:
systemctl stop [servicename]
systemctl disable [servicename]
systemctl daemon-reload
systemctl reset-failed

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: