The Daemon Extension enables applications to easily perform standard daemonization operations.
- Configurable runtime user and group
- Adds the
--daemoncommand line option
app.daemonize()function to trigger daemon functionality where necessary (either in a cement
pre_runhook or an application controller sub-command, etc)
- Manages a PID file including cleanup on
- No external dependencies
The daemon extension is configurable with the following settings under a
[daemon]section in the application configuration:
Configurations can be passed as defaults to
from cement import App, init_defaults
DEFAULTS = init_defaults('myapp', 'daemon')
DEFAULTS['daemon']['user'] = 'myuser'
DEFAULTS['daemon']['group'] = 'mygroup'
DEFAULTS['daemon']['dir'] = '/var/lib/myapp/'
DEFAULTS['daemon']['pid_file'] = '/var/run/myapp/myapp.pid'
DEFAULTS['daemon']['umask'] = 0
label = 'myapp'
config_defaults = DEFAULTS
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:
user = myuser
group = mygroup
dir = /var/lib/myapp/
pid_file = /var/run/myapp/myapp.pid
umask = 0
The following example shows how to add the daemon extension, as well as trigger daemon functionality before
from time import sleep
from cement import App
label = 'myapp'
extensions = ['daemon']
with MyApp() as app:
count = 0
count = count + 1
print('Iteration: %s' % count)
Some applications may prefer to only daemonize certain sub-commands rather than the entire parent application. For example:
from cement import App, Controller, ex
label = 'base'