Daemon
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 cementpre_run
hook or an application controller sub-command, etc) - Manages a PID file including cleanup on
app.close()
API References:
- No external dependencies
- Unix/Linux
- macOS
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
: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
class MyApp(App):
class Meta:
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:[daemon]
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
app.run()
is called.from time import sleep
from cement import App
class MyApp(App):
class Meta:
label = 'myapp'
extensions = ['daemon']
with MyApp() as app:
app.daemonize()
app.run()
count = 0
while True:
count = count + 1
print('Iteration: %s' % count)
sleep(10)
Some applications may prefer to only daemonize certain sub-commands rather than the entire parent application. For example:
from cement import App, Controller, ex
class Base(Controller):
class Meta:
label = 'base'