Package: zdaemon
zdaemonis a Unix (Unix, Linux, Mac OS X) Python program that wraps
commands to make them behave as proper daemons.ContentsUsing zdaemonEnvironment VariablesTranscript logStart test program and timeoutReference DocumentationChange log5.0 (2023-05-24)4.4 (2022-12-02)4.3 (2018-10-30)4.2.0 (2016-12-07)4.1.0 (2015-04-16)4.0.1 (2014-12-26)4.0.0 (2013-05-10)4.0.0a1 (2013-02-15)3.0.5 (2012-11-27)3.0.4 (2012-07-30)3.0.3 (2012-07-10)3.0.2 (2012-07-10)3.0.1 (2012-06-08)3.0.0 (2012-06-08)2.0.6 (2012-06-07)2.0.5 (2012-06-07)2.0.4 (2009-04-20)2.0.3 (2009-04-11)Bugs Fixed2.0.2 (2008-04-05)2.0.1 (2007-10-31)2.0.0 (2007-07-19)2.0a6 (2007-01-11)2.0a3, 2.0a4, 2.0a5 (2007-01-10)2.0a2 (2007-01-10)2.0a1 (2006-12-21)Bugs FixedNew Features1.4a1 (2005-11-21)1.4a1 (2005-11-05)After some unknown release(???)After 1.11.1 (2005-06-09)To-DosUsing zdaemonzdaemon provides a script, zdaemon, that can be used to run other
programs as POSIX (Unix) daemons. (Of course, it is only usable on
POSIX-complient systems.)Using zdaemon requires specifying a number of options, which can be
given in a configuration file, or as command-line options.  It also
accepts commands teling it what do do.  The commands are:startStart a process as a daemonstopStop a running daemon processrestartStop and then restart a programstatusFind out if the program is runningforeground or fgRun a programkill signalSend a signal to the daemon processreopen_transcriptReopen the transcript log.  See the discussion of the transcript
log below.help commandGet help on a commandCommands can be given on a command line, or can be given using an
interactive interpreter.Let’s start with a simple example.  We’ll use command-line options to
run the echo command:sh> ./zdaemon -p ‘echo hello world’ fg
echo hello world
hello worldHere we used the -p option to specify a program to run.  We can
specify a program name and command-line options in the program
command. Note, however, that the command-line parsing is pretty
primitive.  Quotes and spaces aren’t handled correctly.  Let’s look at
a slightly more complex example.  We’ll run the sleep command as a
daemon :)sh> ./zdaemon -p ‘sleep 100’ start
. .
daemon process started, pid=819This ran the sleep daemon.  We can check whether it ran with the
status command:sh> ./zdaemon -p ‘sleep 100’ status
program running; pid=819We can stop it with the stop command:sh> ./zdaemon -p ‘sleep 100’ stop
. .
daemon process stoppedsh> ./zdaemon -p ‘sleep 100’ status
daemon manager not running
Failed: 3Normally, we control zdaemon using a configuration file.  Let’s create
a typical configuration file:<runner>
  program sleep 100
</runner>Now, we can run with the -C option to read the configuration file:sh> ./zdaemon -Cconf start
. .
daemon process started, pid=1136If we list the directory:sh> ls
conf
zdaemon
zdsockWe’ll see that a file, zdsock, was created.  This is a unix-domain
socket used internally by ZDaemon.  We’ll normally want to control
where this goes.sh> ./zdaemon -Cconf stop
. .
daemon process stoppedHere’s an updated configuration:<runner>
  program sleep 100
  socket-name /tmp/demo.zdsock
</runner>Now, when we run zdaemon:sh> ./zdaemon -Cconf start
. .
daemon process started, pid=1139sh> ls
conf
zdaemonThe socket file is created in the given directory.sh> ./zdaemon -Cconf stop
. .
daemon process stoppedIn the example, we included a command-line argument in the program
option. We can also provide options on the command line:<runner>
  program sleep
  socket-name /tmp/demo.zdsock
</runner>Then we can pass the program argument on the command line:sh> ./zdaemon -Cconf start 100
. .
daemon process started, pid=1149sh> ./zdaemon -Cconf status
program running; pid=1149sh> ./zdaemon -Cconf stop
. .
daemon process stoppedEnvironment VariablesSometimes, it is necessary to set environment variables before running
a program.  Perhaps the most common case for this is setting
LD_LIBRARY_PATH so that dynamically loaded libraries can be found.<runner>
  program env
  socket-name /tmp/demo.zdsock
</runner>
<environment>
  LIBRARY_PATH /home/foo/lib
  HOME /home/foo
</environment>Now, when we run the command, we’ll see out environment settings reflected:sh> ./zdaemon -Cconf fg
env
USER=jim
HOME=/home/foo
LOGNAME=jim
USERNAME=jim
TERM=dumb
PATH=/home/jim/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin
EMACS=t
LANG=en_US.UTF-8
SHELL=/bin/bash
EDITOR=emacs
LIBRARY_PATH=/home/foo/libTranscript logWhen zdaemon run a program in daemon mode, it disconnects the
program’s standard input, standard output, and standard error from the
controlling terminal.  It can optionally redirect the output to
standard error and standard output to a file.  This is done with the
transcript option.  This is, of course, useful for logging output from
long-running applications.Let’s look at an example. We’ll have a long-running process that
simple tails a data file:>>> f = open('data', 'w', 1)
>>> import os
>>> _ = f.write('rec 1\n'); f.flush(); os.fsync(f.fileno())Now, here’s out zdaemon configuration:<runner>
  program tail -f data
  transcript log
</runner>Now we’ll start:sh> ./zdaemon -Cconf start
. .
daemon process started, pid=7963After waiting a bit, if we look at the log file, it contains the tail output:>>> with open('log') as file:
...     file.read()
'rec 1\n'We can rotate the transcript log by renaming it and telling zdaemon to
reopen it:>>> import os
>>> os.rename('log', 'log.1')If we generate more output:>>> _ = f.write('rec 2\n'); f.flush(); os.fsync(f.fileno())The output will appear in the old file, because zdaemon still has it
open:>>> with open('log.1') as file:
...     file.read()
'rec 1\nrec 2\n'Now, if we tell zdaemon to reopen the file:sh> ./zdaemon -Cconf reopen_transcriptand generate some output:>>> _ = f.write('rec 3\n'); f.flush(); os.fsync(f.fileno())the output will show up in the new file, not the old:>>> with open('log') as file:
...     file.read()
'rec 3\n'>>> with open('log.1') as file:
...     file.read()
'rec 1\nrec 2\n'Close files and clean up:>>> f.close()sh> ./zdaemon -Cconf stop
. .
daemon process stoppedStart test program and timeoutNormally, zdaemon considers a process to have started when the process
itself has been created.  A process may take a while before it is
truly up and running.  For example, a database server or a web server
may take time before they’re ready to accept requests.You can optionally supply a test program, via thestart-test-programconfiguration option, that is called repeatedly until it returns a 0
exit status or until a time limit,start-timeout, has been reached.Reference DocumentationThe following options are available for use in the runner section of
configuration files and as command-line options.programCommand-line option: -p or –programThis option gives the command used to start the subprocess
managed by zdaemon.  This is currently a simple list of
whitespace-delimited words. The first word is the program
file, subsequent words are its command line arguments.  If the
program file contains no slashes, it is searched using $PATH.
(Note that there is no way to to include whitespace in the program
file or an argument, and under certain circumstances other
shell metacharacters are also a problem.)socket-nameCommand-line option: -s or –socket-name.The pathname of the Unix domain socket used for communication
between the zdaemon command-line tool and a daemon-management
process.  The default is relative to the current directory in
which zdaemon is started.  You want to specify
an absolute pathname here.This defaults to “zdsock”, which is created in the directory
in which zdrun is started.daemonCommand-line option: -d or –daemon.If this option is true, zdaemon runs in the background as a
true daemon.  It forks a child process which becomes the
subprocess manager, while the parent exits (making the shell
that started it believe it is done).  The child process also
does the following:if the directory option is set, change into that directoryredirect stdin, stdout and stderr to /dev/nullcall setsid() so it becomes a session leadercall umask() with specified valueThe default for this option is on by default.  The
command-line option therefore has no effect.  To disable
daemon mode, you must use a configuration file:<runner>
  program sleep 1
  daemon off
</runner>directoryCommand-line option: -z or –directory.If the daemon option is true (default), this option can
specify a directory into which zdrun.py changes as part of the
“daemonizing”.  If the daemon option is false, this option is
ignored.backoff-limitCommand-line option: -b or –backoff-limit.When the subprocess crashes, zdaemon inserts a one-second
delay before it restarts it.  When the subprocess crashes
again right away, the delay is incremented by one second, and
so on.  What happens when the delay has reached the value of
backoff-limit (in seconds), depends on the value of the
forever option.  If forever is false, zdaemon gives up at
this point, and exits.  An always-crashing subprocess will
have been restarted exactly backoff-limit times in this case.
If forever is true, zdaemon continues to attempt to restart
the process, keeping the delay at backoff-limit seconds.If the subprocess stays up for more than backoff-limit
seconds, the delay is reset to 1 second.This defaults to 10.foreverCommand-line option: -f or –forever.If this option is true, zdaemon will keep restarting a
crashing subprocess forever.  If it is false, it will give up
after backoff-limit crashes in a row.  See the description of
backoff-limit for details.This is disabled by default.exit-codesCommand-line option: -x or –exit-codes.This defaults to 0,2.If the subprocess exits with an exit status that is equal to
one of the integers in this list, zdaemon will not restart
it.  The default list requires some explanation.  Exit status
0 is considered a willful successful exit; the ZEO and Zope
server processes use this exit status when they want to stop
without being restarted.  (Including in response to a
SIGTERM.)  Exit status 2 is typically issued for command line
syntax errors; in this case, restarting the program will not
help!NOTE: this mechanism overrides the backoff-limit and forever
options; i.e. even if forever is true, a subprocess exit
status code in this list makes zdaemon give up.  To disable
this, change the value to an empty list.start-test-programA command that tests whether the program is up and running.
The command should exit with a zero exit statis if the program
is running and with a non-zero status otherwise.start-timeoutCommand-line option: -T or –start-timeout.If the program takes more thanstart-timeoutseconds to
start, then an error is printed and the control script will
exit with a non-zero exit status.stop-timeoutThis defaults to 300 seconds (5 minutes).When a stop command is issued, a SIGTERM signal is sent to the
process.  zdaemon waits for stop-timeout seconds for the
process to gracefully exit. If the process doesn’t exit in
that time, a SIGKILL signal is sent.userCommand-line option: -u or –user.When zdaemon is started by root, this option specifies the
user as who the the zdaemon process (and hence the daemon
subprocess) will run.  This can be a user name or a numeric
user id.  Both the user and the group are set from the
corresponding password entry, using setuid() and setgid().
This is done before zdaemon does anything else besides
parsing its command line arguments.NOTE: when zdaemon is not started by root, specifying this
option is an error.  (XXX This may be a mistake.)XXX The zdaemon event log file may be openedbeforesetuid() is called.  Is this good or bad?umaskCommand-line option: -m or –umask.When daemon mode is used, this option specifies the octal umask
of the subprocess.default-to-interactiveIf this option is true, zdaemon enters interactive mode
when it is invoked without a positional command argument.  If
it is false, you must use the -i or –interactive command line
option to zdaemon to enter interactive mode.This is enabled by default.logfileCommand-line option: -l or –logfile.This option specifies a log file that is the default target of
the “logtail” zdaemon command.NOTE: This is NOT the log file to which zdaemon writes its
logging messages!  That log file is specified by the
<eventlog> section described below.transcriptCommand-line option: -t or –transcript.The name of a file in which a transcript of all output from
the command being run will be written to when daemonized.If not specified, output from the command will be discarded.This only takes effect when the “daemon” option is enabled.promptThe prompt shown by the controller program.  The default must
be provided by the application.(Note that a few other options are available to support old
configuration files, but aren’t needed any more and can generally be
ignored.)In addition to the runner section, you can use an eventlog section
that specified one or more logfile subsections:<eventlog>
  <logfile>
    path /var/log/foo/foo.log
  </logfile>

  <logfile>
    path STDOUT
  </logfile>
</eventlog>In this example, log output is sent to a file and to standard out.
Log output from zdaemon usually isn’t very interesting but can be
handy for debugging.Change log5.0 (2023-05-24)Drop support for Python 2.7, 3.5, 3.6.4.4 (2022-12-02)Add support for Python 3.8, 3.9, 3.10, 3.11.Drop support for Python 3.4.Drop support forpython setup.py testto run the tests. (#23)Drop support for installing this package without havingsetuptools.4.3 (2018-10-30)Add support for Python 3.6 and 3.7.Drop support for Python 3.3.4.2.0 (2016-12-07)Add support for Python 3.5.Drop support for Python 2.6 and 3.2.4.1.0 (2015-04-16)Add--versioncommand line option (fixeshttps://github.com/zopefoundation/zdaemon/issues/4).killnow accepts signal names, not just numbers
(https://github.com/zopefoundation/zdaemon/issues/11).Restorelogreopenas an alias forkill USR2(removed in version
3.0.0 due to lack of tests):https://github.com/zopefoundation/zdaemon/issues/10.Makelogreopenalso reopen the transcript log:https://github.com/zopefoundation/zdaemon/issues/9.Reopen event log onlogreopenorreopen_transcript:https://github.com/zopefoundation/zdaemon/issues/8.Help message forreopen_transcript(https://github.com/zopefoundation/zdaemon/issues/5).Fix race condition wherestopwould be ignored if the daemon
manager was waiting before respawning a crashed program.https://github.com/zopefoundation/zdaemon/issues/13.Partially fix delayed deadlock when the transcript file runs into a
full disk (https://github.com/zopefoundation/zdaemon/issues/1).Fix test suite leaving stale processes behind
(https://github.com/zopefoundation/zdaemon/issues/7).4.0.1 (2014-12-26)Add support for PyPy.  (PyPy3 is pending release of a fix for:https://bitbucket.org/pypy/pypy/issue/1946)Add support for Python 3.4.Add-t/--transcriptcommand line option.zdaemon can now be invoked as a module as inpython-mzdaemon ...4.0.0 (2013-05-10)Add support for Python 3.2.4.0.0a1 (2013-02-15)Add tox support and MANIFEST.in for proper releasing.Add Python 3.3 support.Drop Python 2.4 and 2.5 support.3.0.5 (2012-11-27)Fixed: the status command didn’t return a non-zero exit status when
the program wasn’t running. This made it impossible for other
software (e.g. Puppet) to tell if a process was running.3.0.4 (2012-07-30)Fixed: The start command exited with a zero exit status even when
the program being started failed to start (or exited imediately).3.0.3 (2012-07-10)Fixed: programs started with zdaemon couldn’t, themselves, invoke
zdaemon.3.0.2 (2012-07-10)Fail :(3.0.1 (2012-06-08)Fixed:The change in 2.0.6 to set a user’s supplemental groups broke common
configurations in which the effective user was set viasuorsudo-uprior to invoking zdaemon.Now, zdaemon doesn’t set groups or the effective user if the
effective user is already set to the configured user.3.0.0 (2012-06-08)Added an option,start-test-programto supply a test command to
test whether the program managed by zdaemon is up and operational,
rather than just running.  When starting a program, the start
command doesn’t return until the test passes. You could, for
example, use this to wait until a web server is actually accepting
connections.Added astart-timeoutoption to error if a program takes too long to
start. This is especially useful in combination with thestart-test-programoption.Added an option, stop-timeout, to control how long to wait
for a graceful shutdown.Previously, this was controlled by backoff-limit, which didn’t make
much sense.Several undocumented, untested, and presumably unused features were removed.2.0.6 (2012-06-07)Fixed: When theuseroption was used to run as a particular
user, supplemental groups weren’t set to the user’s supplemental
groups.2.0.5 (2012-06-07)(Accidental release. Please ignore.)2.0.4 (2009-04-20)Version 2.0.3 broke support for relative paths to the socket (-soption andsocket-nameparameter), now relative paths work again
as in version 2.0.2.Fixed change log format, made table of contents nicer.Fixed author’s email address.Removed zpkg stuff.2.0.3 (2009-04-11)Added support to bootstrap on Jython.If the run directory does not exist it will be created. This allow to use/var/run/mydaemonas run directory when /var/run is a tmpfs (LP #318118).Bugs FixedNo longer uses a hard-coded file name (/tmp/demo.zdsock) in unit tests.
This lets you run the tests on Python 2.4 and 2.5 simultaneously without
spurious errors.make -h work again for both runner and control scripts.
Help is now taken from the __doc__ of the options class users by
the zdaemon script being run.2.0.2 (2008-04-05)Bugs FixedFixed backwards incompatible change in handling of environment option.2.0.1 (2007-10-31)Bugs FixedFixed test renormalizer that did not work in certain cases where the
environment was complex.2.0.0 (2007-07-19)Final release for 2.0.0.2.0a6 (2007-01-11)Bugs FixedWhen the user option was used, it only affected running the daemon.2.0a3, 2.0a4, 2.0a5 (2007-01-10)Bugs FixedThe new (2.0) mechanism used by zdaemon to start the daemon manager
broke some applications that extended zdaemon.Added extra checks to deal with programs that extend zdaemon
and copy the schema and thus don’t see updates to the ZConfig schema.2.0a2 (2007-01-10)New FeaturesAdded support for setting environment variables in the configuration
file.  This is useful when zdaemon is used to run programs that need
environment variables set (e.g. LD_LIBRARY_PATH).Added a command to rotate the transcript log.2.0a1 (2006-12-21)Bugs FixedIn non-daemon mode, start hung, producing annoying dots
when the program exited.The start command hung producing annoying dots if the daemon failed
to start.foreground and start had different semantics because one used
os.system and another used os.spawnNew FeaturesDocumentationCommand-line arguments can now be supplied to the start and
foreground (fg) commandszdctl now invokes itself to run zdrun.  This means that it’s
no-longer necessary to generate a separate zdrun script.  This
especially when the magic techniques to find and run zdrun using
directory sniffing fail to set the path correctly.The daemon mode is now enabled by default.  To get non-daemon mode,
you have to use a configuration file and set daemon to off
there. The old -d option is kept for backward compatibility, but is
a no-op.1.4a1 (2005-11-21)Fixed a bug in the distribution setup file.1.4a1 (2005-11-05)First semi-formal release.After some unknown release(???)Made ‘zdaemon.zdoptions’ not fail for –help when __main__.__doc__
is None.After 1.1Updated test ‘testRunIgnoresParentSignals’:o Use ‘mkdtemp’ to create a temporary directory to hold the test socketrather than creating the test socket in the test directory.
Hopefully this will be more robust.  Sometimes the test directory
has a path so long that the test socket can’t be created.o Changed management of ‘donothing.sh’.  This script is now created bythe test in the temporarily directory with the necessary
permissions. This is to avoids possible mangling of permissions
leading to spurious test failures.  It also avoids management of a
file in the source tree, which is a bonus.Rearranged source tree to conform to more usual zpkg-based layout:o Python package lives under ‘src’.o Dependencies added to ‘src’ as ‘svn:externals’.o Unit tests can now be run from a checkout.Made umask-based test failures due to running as root emit a more
forceful warning.1.1 (2005-06-09)SVN tag:  svn://svn.zope.org/repos/main/zdaemon/tags/zdaemon-1.1Tagged to make better ‘svn:externals’ linkage possible.To-DosMore docs:Document/demonstrate some important features, such as:working directoryBugs:help command