Debian and systemd
Debian 8 is here and systemd is here to stay. I'll be using this article to make some notes and references to get started.
Checking if you have systemd:
dpkg -l | grep systemd
The systemd system and service manager:
# systemctl start [name.service]
# systemctl stop [name.service]
# systemctl restart [name.service]
# systemctl reload [name.service]
$ systemctl status [name.service]
# systemctl is-active [name.service]
$ systemctl list-units --type service --all
$ systemctl list-unit-files (show all service files which are installed)
# systemctl enable [name.service] (make service launch on boot)
# systemctl disable [name.service] (make service not launch on boot)
General first time service procedure:
- Copy unit/service file into /etc/systemd/system/
- Run systemctl enable [name.service] to create the target symlinks (e.g. make it start at boot).
- Run systemctl start [name.service] to start it manually after enabling the first time.
Checking boot times for services:
$ systemd-analyze blame
8.366s mysql.service
7.975s winbind.service
7.937s nmbd.service
7.731s samba-ad-dc.service
5.262s apache2.service
2.940s ModemManager.service
1.880s DJFirewall.service
Query the systemd journal (use with pipe for specific service):
# view logs (each line is short)
journalctl
# view logs with normal line lengths (lines are not contracted), get used to always running -a (For all), a for "showing All of the line".
journalctl -a
# show the 100 latest lines of logs
journalctl -an100
# following the log (synonymous with "tail -f /var/log/syslog"), f for "Following"
journalctl -fa
Creating the simplest possible systemd service (/etc/systemd/system/myservice):
[Unit]
Description=My Simple Service
[Service]
ExecStart=/usr/bin/myservice
[Install]
WantedBy=multi-user.target
Make it a real daemon / fork it:
[Unit]
Description=My Simple Service
[Service]
Type=forking
# The PID file is optional, but recommended in the manpage
# "so that systemd can identify the main process of the daemon"
PIDFile=/var/run/myservice.pid
ExecStart=/usr/bin/myservice --daemon --pidfile /var/run/myservice.pid
[Install]
WantedBy=multi-user.target
^ The difference is in the way the dependencies are handled. If some other services depend on myservice.service,
then, in the first example, systemd will be able to run them as soon as it starts myservice.
In the second example, systemd will wait until myservice forks. The difference matters,
because myservice creates its control socket after starting, but before forking.
So, in the first example, there is some chance that systemd will start something that
tries to connect to the socket before myservice creates it.Automatic restart on failure:
[Unit]
Description=My Simple Service
After=syslog.target
[Service]
Type=forking
PIDFile=/var/run/myservice.pid
# Note the -f: don't fail if there is no PID file
ExecStartPre=/bin/rm -f /var/run/myservice.pid
ExecStart=/usr/bin/myservice \
--daemon --pidfile /var/run/myservice.pid
Restart=on-failure
[Install]
WantedBy=multi-user.target
Using
EnvironmentFile
for parameters:[Unit]
Description=My Simple Service
After=syslog.target
[Service]
Type=forking
EnvironmentFile=/etc/myservice/myservice
PIDFile=/var/run/myservice.pid
# Note the -f: don't fail if there is no PID file
ExecStartPre=/bin/rm -f /var/run/myservice.pid
ExecStart=/usr/bin/myservice \
--daemon --pidfile /var/run/myservice.pid \
$MYSERVICE_OPTS
Restart=on-failure
[Install]
WantedBy=multi-user.target
/etc/myservice/myservice:
# Options to pass to My Simple Service.
MYSERVICE_OPTS="--arg1 --arg2"
Interesting reading:
The Biggest Myths by Lennart Poettering.
sect.systemd in the Debian Handbook.