views:

1150

answers:

5

What would be the simplest way to daemonize a python script in Linux ? I need that this works with every flavor of Linux, so it should only use python based tools.

+3  A: 

nohup

Creating a daemon the Python way

moonshadow
Oh, I didn't know "nohup", thx!
edomaur
+13  A: 

See Stevens and also this lengthy thread on activestate which I found personally to be both mostly incorrect and much to verbose, and I came up with this:

from os import fork, setsid, umask, dup2
from sys import stdin, stdout, stderr

if fork(): exit(0)
umask(0) 
setsid() 
if fork(): exit(0)

stdout.flush()
stderr.flush()
si = file('/dev/null', 'r')
so = file('/dev/null', 'a+')
se = file('/dev/null', 'a+', 0)
dup2(si.fileno(), stdin.fileno())
dup2(so.fileno(), stdout.fileno())
dup2(se.fileno(), stderr.fileno())

If you need to stop that process again, it is required to know the pid, the usual solution to this is pidfiles. Do this if you need one

from os import getpid
outfile = open(pid_file, 'w')
outfile.write('%i' % getpid())
outfile.close()

For security reasons you might consider any of these after demonizing

from os import setuid, setgid, chdir
from pwd import getpwnam
from grp import getgrnam
setuid(getpwnam('someuser').pw_uid)
setgid(getgrnam('somegroup').gr_gid)
chdir('/')

You could also use nohup but that does not work well with python's subprocess module

Florian Bösch
Interesting. How do you start/stop the daemon ?
edomaur
After demonizing I write a pidfile
Florian Bösch
Okay, so after that I can use a standard init.d script with your examples, it should do the trick.
edomaur
Please, also add a os.chdir("/") to the code above.
ΤΖΩΤΖΙΟΥ
A: 

Use grizzled.os.daemonize:

$ easy_install grizzled

>>> from grizzled.os import daemonize
>>> daemon.daemonize()

To understand how this works or to do it yourself, read the discussion on ActiveState.

Jim
+1  A: 

If you do not care for actual discussions (which tend to go offtopic and do not offer authoritative response), you can choose some library that will make your tast easier. I'd recomment taking a look at ll-xist, this library contains large amount of life-saving code, like cron jobs helper, daemon framework, and (what is not interesting to you, but is really great) object-oriented XSL (ll-xist itself).

zgoda
That's an interesting library kit, I'll try it. Thanks.
edomaur
A: 

I have recently used Turkmenbashi :

$ easy_install turkmenbashi
import Turkmenbashi

class DebugDaemon (Turkmenbashi.Daemon):

    def config(self):
        self.debugging = True

    def go(self):
        self.debug('a debug message')
        self.info('an info message')
        self.warn('a warning message')
        self.error('an error message')
        self.critical('a critical message')

if __name__=="__main__":
    d = DebugDaemon()
    d.config()
    d.setenv(30, '/var/run/daemon.pid', '/tmp', None)
    d.start(d.go)
edomaur