cement.ext.ext_daemon

Cement daemon extension module.

class cement.ext.ext_daemon.Environment(**kw)[source]

Bases: object

This class provides a mechanism for altering the running processes environment.

Optional Arguments:

Keyword Arguments:
  • stdin – A file to read STDIN from. Default: /dev/null

  • stdout – A file to write STDOUT to. Default: /dev/null

  • stderr – A file to write STDERR to. Default: /dev/null

  • dir – The directory to run the process in.

  • pid_file – The filesystem path to where the PID (Process ID) should be written to. Default: None

  • user – The user name to run the process as. Default: os.getlogin()

  • group – The group name to run the process as. Default: The primary group of os.getlogin().

  • umask – The umask to pass to os.umask(). Default: 0

_write_pid_file()[source]

Writes os.getpid() out to self.pid_file.

daemonize()[source]

Fork the current process into a daemon.

References:

UNIX Programming FAQ:

1.7 How do I get my program to act like a daemon? http://www.unixguide.net/unix/programming/1.7.shtml http://www.faqs.org/faqs/unix-faq/programmer/faq/

Advanced Programming in the Unix Environment
  1. Richard Stevens, 1992, Addison-Wesley, ISBN 0-201-56317-7.

switch()[source]

Switch the current process’s user/group to self.user, and self.group. Change directory to self.dir, and write the current pid out to self.pid_file.

cement.ext.ext_daemon.cleanup(app)[source]

After application run time, this hook just attempts to clean up the pid_file if one was set, and exists.

cement.ext.ext_daemon.daemonize()[source]

This function switches the running user/group to that configured in config['daemon']['user'] and config['daemon']['group']. The default user is os.getlogin() and the default group is that user’s primary group. A pid_file and directory to run in is also passed to the environment.

It is important to note that with the daemon extension enabled, the environment will switch user/group/set pid/etc regardless of whether the --daemon option was passed at command line or not. However, the process will only ‘daemonize’ if the option is passed to do so. This allows the program to run exactly the same in forground or background.

cement.ext.ext_daemon.extend_app(app)[source]

Adds the --daemon argument to the argument object, and sets the default [daemon] config section options.