[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

PID files of services vs. systemd (was: Re: systemd)



On 10/11/2016 08:04 PM, Pol Hallen wrote:
> Hi all, after upgraded oldstable to stable it happens that systemd
> doesn't create PID file of these packages:
> 
> openvpn
> smartmontools

There's a general misconception here: systemd never creates pid
files for daemons.

The PIDFile= setting in a unit file is a setting that tells
systemd where to look for PID files that it reads in to find
out the main PID of a forking process.

A bit of background: systemd has a concept called "main pid" of
a process. If you use KillMode=process or something along the
lines of ExecReload=/bin/kill -HUP $MAINPID, systemd will
automatically use the main pid of the service for these
operations. Also, a non-main process exiting in a service is
not considered a problem from a systemd perspective, but the
main process exiting is. In a very simple case, the main pid of
a service is trivial: if there's only one pid in a service,
that's the main pid.

However, there are other services that start multiple processes,
and there it's not necessarily easy to determine what the main
process of that service is. In these situations, systemd has
the ability to read pid files (that were written by the service
_itself_ after startup) to determine the main pid. The sequence
would be:

 - systemd starts the program
 - the program forks
 - the fork initializes
 - the program writes the pid file
 - the original process exits
 - systemd notices that, considers the forking service
   initialized, and reads in the pid file

If a forking service doesn't write a pid file, systemd will try
to guess the main process (see the GuessMainPID= setting)
instead. If there's only a single process in the service, that's
going to work reliably, but if there are multiple processes it
might not.

Also, if you don't use Type=forking but other service types,
then PID files are irrelevant from a systemd perspective - and
the PIDFile= setting is ignored. For example, if you have a
Type=notify unit, then the process spawned by systemd will not
exit (startup completion notification is done via the sd-notify
protocol instead) during regular operations, so that's going to
be the main pid of the unit.


Now for your units: I don't have openvpn installed, so I'd have
to check (and am too lazy right now to do so), but for
smartmontools the service is of Type=simple, so nothing forks
there. (smartd is called with -n, with leaves it in the
foreground.) There's simply no need for a PID file here, as the
main pid of the process is trivially known to systemd.

Now if you want to know the main PID of any given running
service for yourself, outside of systemd, then you don't need
a pid file anymore, you can query systemd dynamically,
regardless of service type:

systemctl show -p MainPID smartd.service

for use in scripting you can also do:

PID=$(systemctl show -p MainPID smartd.service 2>/dev/null | cut -d= -f2)
  (will be empty on error, e.g. service not running)

Hope that helps.

Regards,
Christian


Reply to: