Daemon

Introduction

The Daemon Extension enables applications to easily perform standard daemonization operations.

Features

  • Configurable runtime user and group

  • Adds the --daemon command line option

  • Add app.daemonize() function to trigger daemon functionality where necessary (either in a cement pre_run hook or an application controller sub-command, etc)

  • Manages a PID file including cleanup on app.close()

API References:

Requirements

  • No external dependencies

Platform Support

  • Unix/Linux

  • macOS

Configuration

The daemon extension is configurable with the following settings under a [daemon] section in the application configuration:

Setting

Description

user

The user name the process runs as. Default: os.getlogin()

group

The group name the process runs as. Default: the primary group of the user

dir

The directory that the process runs in. Default: /

pid_file

The filesystem path to store the PID (Process ID) file. Default: None

umask

The UMASK value to pass to os.umask(). Default: 0

Configurations can be passed as defaults to App:

Application defaults are then overridden by configurations parsed via a [demon] config section in any of the applications configuration paths. An example configuration block would look like:

Usage

The following example shows how to add the daemon extension, as well as trigger daemon functionality before app.run() is called.

Some applications may prefer to only daemonize certain sub-commands rather than the entire parent application. For example:

Daemonizing Without Commandline Option

Some use cases might require daemonizing the process without having to always pass the --daemon option, or where passing the option might be redundant. You can work around that programatically by simply overriding the daemon argument value in order to force daemonization even if --daemon wasn’t passed.

Note that this would only work after arguments have been parsed (i.e. after app.run() is called).

Last updated