I have ported my LSB-controlled daemon to systemd using a wrapper script that starts and stops the daemon. My systemd (multi-instance) service file uses Type=forking, ExecStart=..., ExecStop=..., and PIDFile=....

When starting, everything looks OK in systemd, but stopping always seems to fail. As in the LSB environment this worked fine, I suspected systemd (228) to do something bad.

Actually I found out that after a successful start the PIDfile is gone, so my wrapper script returns with exit code 1 (as the PID file could not be found where expected).

So does systemd really remove the PID file? The side effect is that systemd seems to kill my daemon hard after the wrapper-script returned an error.


Yes, systemd removes pidfiles when the service unit enters the 'dead' state. However, that happens after the service has been stopped (ExecStop finished and processes killed).

I would recommend not using wrapper scripts for ExecStop, because... well, that's what systemd does. Being a service manager, it knows what PID is your daemon (even if you don't use a pidfile) and it can natively send signals to stop the service.

| improve this answer | |

It seems systemd does not remove the PID file; instead my daemon does not create it due to an incompatibility with "good old init": After fork()ing my daemon's child process waits for the parent to exit like this:

while (getppid() != 1)

Next the child would write the PID file.

As an strace showed, getppid() constantly returned 31940 which turned out to be the PID of systemd (systemd has another PID of 1, too). So systemd does some magic incompatible reparenting.

Maybe the problem is actually related to the fact that my service is running as a "user" service (and not as a "system" service) (I' still unsure about the differences). This question may be related: How can I run systemd as a process with pid 1 and with pid not 1?

Also useful is this: What is the difference between systemd's user and system services?

Actually the problem did go away after I had changed the service from a user service to a system service.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.