Does systemd support on-failure restarts for forking daemons

exit-statussystemd

Originally posted on StackOverflow, told to come ask over here. To recap that conversation–

  1. No, I haven't really been able to try making the change to see what happens (not without potentially impacting a running production service).
  2. RTFM due diligence done. They don't cover this.

My original SO question follows:

Assume I have the following in my systemd unit file:

Type=forking
Restart=on-failure

Parent process exits with status 0 (child launched successfully). At some later point, the child dies with a non-zero status. What happens? systemd can track the child daemon process PID:

Process: 1768 ExecStart=/bin/mydaemon (code=exited, status=0/SUCCESS)
Main PID: 1770 (mydaemon)

Is Restart=on-failure only looking at the parent exit status, or also the child?

Best Answer

I created the following unit file:

[Unit]
Description=Something

[Service]
Type=forking
WorkingDirectory=/tmp
ExecStart=/tmp/script.sh
ExecStop=/tmp/script.sh
Restart=on-failure

The script.sh contains the following:

#!/bin/sh
echo "Forking"
/tmp/myscript.sh &

The myscript.sh contains the following:

#!/bin/sh
sleep 60
exit 1

Sure enough, every 60 seconds, systemd restarts the service.

As shown here, notice the different PID's and the start times, note that it restarts the parent:

linux:~ # systemctl status myService.service
● myService.service - Something
   Loaded: loaded (/etc/systemd/system/myService.service; static; vendor preset: disabled)
   Active: active (running) since Mon 2017-07-10 20:43:29 CEST; 57s ago
  Process: 4393 ExecStart=/tmp/script.sh (code=exited, status=0/SUCCESS)
 Main PID: 4396 (script.sh)
    Tasks: 2 (limit: 512)
   CGroup: /system.slice/myService.service
           ├─4396 /bin/sh /tmp/script.sh
           └─4397 sleep 60

Jul 10 20:43:29 linux.suse systemd[1]: Starting Something...
Jul 10 20:43:29 linux.suse script.sh[4393]: Forking
Jul 10 20:43:29 linux.suse systemd[1]: Started Something.
linux:~ # systemctl status myService.service
● myService.service - Something
   Loaded: loaded (/etc/systemd/system/myService.service; static; vendor preset: disabled)
   Active: active (running) since Mon 2017-07-10 20:44:29 CEST; 1s ago
  Process: 4409 ExecStop=/tmp/script.sh (code=exited, status=0/SUCCESS)
  Process: 4417 ExecStart=/tmp/script.sh (code=exited, status=0/SUCCESS)
 Main PID: 4420 (script.sh)
    Tasks: 2 (limit: 512)
   CGroup: /system.slice/myService.service
           ├─4420 /bin/sh /tmp/script.sh
           └─4421 sleep 60

Jul 10 20:44:29 linux.suse systemd[1]: Starting Something...
Jul 10 20:44:29 linux.suse script.sh[4417]: Forking
Jul 10 20:44:29 linux.suse systemd[1]: Started Something.