[systemd-devel] option to wait for pid file to appear

Michael Chapman mike at very.puzzling.org
Thu May 17 11:03:28 UTC 2018


On Thu, 17 May 2018, Igor Bukanov wrote:
> On 17 May 2018 at 12:07, Michael Chapman <mike at very.puzzling.org> wrote:
> > It _is_ better for the PID file to be written out before the initial
> > process exits, but systemd will handle things correctly even if they
> > happen the other way around. Essentially the service won't be considered
> > to have completed activation until both events occur. If one or the other
> > takes too long (i.e. longer than the TimeoutStartSec= setting), the unit
> > will fail.
> 
> Does it means that even ExecStartPost and especially ExecReload can
> always safely use $MAINPID? I.e. does systemd call these Execs only
> after it managed to read the the pid file even when the file is
> written after the initial ExecStart process exited?

So it looks like systemd will add the MAINPID environment variable any 
time it knows what the main PID is. It learns this by reading the PID 
file, or if PIDFile= is omitted by guessing. It forgets it when it sees 
the SIGCHLD from that PID and no new PID file is available.

So by the time ExecStartPost runs, the main PID may or may not be known. 
Indeed, it's permitted for the PID file to be written out by ExecStartPost 
itself.

For ExecReload, though, there definitely should be a known main PID. I 
can't see any way you can get systemd to reload a service whose main PID 
hasn't been determined (or guessed). ExecReload may cause systemd to 
forget the main PID, if it makes the process exit, but it will be run with 
$MAINPID set.

> And yes, Type=pid-file will be very useful. I have exactly a shell
> script that will benefit from it nicely.

I'll dig it out and see what I can do. It's probably bitrotted quite a bit 
since I wrote it -- I never completed it because my own need for it went 
away.


More information about the systemd-devel mailing list