Converting classic init.d startup script into new systemd
Wednesday, July 3. 2013
I have couple of own daemons running on my Linux-box. Now that all the distros are going systemd, my scripts are becoming obsolete. Sure, the systemd can piggy-back into old init.d-scripts, but ... I'd rather have them converted to the new way.
Lennart Poettering's blog has a helpful article, which got me started on my project. Also the manual pages for systemd (systemd.service and systemd.exec) proved a very valuable reference.
My daemon is pretty much from the trivial end of daemons. It runs as nobody-user to prevent it from disallowing access to number of places in case something/somebody breaks it. It does the classic fork on start and parent process simply exits. Fortunately systemd programmers anticipated that and there is a perfect support for such startup sequence.
Here is my example. I simply placed a file named dhid.service into directory /usr/lib/systemd/system/. Then I could interface with it by systemctl-command. Example:
# systemctl status dhid.service
dhid.service - DHIS client for keeping track of changing dynamic IP addresses in DNS
Loaded: loaded (/usr/lib/systemd/system/dhid.service; disabled)
Active: active (running) since Wed 2013-07-03 15:26:03 EEST; 928ms ago
Process: 32355 ExecStart=/usr/sbin/dhid -P /var/run/dhis/dhid.pid (code=exited, status=0/SUCCESS)
Main PID: 32356 (dhid)
CGroup: name=systemd:/system/dhid.service
└─32356 /usr/sbin/dhid -P /var/run/dhis/dhid.pid
Jul 03 15:26:03 samba dhid[32356]: daemon started
My entire file is here:
[Unit]
Description=DHIS client for keeping track of changing dynamic IP addresses in DNS
After=syslog.target network.target
[Service]
Type=forking
PrivateTmp=yes
User=nobody
Group=nobody
ExecStart=/usr/sbin/dhid -P /var/run/dhis/dhid.pid
PIDFile=/var/run/dhis/dhid.pid
[Install]
WantedBy=multi-user.target
It is really that simple! To make the daemon to start on bootup, just use the systemctl enable dhid.service -command.