Single-instance daemons

Jeff McNeil jeff at jmcneil.net
Wed Nov 12 17:07:20 EST 2008


On Nov 12, 4:57 pm, Jeffrey Barish <jeff_bar... at earthlink.net> wrote:
> As per Stevens/Rago, "file and record locking provides a convenient
> mutual-exclusion mechanism".  They note the convention of putting the lock
> file in /var/run in a file called <name>.pid, where <name> is the name of
> the daemon and content is the pid.  Seems like a good suggestion as I see
> pid files from many other daemons there.  However, /var/run is owned by
> root, so it is not possible to write in it without root permission.  I
> could put the pid file in /tmp, but doing so would make it harder to find.
> I could write a C program to write the lock file that takes command-line
> arguments for passing the name of the daemon and the pid and give the
> executable suid root, but that's a lot of bother.  Has anyone else dealt
> with this problem?
> --
> Jeffrey Barish

Sure, start the daemon as root, write the appropriate files, and then
drop permissions using os.setegid and then os.seteuid. You can chown
the file before priv. drop to your target user so that it can be
removed when your exit handlers run.  Alternatively, you can reclaim
root at cleanup as it's stored as your saved UID.

I've stopped dealing with most of the daemon specifics (session,
process group, forking & dropping TTY, PID file locations, etc...).
Each time I need to write a daemon process these days, I usually just
fall back to writing a twistd plugin. That takes care of everything
for me.




More information about the Python-list mailing list