zdaemon-2.0.7/0000755000175000017500000000000011764435103011301 5ustar jimjimzdaemon-2.0.7/bootstrap.py0000644000175000017500000000413111764432140013665 0ustar jimjim############################################################################## # # Copyright (c) 2006 Zope Corporation and Contributors. # All Rights Reserved. # # This software is subject to the provisions of the Zope Public License, # Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS # FOR A PARTICULAR PURPOSE. # ############################################################################## """Bootstrap a buildout-based project Simply run this script in a directory containing a buildout.cfg. The script accepts buildout command-line options, so you can use the -c option to specify an alternate configuration file. $Id: bootstrap.py 68905 2006-06-29 10:46:56Z jim $ """ import os, shutil, sys, tempfile, urllib2 tmpeggs = tempfile.mkdtemp() ez = {} exec urllib2.urlopen('http://peak.telecommunity.com/dist/ez_setup.py' ).read() in ez ez['use_setuptools'](to_dir=tmpeggs, download_delay=0) import pkg_resources is_jython = sys.platform.startswith('java') if is_jython: import subprocess ws = pkg_resources.working_set if is_jython: assert subprocess.Popen( [sys.executable] + ['-c', 'from setuptools.command.easy_install import main; main()', '-mqNxd', tmpeggs, 'zc.buildout'], env = dict( PYTHONPATH = ws.find(pkg_resources.Requirement.parse('setuptools')).location ), ).wait() == 0 else: assert os.spawnle( os.P_WAIT, sys.executable, sys.executable, '-c', 'from setuptools.command.easy_install import main; main()', '-mqNxd', tmpeggs, 'zc.buildout', {'PYTHONPATH': ws.find(pkg_resources.Requirement.parse('setuptools')).location }, ) == 0 ws.add_entry(tmpeggs) ws.require('zc.buildout') import zc.buildout.buildout zc.buildout.buildout.main(sys.argv[1:] + ['bootstrap']) shutil.rmtree(tmpeggs) zdaemon-2.0.7/setup.py0000644000175000017500000000405511764432162013021 0ustar jimjim############################################################################## # # Copyright (c) 2006-2009 Zope Corporation and Contributors. # All Rights Reserved. # # This software is subject to the provisions of the Zope Public License, # Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS # FOR A PARTICULAR PURPOSE. # ############################################################################## version = '2.0.7' import os entry_points = """ [console_scripts] zdaemon = zdaemon.zdctl:main """ def read(*rnames): return open(os.path.join(os.path.dirname(__file__), *rnames)).read() try: from setuptools import setup setuptools_options = dict( zip_safe=False, entry_points=entry_points, include_package_data = True, install_requires=["ZConfig"], extras_require=dict(test=['zope.testing', 'mock']), ) except ImportError: from distutils.core import setup setuptools_options = {} name = "zdaemon" setup( name=name, version = version, url="http://www.python.org/pypi/zdaemon", license="ZPL 2.1", description= "Daemon process control library and tools for Unix-based systems", author="Zope Corporation and Contributors", author_email="zope-dev@zope.org", long_description=( read('README.txt') + '\n' + read('src/zdaemon/README.txt') + '\n' + read('CHANGES.txt') + '\n' + '========\n' + 'Download\n' + '========\n' ), packages=["zdaemon", "zdaemon.tests"], package_dir={"": "src"}, classifiers = [ 'Intended Audience :: Developers', 'Intended Audience :: System Administrators', 'License :: OSI Approved :: Zope Public License', 'Topic :: Utilities', 'Operating System :: POSIX', ], **setuptools_options) zdaemon-2.0.7/PKG-INFO0000644000175000017500000006010011764435103012373 0ustar jimjimMetadata-Version: 1.0 Name: zdaemon Version: 2.0.7 Summary: Daemon process control library and tools for Unix-based systems Home-page: http://www.python.org/pypi/zdaemon Author: Zope Corporation and Contributors Author-email: zope-dev@zope.org License: ZPL 2.1 Description: ***************************************************** ``zdaemon`` process controller for Unix-based systems ***************************************************** ``zdaemon`` is a Unix (Unix, Linux, Mac OS X) Python program that wraps commands to make them behave as proper daemons. .. contents:: =============== Using zdaemon =============== zdaemon provides a script, zdaemon, that can be used to running 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: start Start a process as a daemon stop Stop a running daemon process restart Stop and then restart a program status Find out if the program is running foreground or fg Run a program kill signal Send a signal to the daemon process reopen_transcript Reopen the transcript log. See the discussion of the transcript log below. help command Get help on a command Commands 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: >>> system("./zdaemon -p 'echo hello world' fg") echo hello world hello world Here 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 :) >>> system("./zdaemon -p 'sleep 100' start") . . daemon process started, pid=819 This ran the sleep deamon. We can check whether it ran with the status command: >>> system("./zdaemon -p 'sleep 100' status") program running; pid=819 We can stop it with the stop command: >>> system("./zdaemon -p 'sleep 100' stop") . . daemon process stopped >>> system("./zdaemon -p 'sleep 100' status") daemon manager not running Normally, we control zdaemon using a configuration file. Let's create a typical configuration file: >>> open('conf', 'w').write( ... ''' ... ... program sleep 100 ... ... ''') Now, we can run with the -C option to read the configuration file: >>> system("./zdaemon -Cconf start") . . daemon process started, pid=1136 If we list the directory: >>> system("ls") conf zdaemon zdsock We'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. >>> system("./zdaemon -Cconf stop") . . daemon process stopped >>> open('conf', 'w').write( ... ''' ... ... program sleep 100 ... socket-name /tmp/demo.zdsock ... ... '''.replace('/tmp', tmpdir)) >>> system("./zdaemon -Cconf start") . . daemon process started, pid=1139 >>> system("ls") conf zdaemon >>> import os >>> os.path.exists("/tmp/demo.zdsock".replace('/tmp', tmpdir)) True >>> system("./zdaemon -Cconf stop") . . daemon process stopped In the example, we included a command-line argument in the program option. We can also provide options on the command line: >>> open('conf', 'w').write( ... ''' ... ... program sleep ... socket-name /tmp/demo.zdsock ... ... '''.replace('/tmp', tmpdir)) >>> system("./zdaemon -Cconf start 100") . . daemon process started, pid=1149 >>> system("./zdaemon -Cconf status") program running; pid=1149 >>> system("./zdaemon -Cconf stop") . . daemon process stopped Environment Variables ===================== Sometimes, 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. >>> open('conf', 'w').write( ... ''' ... ... program env ... socket-name /tmp/demo.zdsock ... ... ... LD_LIBRARY_PATH /home/foo/lib ... HOME /home/foo ... ... '''.replace('/tmp', tmpdir)) >>> system("./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 LD_LIBRARY_PATH=/home/foo/lib Transcript log ============== When 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', 0) >>> import os >>> f.write('rec 1\n'); os.fsync(f.fileno()) >>> open('conf', 'w').write( ... ''' ... ... program tail -f data ... transcript log ... ... ''') >>> system("./zdaemon -Cconf start") . . daemon process started, pid=7963 .. Wait a little bit to make sure tail has a chance to work >>> import time >>> time.sleep(0.1) Now, if we look at the log file, it contains the tail output: >>> open('log').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'); os.fsync(f.fileno()) .. Wait a little bit to make sure tail has a chance to work >>> time.sleep(1) The output will appear in the old file, because zdaemon still has it open: >>> open('log.1').read() 'rec 1\nrec 2\n' Now, if we tell zdaemon to reopen the file: >>> system("./zdaemon -Cconf reopen_transcript") and generate some output: >>> f.write('rec 3\n'); os.fsync(f.fileno()) .. Wait a little bit to make sure tail has a chance to work >>> time.sleep(1) the output will show up in the new file, not the old: >>> open('log').read() 'rec 3\n' >>> open('log.1').read() 'rec 1\nrec 2\n' Reference Documentation ======================= The following options are available for use in the runner section of configuration files and as command-line options. program Command-line option: -p or --program This 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-name Command-line option: -s or --socket-name. The pathname of the Unix domain socket used for communication between the zdaemon command-line tool and a deamon-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. daemon Command-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 directory - redirect stdin, stdout and stderr to /dev/null - call setsid() so it becomes a session leader - call umask() with specified value The 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:: program sleep 1 daemon off directory Command-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-limit Command-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. forever Command-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-codes Command-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. user Command-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 opened *before* setuid() is called. Is this good or bad? umask Command-line option: -m or --umask. When daemon mode is used, this option specifies the octal umask of the subprocess. default-to-interactive If 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. 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 section described below. 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. prompt The 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:: path /var/log/foo/foo.log path STDOUT 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. =========== Changelog =========== 2.0.7 (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 via ``su`` or ``sudo -u`` prior to invoking zdaemon. Now, zdaemon doesn't set groups or the effective user if the effective user is already set to the configured user. 2.0.6 (2012-06-07) ================== - Fixed: When the ``user`` option 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 (``-s`` option and ``socket-name`` parameter), 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/mydaemon` as run directory when /var/run is a tmpfs (LP #318118). Bugs Fixed ---------- - No longer uses a hardcoded filename (/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 Fixed ---------- - Fixed backwards incompatible change in handling of environment option. 2.0.1 (2007-10-31) ================== Bugs Fixed ---------- - Fixed 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 Fixed ---------- - When the user option was used, it only affected running the daemon. 2.0a3, 2.0a4, 2.0a5 (2007-01-10) ================================ Bugs Fixed ---------- - The 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 Features ------------ - Added 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 Fixed ---------- - In non-daemon mode, start hung, producing annoying dots when the program exited. - The start command hung producing annoying dots if the deamon failed to start. - foreground and start had different semantics because one used os.system and another used os.spawn New Features ------------ - Documentation - Command-line arguments can now be supplied to the start and foreground (fg) commands - zdctl 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 corrrectly. - The daemon mode is now enabled by default. To get non-deamon mode, you have to use a configuration file and set deamon 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.1 ========= - Updated test 'testRunIgnoresParentSignals': o Use 'mkdtemp' to create a temporary directory to hold the test socket rather 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 by the test in the temporarary 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.1 - Tagged to make better 'svn:externals' linkage possible. To-Dos ====== More docs: - Document/demonstrate some important features, such as: - working directory Bugs: - help command ======== Download ======== Platform: UNKNOWN Classifier: Intended Audience :: Developers Classifier: Intended Audience :: System Administrators Classifier: License :: OSI Approved :: Zope Public License Classifier: Topic :: Utilities Classifier: Operating System :: POSIX zdaemon-2.0.7/CHANGES.txt0000644000175000017500000001206711764432140013116 0ustar jimjim=========== Changelog =========== 2.0.7 (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 via ``su`` or ``sudo -u`` prior to invoking zdaemon. Now, zdaemon doesn't set groups or the effective user if the effective user is already set to the configured user. 2.0.6 (2012-06-07) ================== - Fixed: When the ``user`` option 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 (``-s`` option and ``socket-name`` parameter), 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/mydaemon` as run directory when /var/run is a tmpfs (LP #318118). Bugs Fixed ---------- - No longer uses a hardcoded filename (/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 Fixed ---------- - Fixed backwards incompatible change in handling of environment option. 2.0.1 (2007-10-31) ================== Bugs Fixed ---------- - Fixed 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 Fixed ---------- - When the user option was used, it only affected running the daemon. 2.0a3, 2.0a4, 2.0a5 (2007-01-10) ================================ Bugs Fixed ---------- - The 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 Features ------------ - Added 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 Fixed ---------- - In non-daemon mode, start hung, producing annoying dots when the program exited. - The start command hung producing annoying dots if the deamon failed to start. - foreground and start had different semantics because one used os.system and another used os.spawn New Features ------------ - Documentation - Command-line arguments can now be supplied to the start and foreground (fg) commands - zdctl 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 corrrectly. - The daemon mode is now enabled by default. To get non-deamon mode, you have to use a configuration file and set deamon 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.1 ========= - Updated test 'testRunIgnoresParentSignals': o Use 'mkdtemp' to create a temporary directory to hold the test socket rather 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 by the test in the temporarary 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.1 - Tagged to make better 'svn:externals' linkage possible. To-Dos ====== More docs: - Document/demonstrate some important features, such as: - working directory Bugs: - help command zdaemon-2.0.7/README.txt0000644000175000017500000000045211764432140012776 0ustar jimjim***************************************************** ``zdaemon`` process controller for Unix-based systems ***************************************************** ``zdaemon`` is a Unix (Unix, Linux, Mac OS X) Python program that wraps commands to make them behave as proper daemons. .. contents:: zdaemon-2.0.7/setup.cfg0000644000175000017500000000007311764435103013122 0ustar jimjim[egg_info] tag_build = tag_date = 0 tag_svn_revision = 0 zdaemon-2.0.7/src/0000755000175000017500000000000011764435103012070 5ustar jimjimzdaemon-2.0.7/src/zdaemon.egg-info/0000755000175000017500000000000011764435103015217 5ustar jimjimzdaemon-2.0.7/src/zdaemon.egg-info/dependency_links.txt0000644000175000017500000000000111764435103021265 0ustar jimjim zdaemon-2.0.7/src/zdaemon.egg-info/PKG-INFO0000644000175000017500000006010011764435103016311 0ustar jimjimMetadata-Version: 1.0 Name: zdaemon Version: 2.0.7 Summary: Daemon process control library and tools for Unix-based systems Home-page: http://www.python.org/pypi/zdaemon Author: Zope Corporation and Contributors Author-email: zope-dev@zope.org License: ZPL 2.1 Description: ***************************************************** ``zdaemon`` process controller for Unix-based systems ***************************************************** ``zdaemon`` is a Unix (Unix, Linux, Mac OS X) Python program that wraps commands to make them behave as proper daemons. .. contents:: =============== Using zdaemon =============== zdaemon provides a script, zdaemon, that can be used to running 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: start Start a process as a daemon stop Stop a running daemon process restart Stop and then restart a program status Find out if the program is running foreground or fg Run a program kill signal Send a signal to the daemon process reopen_transcript Reopen the transcript log. See the discussion of the transcript log below. help command Get help on a command Commands 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: >>> system("./zdaemon -p 'echo hello world' fg") echo hello world hello world Here 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 :) >>> system("./zdaemon -p 'sleep 100' start") . . daemon process started, pid=819 This ran the sleep deamon. We can check whether it ran with the status command: >>> system("./zdaemon -p 'sleep 100' status") program running; pid=819 We can stop it with the stop command: >>> system("./zdaemon -p 'sleep 100' stop") . . daemon process stopped >>> system("./zdaemon -p 'sleep 100' status") daemon manager not running Normally, we control zdaemon using a configuration file. Let's create a typical configuration file: >>> open('conf', 'w').write( ... ''' ... ... program sleep 100 ... ... ''') Now, we can run with the -C option to read the configuration file: >>> system("./zdaemon -Cconf start") . . daemon process started, pid=1136 If we list the directory: >>> system("ls") conf zdaemon zdsock We'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. >>> system("./zdaemon -Cconf stop") . . daemon process stopped >>> open('conf', 'w').write( ... ''' ... ... program sleep 100 ... socket-name /tmp/demo.zdsock ... ... '''.replace('/tmp', tmpdir)) >>> system("./zdaemon -Cconf start") . . daemon process started, pid=1139 >>> system("ls") conf zdaemon >>> import os >>> os.path.exists("/tmp/demo.zdsock".replace('/tmp', tmpdir)) True >>> system("./zdaemon -Cconf stop") . . daemon process stopped In the example, we included a command-line argument in the program option. We can also provide options on the command line: >>> open('conf', 'w').write( ... ''' ... ... program sleep ... socket-name /tmp/demo.zdsock ... ... '''.replace('/tmp', tmpdir)) >>> system("./zdaemon -Cconf start 100") . . daemon process started, pid=1149 >>> system("./zdaemon -Cconf status") program running; pid=1149 >>> system("./zdaemon -Cconf stop") . . daemon process stopped Environment Variables ===================== Sometimes, 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. >>> open('conf', 'w').write( ... ''' ... ... program env ... socket-name /tmp/demo.zdsock ... ... ... LD_LIBRARY_PATH /home/foo/lib ... HOME /home/foo ... ... '''.replace('/tmp', tmpdir)) >>> system("./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 LD_LIBRARY_PATH=/home/foo/lib Transcript log ============== When 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', 0) >>> import os >>> f.write('rec 1\n'); os.fsync(f.fileno()) >>> open('conf', 'w').write( ... ''' ... ... program tail -f data ... transcript log ... ... ''') >>> system("./zdaemon -Cconf start") . . daemon process started, pid=7963 .. Wait a little bit to make sure tail has a chance to work >>> import time >>> time.sleep(0.1) Now, if we look at the log file, it contains the tail output: >>> open('log').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'); os.fsync(f.fileno()) .. Wait a little bit to make sure tail has a chance to work >>> time.sleep(1) The output will appear in the old file, because zdaemon still has it open: >>> open('log.1').read() 'rec 1\nrec 2\n' Now, if we tell zdaemon to reopen the file: >>> system("./zdaemon -Cconf reopen_transcript") and generate some output: >>> f.write('rec 3\n'); os.fsync(f.fileno()) .. Wait a little bit to make sure tail has a chance to work >>> time.sleep(1) the output will show up in the new file, not the old: >>> open('log').read() 'rec 3\n' >>> open('log.1').read() 'rec 1\nrec 2\n' Reference Documentation ======================= The following options are available for use in the runner section of configuration files and as command-line options. program Command-line option: -p or --program This 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-name Command-line option: -s or --socket-name. The pathname of the Unix domain socket used for communication between the zdaemon command-line tool and a deamon-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. daemon Command-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 directory - redirect stdin, stdout and stderr to /dev/null - call setsid() so it becomes a session leader - call umask() with specified value The 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:: program sleep 1 daemon off directory Command-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-limit Command-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. forever Command-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-codes Command-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. user Command-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 opened *before* setuid() is called. Is this good or bad? umask Command-line option: -m or --umask. When daemon mode is used, this option specifies the octal umask of the subprocess. default-to-interactive If 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. 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 section described below. 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. prompt The 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:: path /var/log/foo/foo.log path STDOUT 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. =========== Changelog =========== 2.0.7 (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 via ``su`` or ``sudo -u`` prior to invoking zdaemon. Now, zdaemon doesn't set groups or the effective user if the effective user is already set to the configured user. 2.0.6 (2012-06-07) ================== - Fixed: When the ``user`` option 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 (``-s`` option and ``socket-name`` parameter), 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/mydaemon` as run directory when /var/run is a tmpfs (LP #318118). Bugs Fixed ---------- - No longer uses a hardcoded filename (/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 Fixed ---------- - Fixed backwards incompatible change in handling of environment option. 2.0.1 (2007-10-31) ================== Bugs Fixed ---------- - Fixed 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 Fixed ---------- - When the user option was used, it only affected running the daemon. 2.0a3, 2.0a4, 2.0a5 (2007-01-10) ================================ Bugs Fixed ---------- - The 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 Features ------------ - Added 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 Fixed ---------- - In non-daemon mode, start hung, producing annoying dots when the program exited. - The start command hung producing annoying dots if the deamon failed to start. - foreground and start had different semantics because one used os.system and another used os.spawn New Features ------------ - Documentation - Command-line arguments can now be supplied to the start and foreground (fg) commands - zdctl 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 corrrectly. - The daemon mode is now enabled by default. To get non-deamon mode, you have to use a configuration file and set deamon 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.1 ========= - Updated test 'testRunIgnoresParentSignals': o Use 'mkdtemp' to create a temporary directory to hold the test socket rather 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 by the test in the temporarary 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.1 - Tagged to make better 'svn:externals' linkage possible. To-Dos ====== More docs: - Document/demonstrate some important features, such as: - working directory Bugs: - help command ======== Download ======== Platform: UNKNOWN Classifier: Intended Audience :: Developers Classifier: Intended Audience :: System Administrators Classifier: License :: OSI Approved :: Zope Public License Classifier: Topic :: Utilities Classifier: Operating System :: POSIX zdaemon-2.0.7/src/zdaemon.egg-info/requires.txt0000644000175000017500000000004111764435103017612 0ustar jimjimZConfig [test] zope.testing mockzdaemon-2.0.7/src/zdaemon.egg-info/top_level.txt0000644000175000017500000000001011764435103017740 0ustar jimjimzdaemon zdaemon-2.0.7/src/zdaemon.egg-info/not-zip-safe0000644000175000017500000000000111764432177017455 0ustar jimjim zdaemon-2.0.7/src/zdaemon.egg-info/entry_points.txt0000644000175000017500000000006011764435103020511 0ustar jimjim [console_scripts] zdaemon = zdaemon.zdctl:main zdaemon-2.0.7/src/zdaemon.egg-info/SOURCES.txt0000644000175000017500000000127311764435103017106 0ustar jimjimCHANGES.txt README.txt bootstrap.py buildout.cfg setup.py src/zdaemon/README.txt src/zdaemon/__init__.py src/zdaemon/component.xml src/zdaemon/sample.conf src/zdaemon/schema.xml src/zdaemon/zdctl.py src/zdaemon/zdoptions.py src/zdaemon/zdrun.py src/zdaemon.egg-info/PKG-INFO src/zdaemon.egg-info/SOURCES.txt src/zdaemon.egg-info/dependency_links.txt src/zdaemon.egg-info/entry_points.txt src/zdaemon.egg-info/not-zip-safe src/zdaemon.egg-info/requires.txt src/zdaemon.egg-info/top_level.txt src/zdaemon/tests/__init__.py src/zdaemon/tests/nokill.py src/zdaemon/tests/parent.py src/zdaemon/tests/tests.py src/zdaemon/tests/testuser.py src/zdaemon/tests/testzdoptions.py src/zdaemon/tests/testzdrun.pyzdaemon-2.0.7/src/zdaemon/0000755000175000017500000000000011764435103013525 5ustar jimjimzdaemon-2.0.7/src/zdaemon/__init__.py0000644000175000017500000000127611764432140015642 0ustar jimjim############################################################################## # # Copyright (c) 2001, 2002 Zope Corporation and Contributors. # All Rights Reserved. # # This software is subject to the provisions of the Zope Public License, # Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS # FOR A PARTICULAR PURPOSE. # ############################################################################## """zdaemon -- a package to manage a daemon application.""" zdaemon-2.0.7/src/zdaemon/zdoptions.py0000644000175000017500000004114711764432140016135 0ustar jimjim############################################################################## # # Copyright (c) 2003 Zope Corporation and Contributors. # All Rights Reserved. # # This software is subject to the provisions of the Zope Public License, # Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS # FOR A PARTICULAR PURPOSE. # ############################################################################## """Option processing for zdaemon and related code.""" import os import sys import getopt import ZConfig class ZDOptions: """a zdaemon script. Usage: python