Problem with any of os.system(), os.fork() & os.execp() and os.spawn()

Eric Chamberland Eric.Chamberland at videotron.ca
Wed Jan 2 10:42:21 EST 2002


Hi,

Al I want is to execute a simple tcsh script from a python process.  But the problem is
that a put processes in the background in my tcsh shell (with &) and the execution of the
script is stopped _only_ when the console from which I started the python process has been
terminated.

Here is my simple python script:
----------------------------
#!/usr/bin/env python
import os,time

time.sleep(5)
lArgs=['/tmp/myscript']

# we fork and execute the script
#if (os.fork() == 0):
#     os.execv(lArgs[0],lArgs)
#else:
#     os.wait()

#we execute the script with os.system:
os.system(lArgs[0])
----------------------------

and here is my simple script (put it in /tmp/myscript):
----------------------------
#!/bin/tcsh
rm -f /tmp/out.txt
echo "first" > /tmp/out.txt&
echo "second" >> /tmp/out.txt&
echo "third" >> /tmp/out.txt&
----------------------------

Here is how to reproduce the problem:

1- execute the python program and put it in background
2- before 5 seconds, exit the shell from which you executed the python program
3- wait .. ;-)
4- look at the file /tmp/out.txt, it will contain only one line, in occurance the word
"first"

If you don't exit the sheel from which you started the python program, it will work fine. 
Also, if you don't put the echo's in the background in "/tmp/myscript/, it will also work.

So, I need to have commands in "myscript" to be run in background, so how do I make this
simple thing work correctly?  I tried, fork and execv, spawn, and they all have the same
behavior...

Anyone can help?

Thanks,

Eric



More information about the Python-list mailing list