473,387 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 1689
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Emile van Sebille | last post by:
QOTW: "If we get 2.3.3c1 out in early December, we could release 2.3.3 final before the end of the year, and start 2004 with a 100% bug-free codebase <wink>." -- Tim Peters "cjOr proWe vbCould...
0
by: Emile van Sebille | last post by:
QOTW: "Have you ever used the copy module? I am *not* a beginner, and have used it *once* (and I can't remember what for, either)." -- Michael Hudson "It will likely take a little practice...
0
by: Emile van Sebille | last post by:
QOTW (in the OS agnostic category): "There is a (very popular) Python package out there which exposes the win32 api. I'm not sure what it's called. (win32api? pythonwin? win32all?)" -- Francis...
4
by: jubei | last post by:
So I'm trying to daemonize a python script , I've got the following already if os.fork(): sys.exit() i'm not sure how to handle closing and then reopening stdin,stdout,stderr, or if...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.