Systemd drop-in fails to create PID file

Solution 1:

The PIDFile= setting does not create a PID file. That is still up to the service itself to do, the same as it has been for the last 40 years. Rather, this option tells systemd where to find an existing PID file (if any). In most cases it's not required at all, as systemd will keep services in their own cgroups and does not need a PID file to keep track of them. However, systemd will delete a PID file when the service exits, if the service fails to clean up after itself.

From the documentation:

Takes an absolute file name pointing to the PID file of this daemon. Use of this option is recommended for services where Type= is set to forking. systemd will read the PID of the main process of the daemon after start-up of the service. systemd will not write to the file configured here, although it will remove the file after the service has shut down if it still exists.

Solution 2:

Regrettably, systemd won't create a PID file for a non-forking service even if you specify a PIDFile= line in the service's unit file. But you may be able to cheat with an ExecStartPost= line, such as:

ExecStartPost=/bin/sh -c 'umask 022; pgrep YOURSERVICE > /var/run/YOURSERVICE.pid'

Tags:

Pid

Systemd

Files