14

I have a drop-in for systemd-machined at the path /etc/systemd/system/systemd-machined.service.d/10-machined-pid-file.conf. when I run systemctl status systemd-machined I do see the lines

Drop-In: /etc/systemd/system/systemd-machined.service.d
       └─10-machined-pid-file.conf

However, I do not see a PID file in /var/run/. Which based on my drop-in:

[Serivce]
PIDFile=/var/run/machined.pid

I believe there should not be any issue creating that PID file. Is there something I am missing?

Christian Grabowski
  • 529
  • 1
  • 5
  • 17

3 Answers3

25

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.

Michael Hampton
  • 237,123
  • 42
  • 477
  • 940
  • Thanks for clarification. In my case playframework created pid file in project root folder, but stopping or killing this process/service didn't delete "pid" file. So I couldn't restart my project service because of "pid" file. Adding this "PIDFile=/path/to/pid" line to my systemd service file worked fine. It deleted pid file if service is stopped or killed – Ikrom Nov 29 '18 at 06:39
14

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'
Mike Gleason
  • 331
  • 3
  • 4
  • I think that this could put several PIDs in the PID file, but (and I might be wrong here) isn't it expected to have only the main process PID when forking happens?. See https://www.freedesktop.org/software/systemd/man/systemd.service.html#PIDFile=. – Jaime Hablutzel Oct 29 '21 at 16:32
0

i think "grep'ing" pid file is wrong approach if you want to have PID of main process jus use

systemctl show -p MainPID --value SERVICE

(where service is unit name)

example usage:

ExecStartPost=/usr/bin/bash -c '/usr/bin/systemctl show -p MainPID --value unbound > /run/unbound.pid'

*ps ensure cmd paths (distro vary)

 which systemctl 

 which bash 

ceph3us
  • 115
  • 9