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

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



Sun, 20 Jun 2004 23:42:21 GMT  
 Problem with any of os.system(), os.fork() & os.execp() and os.spawn()

Quote:

> So, I need to have commands in "myscript" to be run in background,
> so how do I make this simple thing work correctly?

This has nothing to do with python. Please try

(sleep 5;/tmp/myscript) &

in your shell. You will see that the same effect happens. If you
strace the shell, you will see that tcsh executes the sequence

...
1912  write(1, "first\n", 6)            = 6
1912  _exit(0)                          = ?
1905  setpgid(1912, 1912)               = 0

This is the fragment where the first subshell exits; 1905 is the tcsh
and 1912 the echo process. Shortly afterwards, you see

1905  write(17, "[1] 1912\n", 9)        = -1 EIO (Input/output error)
1905  munmap(0x40017000, 33577)         = 0
1905  _exit(1)                          = ?

FD 17, in this case, is a dup of stdout. Since the terminal is closed,
the write operation gets a SIGIO. In turn, tcsh decides to exit,
instead of invoking the rest of the script.

So if there is a problem, it is in tcsh. I'd recommend to rewrite this
script in Python.

Regards,
Martin



Mon, 21 Jun 2004 05:54:31 GMT  
 Problem with any of os.system(), os.fork() & os.execp() and os.spawn()

| 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.

Good luck!  Your problem here is really tcsh.  The only advice I can
give you is "never write scripts in csh", and that goes for tcsh as well.

I went so far as to verify that it works for me on NetBSD if I use, for
example, bash.  I wouldn't waste a second trying to figure out what's
wrong with tcsh or how it can be fixed, seriously.




Mon, 21 Jun 2004 08:26:02 GMT  
 Problem with any of os.system(), os.fork() & os.execp() and os.spawn()

...
| And if Donn and Martin haven't convinced you that writing scripts in any
| variant of the c shell is bad, have a look at;
|
| http://www.notelrac.com/essays.dir/softeng.dir/csh_harmful.html

Bah, a half-hearted attempt.  Someone who really cared could put
together a much larger list of botches, including things that tcsh
has to inherit.

He mentions Plan 9 "rc", and it is indeed a fine shell language,
pure, austere elegance.  See http://www.star.le.ac.uk/~tjg/rc/
for the UNIX port, a tiny shell that you can't expect to get in
any UNIX vendor's distribution, but it's portable - and what Python
programmer expects the interpreter to already be there?




Tue, 22 Jun 2004 02:03:47 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Newbie question: how to make os.fork() and os.pipe() work

2. os._exit(), os.kill(os.getpid(), 9)

3. os.popen() vs os.system()

4. Using os.system and os.popen from pythonwin under Windows NT

5. os.system() or os.execv() w/stdout redirect and PID tracking

6. os.system/os.popen ultrasparc

7. Blocking ctrl-c to os.popen() or os.system()

8. os.remove & os.rmdir

9. problem with os.environ and os.execvpe()

10. Black OS/2 icons and mini-icons for VisualWorks 2.0 for OS/2

11. FORTH/OS OS/4th

12. Oberon OS/Genera Lisp OS connection?

 

 
Powered by phpBB® Forum Software