[asterisk-users] [Linux/Python 2.4.2] Forking Python doesn't work

Vincent vincent.delporte at bigfoot.com
Wed Feb 13 15:02:18 CST 2008


On Wed, 13 Feb 2008 14:25:52 +0100, Michiel van Baak
<michiel at vanbaak.info> wrote:
>If you want it to detach the program from it's parent you
>need the double fork yes.

Thanks for the confirmation, but when doing this, the NetCID
application no longer pops up, regardless of whether I put the NetCID
code in the "second parent" or "second child":

============
exten => 9300,1,AGI(/tmp/test5.py|${CALLERID(num)}|${CALLERID(name)})
exten => 9300,n,Dial(${MYPHONE},15)        
============
# cat test5.py 

#!/usr/bin/python
import socket,sys,time,os

def sendstuff(data):
       s.sendto(data,(ipaddr,portnum))
       return

log = open('/tmp/output.txt','w')

sys.stdout = open(os.devnull, 'w')
if os.fork():
        #Parent
        log.write("Step 1\n")
        log.close()
        os._exit(0)
else:
        #Child
        os.chdir('/tmp')
        os.setsid()
        os.umask(0)

        if os.fork():
                #Parent
                log.write("Step 2\n")
                log.close()

                now = time.localtime(time.time())
                dateandtime = time.strftime("%d/%m/%Y %H:%M", now)

                myarray = []
                myarray.append("STAT Rings: 1")
                myarray.append("RING")
                myarray.append("NAME " + cidname)
                myarray.append("TTSN Call from " + cidname)
                myarray.append("NMBR " + cidnum)
                myarray.append("TYPE K")

                s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
 s.setsockopt(socket.SOL_SOCKET,socket.SO_BROADCAST,True)

                portnum = 42685
                ipaddr = "192.168.0.255"

                for i in myarray:
                        sendstuff(i)

                time.sleep(5)
                sendstuff("IDLE " + dateandtime)
                os._exit(0)
        else:
                #Child        
                log.write("Step 3\n")        
                log.close()
                os._exit(0)
============

Has someone already forked a Python script successfully from Asterisk
through AGI?

Thanks.




More information about the asterisk-users mailing list