Package: daemonpy3
## IntroductionMy first introduction to running a command as a daemon looked like this:nohup mycommand --myoption myarg >/dev/null 2>/dev/null &While this is a good for doing some development work, a proper daemon requiresquite a number of other things to happen.  The wikipedia pagehttp://en.wikipedia.org/wiki/Daemon\_(computing) says the following needs tohappen:- Dissociating from the controlling tty- Becoming a session leader- Becoming a process group leader- Executing as a background task by forking and exiting (once or twice). Thisis required sometimes for the process to become a session leader. It alsoallows the parent process to continue its normal execution.- Setting the root directory ("/") as the current working directory so that theprocess does not keep any directory in use that may be on a mounted filesystem (allowing it to be unmounted).- Changing the umask to 0 to allow open(), creat(), et al. operating systemcalls to provide their own permission masks and not to depend on the umask ofthe caller- Closing all inherited files at the time of execution that are left open bythe parent process, including file descriptors 0, 1 and 2 (stdin, stdout,stderr). Required files will be opened later.- Using a logfile, the console, or /dev/null as stdin, stdout, and stderrAdditionally, when running a process under monit or initd, it is typical forthe process id of the daemon to be stored in a file, so the daemon can bemonitored and killed easily.Finally, it is common to use `start`, `stop`, `restart`, and`status` commands to control the daemon.Daemoncmd takes care of creating a daemon the right way, putting its pid ina file, and interacting with the daemon via `start`, `stop`, etc. commands.Using it is as simple as:/path/to/daemoncmd start --pidfile /tmp/mydaemon.pid /path/to/mycommandSee the Usage section for more details.For more information on daemons and daemons in python, see:- http://pypi.python.org/pypi/python-daemon- http://www.python.org/dev/peps/pep-3143/- http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66012- http://en.wikipedia.org/wiki/Daemon\_(computing)## AlternativesHere are some projects that are similar in scope to daemoncmd:- http://pypi.python.org/pypi/zdaemon/2.0.4, and seehttp://ridingpython.blogspot.cz/2011/08/turning-your-python-script-into-linux.htmlfor a description.- https://github.com/indexzero/foreverWhile daemoncmd is a great way to start running a daemon, there are manyprograms designed to run and monitor processes in a way that is more robust andfull-featured than simply starting your own daemon.  Here is a list of some ofthem (see http://news.ycombinator.com/item?id=1368855 for a discussion):- launchd http://launchd.macosforge.org/- upstart http://upstart.ubuntu.com/- monit http://mmonit.com/monit/- supervisord http://supervisord.org/- daemontools http://cr.yp.to/daemontools.html- daemonize http://bmc.github.com/daemonize/- runit http://smarden.sunsite.dk/runit/- perp http://b0llix.net/perp/For production systems, I recommend using a tool like one of these.  I haveused supervisord and monit before.  Some of these tools work with daemons, likemonit, in which case daemoncmd could come in handy.  Other tools, likesupervisord, can supervise your process in such a way that you do not need todaemonize it.## ContributeIf this project does not do what you want, please open an issue or a pullrequest on github, https://github.com/todddeluca/daemoncmd.## Requirements- Probably Python 2.7 (since that is the only version it has been tested with.)## Installation### Install from pypi.python.orgDownload and install daemoncmd:pip install daemoncmd### Install from github.comClone and install daemoncmd:git clone git@github.com:todddeluca/daemoncmd.gitcd daemoncmdpython setup.py install## UsageThis module has two separate uses cases:- running a command as a daemon process- forking the current python process as a daemon.Daemonizing a command allows one to start, stop, and restart a non-daemoncommand as a daemon process.  This requires specifying a pid file which is usedto interact with the process.Usage examples:daemoncmd start --pidfile /tmp/daemon.pid \--stdout /tmp/daemon.log --stderr /tmp/daemon.log sleep 100daemoncmd restart --pidfile /tmp/daemon.pid \--stdout /tmp/daemon.log --stderr /tmp/daemon.log sleep 100daemoncmd status --pidfile /tmp/daemon.piddaemoncmd stop --pidfile /tmp/daemon.pidAnother use case is forking the current process into a daemon.  Accordingto pep 3143, forking as a daemon might be done by the standard library someday.Usage example:import daemoncmdimport mytaskdaemoncmd.daemonize()mytask.doit()Or from the command line:python -c 'import daemoncmd, mytask; daemoncmd.daemonize(); mytask.doit()'Other usage notes:- The command should not daemonize itself, since that is what this script doesand it would make the pid in the pidfile incorrect.- The command should refer to the absolute path of the executable, sincedaemonization sets the current working directory (cwd) to '/'.  Moregenerally, do not assume what the cwd is.- If daemoncmd is run by monit, etc., PATH and other env vars might berestricted for security reasons.- daemoncmd does not try to run the daemon as a particular uid.  That wouldbe handled by a process manager like monit, launchd, init, god, etc.- When running under monit, etc., pass environment variables to the commandlike so:FOO=testing daemoncmd start --pidfile /tmp/daemon.pid \--stdout /tmp/daemon.log printenv FOO