mercoledì 5 febbraio 2014

Sympathy for the daemon

Daemon is a linux process that runs in background; apache or mysql are daemons.
In a linux environment, we can run a background program  using cronjob, but it has some limitations, and in some scenarios it' s not a good idea.
For example, using cronjob, we can't control if the previously run has finished yet.
So often it's more convenient run a process as a daemon and this post describes how can be do this in python.
The main duty of a daemon consists to fork it self; it fires up a child process and detaches it. So parent process now can dies and it will give the console back. 

//Method

import os

try:
    ''' fork a process from parent processor 
        so father can exit and return control to the shell '''
    pid = os.fork()
except OSError, e:
    raise Exception, "%s [%d]" % (e.strerror, e.errno)
if (pid == 0):
    # this is first child

continue ...

Nessun commento:

Posta un commento