[systemd-devel] Best approach to run python service in virtualenv with systemd

Lennart Poettering lennart at poettering.net
Sun Apr 17 13:01:13 UTC 2016


On Thu, 14.04.16 15:40, Stanislav Kopp (staskopp at gmail.com) wrote:

> yes, looks normal to me.
> 
> mercurial:/srv/kallithea# cat kallithea.pid
> 7410mercurial:/srv/kallithea#

So this suggests that your service doesn't place a trailing newline in
that file, which isn't pretty, but actually shouldn't really matter,
as systemd is fine both with and without it.

> 7410 is the right PID of process.

My only guess is that your package is writing the PID file only after
having double forked or so. That means systemd might read the file
before it is correctly written by your daemon.

The error message you see is generated when the string in that file is
not a valid PID. It's generated by this piece of code:

https://github.com/systemd/systemd/blob/master/src/core/service.c#L797

My guess is now that your service creates the PID file early, but does
not actually write the PID to it, before returning, but delays that in
some racy way. This way, the PID file will sometimes exist but be
empty when systemd tries to read it and you see the error you are
seeing.

To verify that, consider using gdb to attach to PID 1. Set a
breakpoint on the log_warning() invocation, run your service, and then
check what the "p" string contains...

Other than that, check that your service is properly writing its PID
files, i.e. before returning in the parent process. See daemon(7) for
details.

(Of course, even better would be do drop the whole PID file, and
forking madness and use Type=notify or so, and sd_notify() – or some
Python equivalent – to let systemd know when you are complete.)

Lennart

-- 
Lennart Poettering, Red Hat


More information about the systemd-devel mailing list