Package park :: Package util :: Module daemon

Module daemon

source code

Disk And Execution MONitor (Daemon)

daemonize()
Detach the current process from the calling terminal so it can run as a service.
startstop()

Check the first argument to the program.

start: start the daemon stop: stop the daemon restart: stop the daemon if running, then start it again status: display running status run: run but not in daemon mode

Configurable daemon behaviors:

1.) The current working directory set to the "/" directory. 2.) The current file creation mode mask set to 0. 3.) Close all open files (1024). 4.) Redirect standard I/O streams to "/dev/null".

Almost none of this is necessary (or advisable) if your daemon is being started by inetd. In that case, stdin, stdout and stderr are all set up for you to refer to the network connection, and the fork()s and session manipulation should not be done (to avoid confusing inetd). Only the chdir() and umask() steps remain as useful.

References:

UNIX Programming FAQ 1.7 How do I get my program to act like a daemon?
http://www.erlenstar.demon.co.uk/unix/faq_2.html#SEC16
Advanced Programming in the Unix Environment
  1. Richard Stevens, 1992, Addison-Wesley, ISBN 0-201-56317-7.
History:
2001/07/10 by JÃŒrgen Hermann 2002/08/28 by Noah Spurrier 2003/02/24 by Clark Evans 2005/10/03 by Chad J. Schroeder 2008/11/05 by Paul Kienzle

Based on http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66012

Functions
 
daemonize(stdout='/dev/null', stderr=None, stdin='/dev/null', pidfile=None, startmsg='started with pid %s')
This forks the current process into a daemon.
source code
 
readpid(pidfile) source code
 
process_is_running(pid)
Check if the given process is running.
source code
 
startstop(stdout='/dev/null', stderr=None, stdin='/dev/null', pidfile='pid.txt', startmsg='started with pid %s')
Process start/stop/restart/status/run commands.
source code
 
test()
This is an example main function run by the daemon.
source code
Variables
  UMASK = 0
  WORKDIR = '/'
  MAXFD = 1024
  REDIRECT_TO = '/dev/null'
Function Details

daemonize(stdout='/dev/null', stderr=None, stdin='/dev/null', pidfile=None, startmsg='started with pid %s')

source code 

This forks the current process into a daemon.

The stdin, stdout, and stderr arguments are file names that will be opened and be used to replace the standard file descriptors in sys.stdin, sys.stdout, and sys.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 you expect.

process_is_running(pid)

source code 

Check if the given process is running.

Note that this just checks that the pid is in use; since process ids can be reused, this isn't a reliable test.

startstop(stdout='/dev/null', stderr=None, stdin='/dev/null', pidfile='pid.txt', startmsg='started with pid %s')

source code 

Process start/stop/restart/status/run commands.

Start/stop/restart allow the process to be used as an init.d service. Run runs the process without daemonizing, e.g., from inittab.

test()

source code 
This is an example main function run by the daemon. This prints a count and timestamp once per second.