472,811 Members | 1,472 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

"fork and exit" needed?

Hi

I'm a Python newbie, and would like to rewrite this Perl scrip
to be run with the Asterisk PBX:

http://www.voip-info.org/wiki/view/Asterisk+NetCID

Anyone knows if those lines are necessary, why, and what their
alternative is in Python?

-------
open STDOUT, '>/dev/null';
fork and exit;
-------

Thank you.
Nov 27 '06 #1
13 3077
Vincent Delporte wrote:
Hi

I'm a Python newbie, and would like to rewrite this Perl scrip
to be run with the Asterisk PBX:

http://www.voip-info.org/wiki/view/Asterisk+NetCID

Anyone knows if those lines are necessary, why, and what their
alternative is in Python?
open STDOUT, '>/dev/null';
Either redefine stdout to an open file object for /dev/null or run the
script as "script.py >/dev/null"
fork and exit;
something like:

if os.fork():
sys.exit(0)

Tells the parent to exit after the fork while the child keeps running.
These are both steps in becoming a daemon (the comp.unix.programmer
FAQ, while skewed toward C, explains why some of these steps are
needed).

Nov 28 '06 #2
In <8o********************************@4ax.com>, Vincent Delporte wrote:
Anyone knows if those lines are necessary, why, and what their
alternative is in Python?

-------
open STDOUT, '>/dev/null';
sys.stdout = open(os.devnull, 'w')

Ciao,
Marc 'BlackJack' Rintsch
Nov 28 '06 #3
Marc 'BlackJack' Rintsch <bj****@gmx.netwrote:
In <8o********************************@4ax.com>, Vincent Delporte wrote:
Anyone knows if those lines are necessary, why, and what their
alternative is in Python?

open STDOUT, '>/dev/null';

sys.stdout = open(os.devnull, 'w')
This doesn't have the desired effect

If you run this

import os,sys,time
print os.getpid()
sys.stdout = open(os.devnull, 'w')
time.sleep(60)

It prints its pid.

$ ls -l /proc/32004/fd
total 4
lrwx------ 1 ncw ncw 64 Nov 28 09:55 0 -/dev/pts/17
lrwx------ 1 ncw ncw 64 Nov 28 09:55 1 -/dev/pts/17
lrwx------ 1 ncw ncw 64 Nov 28 09:55 2 -/dev/pts/17
l-wx------ 1 ncw ncw 64 Nov 28 09:55 3 -/dev/null

That quite clearly shows that stdout isn't /dev/null which is required
for proper deamonisation under unix.

I'm not sure how you do open stdout to /dev/null in python though!

I suspect something like this...

import posix
posix.close(1)
posix.open("/dev/null", posix.O_WRONLY)
--
Nick Craig-Wood <ni**@craig-wood.com-- http://www.craig-wood.com/nick
Nov 28 '06 #4
Nick Craig-Wood wrote:
If you run this

import os,sys,time
print os.getpid()
sys.stdout = open(os.devnull, 'w')
time.sleep(60)

It prints its pid.
and not only that, if you run

print "world",
print "hello"

it prints "hello world" in the wrong order!

</F>

Nov 28 '06 #5
Dennis Lee Bieber <wl*****@ix.netcom.comwrote:
On Tue, 28 Nov 2006 04:30:09 -0600, Nick Craig-Wood
<ni**@craig-wood.comdeclaimed the following in comp.lang.python:

If you run this

import os,sys,time
print os.getpid()
sys.stdout = open(os.devnull, 'w')
time.sleep(60)

It prints its pid.
I would hope so, as you performed the print BEFORE reassigning
stdout...
That is what I intended. I wanted the pid to look in /proc/<pid>/fd
to see what file descriptors were really open.
import os,sys,time
print "pre:", os.getpid()
sys.stdout = open(os.devnull, 'w')
print "post:", os.getpid()
time.sleep(60)

(Granted, I'm on WinXP; I also suspect the original stdout is still open
in the background, maybe dualled with stderr?)
Yes that is the point - the original stdout is still open.

I don't think this discussion is relevant to windows though - windows
has its own way of making daemons.

--
Nick Craig-Wood <ni**@craig-wood.com-- http://www.craig-wood.com/nick
Nov 28 '06 #6
Nick Craig-Wood wrote:
>>open STDOUT, '>/dev/null';
sys.stdout = open(os.devnull, 'w')
$ ls -l /proc/32004/fd
total 4
lrwx------ 1 ncw ncw 64 Nov 28 09:55 0 -/dev/pts/17
lrwx------ 1 ncw ncw 64 Nov 28 09:55 1 -/dev/pts/17
lrwx------ 1 ncw ncw 64 Nov 28 09:55 2 -/dev/pts/17
l-wx------ 1 ncw ncw 64 Nov 28 09:55 3 -/dev/null

I'm not sure how you do open stdout to /dev/null in python though!
I suspect something like this...
import posix
posix.close(1)
posix.open("/dev/null", posix.O_WRONLY)
Yes, you're close enough... The explanations are here:
http://www.google.com/search?q=python%20close%20stdout,
I like this one in particular:
http://www.python.org/infogami-faq/l...ally-close-it/

If you explicitly want to leave file descriptors 0-2 present
(Do you gain anything by not closing them? If you know, do
tell...), but pointing do /dev/null, you could do:

null = os.open(os.devnull,os.O_WRONLY)
os.dup2(null,0)
os.dup2(null,1)
os.dup2(null,2)
os.close(null)

Untested.

More info on file descriptors and python here:
http://docs.python.org/lib/os-fd-ops.html

Mitja
Nov 29 '06 #7
On Tue, 28 Nov 2006 08:30:03 -0600, Nick Craig-Wood
<ni**@craig-wood.comwrote:
> import os,sys,time
print "pre:", os.getpid()
sys.stdout = open(os.devnull, 'w')
print "post:", os.getpid()
time.sleep(60)

(Granted, I'm on WinXP; I also suspect the original stdout is still open
in the background, maybe dualled with stderr?)

Yes that is the point - the original stdout is still open.

I don't think this discussion is relevant to windows though - windows
has its own way of making daemons.
Thanks everyone. I'll see if the Python script runs OK in an Asterisk
PBX on Linux.
Nov 29 '06 #8
Mitja Trampus <nu*@example.comwrote:
Nick Craig-Wood wrote:
I'm not sure how you do open stdout to /dev/null in python though!
I suspect something like this...
import posix
posix.close(1)
posix.open("/dev/null", posix.O_WRONLY)

Yes, you're close enough... The explanations are here:
http://www.google.com/search?q=python%20close%20stdout,
I like this one in particular:
http://www.python.org/infogami-faq/l...ally-close-it/

If you explicitly want to leave file descriptors 0-2 present
(Do you gain anything by not closing them? If you know, do
tell...),
It is traditional I think...

From the unix FAQ, http://www.erlenstar.demon.co.uk/unix/faq.txt :-

6. `close()' fds 0, 1, and 2. This releases the standard in, out, and
error we inherited from our parent process. We have no way of knowing
where these fds might have been redirected to. Note that many daemons
use `sysconf()' to determine the limit `_SC_OPEN_MAX'. `_SC_OPEN_MAX'
tells you the maximun open files/process. Then in a loop, the daemon
can close all possible file descriptors. You have to decide if you
need to do this or not. If you think that there might be
file-descriptors open you should close them, since there's a limit on
number of concurrent file descriptors.

7. Establish new open descriptors for stdin, stdout and stderr. Even if
you don't plan to use them, it is still a good idea to have them open.
The precise handling of these is a matter of taste; if you have a
logfile, for example, you might wish to open it as stdout or stderr,
and open `/dev/null' as stdin; alternatively, you could open
`/dev/console' as stderr and/or stdout, and `/dev/null' as stdin, or
any other combination that makes sense for your particular daemon.
but pointing do /dev/null, you could do:

null = os.open(os.devnull,os.O_WRONLY)
os.dup2(null,0)
os.dup2(null,1)
os.dup2(null,2)
os.close(null)

Untested.
Ah, dup2() was on the tip of my mind's tongue ;-)

You could write it like this, and then it even works on Windows!

import os
import sys
import time
print os.getpid()
null = os.open(os.devnull,os.O_RDWR)
os.dup2(null, sys.stdin.fileno())
os.dup2(null, sys.stdout.fileno())
os.dup2(null, sys.stderr.fileno())
os.close(null)
print "You won't see this"
print >>sys.stderr, "Or this"
time.sleep(60)

--
Nick Craig-Wood <ni**@craig-wood.com-- http://www.craig-wood.com/nick
Nov 29 '06 #9
On Tue, 28 Nov 2006 04:30:09 -0600, Nick Craig-Wood
<ni**@craig-wood.comwrote:
>I'm not sure how you do open stdout to /dev/null in python though!

I suspect something like this...

import posix
posix.close(1)
posix.open("/dev/null", posix.O_WRONLY)
Thanks everyone, but no go :-/ Neither the above nor "sys.stdout =
open(os.devnull, 'w') " trigger the application.

This is a script that is launched by the Asterisk open-source PBX
server when a call comes in. Its goal is to broadcast some messages to
a Windows caller ID application that is installed on all client hosts
so that they know who's calling.

Before I go ask in an Asterisk forum, is there some Python-related
issue that experienced Python developers can spot in this rewrite of a
Perl script that works?

Here's the original Perl script:
http://www.voip-info.org/wiki/view/Asterisk+NetCID

Here's my Python rewrite:
http://codecomplete.free.fr/asterisk/python_cid.txt

Yes, I changed the UDP port from 42685 to 42687 so that my development
client host is the only one getting the broadcast :-)

Thank you!
Nov 30 '06 #10
On Thu, 30 Nov 2006 15:38:11 +0100, Vincent Delporte <bl*@bla.com>
wrote:
>Here's my Python rewrite:
http://codecomplete.free.fr/asterisk/python_cid.txt
More information. Here's what Asterisk says when I call in:

*CLI>

-- Executing LookupCIDName("SIP/fxo-0844e458", "") in new stack

-- Changed Caller*ID name to Work

-- Executing AGI("SIP/fxo-0844e458",
"ncid.python.agi|087077XXXX|Bureau Freebox") in new stack

-- Launched AGI Script /var/lib/asterisk/agi-bin/ncid.python.agi
Failed to execute '/var/lib/asterisk/agi-bin/ncid.python.agi': Exec
format error

-- AGI Script ncid.python.agi completed, returning 0

FWIW, I wrote the original in a Windows text editor, and copy-pasted
the script in Asterisk by sshing into the server. CRLF vs. CR issue?
Something else?

Thanks.
Nov 30 '06 #11
On Thu, 30 Nov 2006 15:48:53 +0100, Vincent Delporte <bl*@bla.com>
wrote:
-- Launched AGI Script /var/lib/asterisk/agi-bin/ncid.python.agi
Failed to execute '/var/lib/asterisk/agi-bin/ncid.python.agi': Exec
format error
Stupid me :-/ Forgot the all-important

#!/usr/bin/python

Sorry for the disturbance..
Nov 30 '06 #12
Vincent Delporte wrote:
On Thu, 30 Nov 2006 15:38:11 +0100, Vincent Delporte <bl*@bla.com>
wrote:
>Here's my Python rewrite:
http://codecomplete.free.fr/asterisk/python_cid.txt
First line of your script should be:

#! /usr/bin/env python

which is how Linux knows what interpreter to use for the script.

HTH,
Mike

--
________________________________________________
Mike C. Fletcher
Designer, VR Plumber, Coder
http://www.vrplumber.com
http://blog.vrplumber.com

Nov 30 '06 #13
On Thu, 30 Nov 2006 10:18:24 -0500, "Mike C. Fletcher"
<mc******@vrplumber.comwrote:
>which is how Linux knows what interpreter to use for the script.
Thanks. That's what I found out after a bit more research. I didn't
pay attention to this because it's not needed to run under Windows,
and I was focusing on the fork() and stdout thing.
Nov 30 '06 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: beachnut | last post by:
Hi, all. This should be pretty easy: When parsing my XmlDocument object with a validating reader, what's the proper way to detect when it "exits" an element's block? It's the first time I've...
1
by: Huey | last post by:
Hi All, I encountered a funny thing, and my code schetch as below: #define READ 0 #define WRITE 1 int byteRead, status, pd; char buff;
24
by: Agnes | last post by:
I need to disable the exit button in the form . However, the min. and max. button need to keep it How ? Thanks a lot From Agnes
19
by: ern | last post by:
Right now I'm using exit(0) to terminate my program. My program is a console .exe application. After the "exit(0)" line of code is encountered, the console application waits for an enter press,...
2
by: Serious_Practitioner | last post by:
Good day, and thank you in advance for any assistance. I'm having trouble with something that I'm trying for the first time. Using Access 2000 - I want to run a function either on the click of a...
3
by: anders | last post by:
Hi! I have a problem converting a cgi-program to run on windows. The problem is that the CGI is collecting a lot of information (a orderhandlingsystem) and under unix the main process makes a...
0
by: maheshnew2007 | last post by:
Hi, I have simple Ant's build.xml file, which contains fork="yes". build.xml file: <?xml version="1.0"?> <project name="test" default="compile" basedir="."> <property name="src"...
1
by: JEJE | last post by:
To All, What is the difference between the following Exit code?? Exit (0)?? Exit(1)?? Exit (2)??
2
by: dstork | last post by:
Anyone know how to rename the "Exit Access" command at the bottom of the Office menu. I'd like to rename it to "Exit" as I had done in previous versions of Access. I know I can disable it with ...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.