ll.daemon – Forking daemon processes
This module can be used on UNIX to fork a daemon process. It is based on Jürgen Hermann’s Cookbook recipe.
An example script might look like this:
from ll import daemon
counter = daemon.Daemon(
stdin="/dev/null",
stdout="/tmp/daemon.log",
stderr="/tmp/daemon.log",
pidfile="/var/run/counter/counter.pid",
user="nobody"
)
if __name__ == "__main__":
if counter.service():
import sys, os, time
sys.stdout.write(f"Daemon started with pid {os.getpid()}\n")
sys.stdout.write(f"Daemon stdout output\n")
sys.stderr.write(f"Daemon stderr output\n")
c = 0
while True:
sys.stdout.write(f"{c}: {time.ctime(time.time())}\n")
sys.stdout.flush()
c += 1
time.sleep(1)
- class ll.daemon.Daemon[source]
Bases:
objectThe
Daemonclass provides methods for starting and stopping a daemon process as well as handling command line arguments.- __init__(stdin='/dev/null', stdout='/dev/null', stderr='/dev/null', pidfile=None, user=None, group=None)[source]
The
stdin,stdout, andstderrarguments are file names that will be opened and be used to replace the standard file descriptors insys.stdin,sys.stdout, andsys.stderr. These arguments are optional and default to"/dev/null". Note that stderr is opened unbuffered, so if it shares a file with stdout then interleaved output may not appear in the order that you expect.pidfilemust be the name of a file.start()will write the pid of the newly forked daemon to this file.stop()uses this file to kill the daemon.usercan be the name or uid of a user.start()will switch to this user for running the service. IfuserisNoneno user switching will be done.In the same way
groupcan be the name or gid of a group.start()will switch to this group.
- openstreams()[source]
Open the standard file descriptors stdin, stdout and stderr as specified in the constructor.
- handlesighup(signum, frame)[source]
Handle a
SIG_HUPsignal: Reopen standard file descriptors.
- handlesigterm(signum, frame)[source]
Handle a
SIG_TERMsignal: Remove the pid file and exit.
- switchuser(user, group)[source]
Switch the effective user and group. If
userandgroupare bothNonenothing will be done.userandgroupcan be anint(i.e. a user/group id) orstr(a user/group name).
- start()[source]
Daemonize the running script. When this method returns the process is completely decoupled from the parent environment.
- stop()[source]
Send a
SIGTERMsignal to a running daemon. The pid of the daemon will be read from the pidfile specified in the constructor.
- argparser()[source]
Return an
argparseparser for parsing the command line arguments. This can be overwritten in subclasses to add more arguments.
- parseargs(parser, args=None)[source]
Use the parser returned by
argparser()to parse the argument sequenceargs, modifyselfaccordingly and return the result of the parsersparse_args()call.
- service(args=None)[source]
Handle command line arguments and start or stop the daemon accordingly.
argsmust be a list of command line arguments (including the program name inargs[0]). IfargsisNoneor unspecifiedsys.argvis used.The return value is true when a starting option has been specified as the command line argument, i.e. if the daemon should be started.
The
argparsearguments are available afterwards asself.args.