📜 ⬆️ ⬇️

Supervisord and forever are no longer needed. Systemd

This article will talk about systemd, which is included in Debian 8 jessie. I am writing about Debian because I use it. I’m writing about systemd not because its a fan, but some things really make me happy.

So why no longer need supervisord and forever?

The task of supervisord and its analogue written in nodejs is forever: to demonize the non-demonized and raise when it falls. These tasks are now performed by the regular systemd. What is so good about it? Well, at a minimum, no additional tools are needed (which, by the way, are not only the two mentioned) and there is a regular systemic tool for solving such problems.

For example, it is very easy to configure sshd in such a way that it will rise in case of a crash or kill. We need to find our sshd .service file. To do this, run:
root@lynx:~# systemctl status ssh ● ssh.service - OpenBSD Secure Shell server Loaded: loaded (/lib/systemd/system/ssh.service; enabled) Active: active (running) since Fri 2015-10-09 20:09:29 UTC; 55s ago Main PID: 26884 (sshd) CGroup: /system.slice/ssh.service └─26884 /usr/sbin/sshd -D Oct 09 20:09:29 lynx sshd[26884]: Server listening on 0.0.0.0 port 22. Oct 09 20:09:29 lynx sshd[26884]: Server listening on :: port 22. 

Actually /lib/systemd/system/ssh.service is the file we need. Here is its default content:
 [Unit] Description=OpenBSD Secure Shell server After=network.target auditd.service ConditionPathExists=!/etc/ssh/sshd_not_to_be_run [Service] EnvironmentFile=-/etc/default/ssh ExecStart=/usr/sbin/sshd -D $SSHD_OPTS ExecReload=/bin/kill -HUP $MAINPID KillMode=process Restart=on-failure [Install] WantedBy=multi-user.target Alias=sshd.service 

In the Service section there is a special option “Restart”. It just indicates in which cases restart the service automatically. We can write there:
 Restart=always 

And if you crash or try not to fly, sshd will be restarted. To make changes in the file take effect, you need to perform:
 root@lynx:~# systemctl daemon-reload root@lynx:~# systemctl restart ssh 

Now check that the service works:
 root@lynx:~# systemctl status ssh ● ssh.service - OpenBSD Secure Shell server Loaded: loaded (/lib/systemd/system/ssh.service; enabled) Active: active (running) since Fri 2015-10-09 20:09:29 UTC; 3min 58s ago Main PID: 26884 (sshd) CGroup: /system.slice/ssh.service └─26884 /usr/sbin/sshd -D Oct 09 20:09:29 lynx sshd[26884]: Server listening on 0.0.0.0 port 22. Oct 09 20:09:29 lynx sshd[26884]: Server listening on :: port 22. 

Let's kill sshd and see how it starts up again:
 root@lynx:~# killall sshd root@lynx:~# systemctl status ssh ● ssh.service - OpenBSD Secure Shell server Loaded: loaded (/lib/systemd/system/ssh.service; enabled) Active: active (running) since Fri 2015-10-09 20:13:47 UTC; 1s ago Main PID: 14123 (sshd) CGroup: /system.slice/ssh.service └─14123 /usr/sbin/sshd -D Oct 09 20:13:47 lynx sshd[14123]: Server listening on 0.0.0.0 port 22. Oct 09 20:13:47 lynx sshd[14123]: Server listening on :: port 22. 

')
You see, the PID is different - it says that sshd really died, and systemd started it again.

Congratulations! Now your sshd will be impossible to kill with kill-ohm and your server will be accessible via ssh even if sshd is killed.

Source: https://habr.com/ru/post/268583/


All Articles