469,610 Members | 1,783 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,610 developers. It's quick & easy.

Daemonizing python

Hi!

I was wondering if it is possible to turn the current python proccess into
a unix daemon, but not doing it like this:
python myscript.py &
but from code programaticaly.
Thanks.
Sep 24 '06 #1
10 1506
NinjaZombie <ni*********@net.hrwrites:
I was wondering if it is possible to turn the current python proccess into
a unix daemon, but not doing it like this:
python myscript.py &
but from code programaticaly.
Yeah, os.fork and the parent process exits.
Sep 24 '06 #2
Paul Rubin wrote:
NinjaZombie <ni*********@net.hrwrites:
>I was wondering if it is possible to turn the current python
proccess into a unix daemon, but not doing it like this:
python myscript.py &
but from code programaticaly.
Yeah, os.fork and the parent process exits.
Or little helpers like twistd -- only feasible if you use Twisted
though.

Regards,
Björn

--
BOFH excuse #438:

sticky bit has come loose

Sep 24 '06 #3
Na dan Sun, 24 Sep 2006 23:19:12 +0200, Bjoern Schliessmann je napisao:
Paul Rubin wrote:
>NinjaZombie <ni*********@net.hrwrites:
>>I was wondering if it is possible to turn the current python
proccess into a unix daemon, but not doing it like this:
python myscript.py &
but from code programaticaly.
>Yeah, os.fork and the parent process exits.

Or little helpers like twistd -- only feasible if you use Twisted
though.
Thanks guys, but I'm a little new to this. Take a look at this very simple
code:

import os
print "Entering program"
os.fork()
while (1):
pass # some other work
I was expexting that after 'os.fork()', the rest of the program would run
in the background, which it isn't.
Sep 24 '06 #4
NinjaZombie <ni*********@net.hrwrites:
Thanks guys, but I'm a little new to this. Take a look at this very simple
code:

import os
print "Entering program"
os.fork()
while (1):
pass # some other work
Try it this way:

import os, sys
print "Entering program"

if os.fork():
sys.exit() # parent process exits so shell returns

while (1): # child process continues
pass # some other work
Sep 24 '06 #5
Na dan Sun, 24 Sep 2006 14:35:31 -0700, Paul Rubin je napisao:
Try it this way:

import os, sys
print "Entering program"

if os.fork():
sys.exit() # parent process exits so shell returns

while (1): # child process continues
pass # some other work
This works! Thanks a bunch, Paul.
Sep 24 '06 #6
NinjaZombie schrieb:
Hi!

I was wondering if it is possible to turn the current python proccess into
a unix daemon, but not doing it like this:
python myscript.py &
but from code programaticaly.
There is a good daemonization recipe on activstate:

http://aspn.activestate.com/ASPN/Coo...n/Recipe/66012

Diez
Sep 24 '06 #7
"Diez B. Roggisch" <de***@nospam.web.dewrites:
There is a good daemonization recipe on activstate:
http://aspn.activestate.com/ASPN/Coo...n/Recipe/66012
That is worth reading, including the long comment thread.
Sep 24 '06 #8
Hi,

what is the main difference of running a python program as a daemon or
as a cronjob?

I have written a program at work that checks all internet connections of
our failover sites and saves the results in a MySQL-database.
The whole program is made with django (a webframework) so I had to be
sure that the "checking procedure" is done every 30 minutes.

I used a cronjob for that job, but I don't know if a daemon would be
better...
NinjaZombie wrote:
Hi!

I was wondering if it is possible to turn the current python proccess into
a unix daemon, but not doing it like this:
python myscript.py &
but from code programaticaly.
Thanks.
--
homepage: http://www.the-grudge.net
ICQ: 9450091
Skype: thegrudge_2
Mozilla Thunderbird 1.5.0.5
Arch Linux
Sep 25 '06 #9
Paul Rubin wrote:
"Diez B. Roggisch" <de***@nospam.web.dewrites:
>There is a good daemonization recipe on activstate:
http://aspn.activestate.com/ASPN/Coo...n/Recipe/66012

That is worth reading, including the long comment thread.
Yeah, it is somewhat disappointing that the version in the comments is not
the one upfront. I should have already mentioned that, thanks for doing
that.
Diez
Sep 25 '06 #10

Andi Clemens wrote:
Hi,

what is the main difference of running a python program as a daemon or
as a cronjob?

I have written a program at work that checks all internet connections of
our failover sites and saves the results in a MySQL-database.
The whole program is made with django (a webframework) so I had to be
sure that the "checking procedure" is done every 30 minutes.
With a daemon, you might be able to flag up problems earlier than the
periodic check would allow. On the other hand, you *know* that cron
will run every 30 minutes. Do you *know* that your daemon will not
stop running somehow? I suppose I put more trust in cron than I do in
myself.

Regards,
Andy

Sep 25 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.

By using this site, you agree to our Privacy Policy and Terms of Use.