Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > How to tell if a forked process is done?

Reply
Thread Tools

How to tell if a forked process is done?

 
 
John Lin
Guest
Posts: n/a
 
      09-23-2003
Howdy,

I want to know how to tell if a forked process is done.

Actually, my real question is that I want to run a shell script inside
of a python script, and after the shell script has finished running, I
want to do more stuff *condition* on the fact that the shell script
has finished running, inside the same python script.

The only way I can think of is to fork a process and then call the
shell script, as in:
pid = os.fork()
if pid == 0:
os.execl(shellscript_name.sh, "")
but how can I know if the shell script is finished?

In sum, my two questions are:
1. How can I know if a forked shell script is finished?
2. How can I run a shell script inside a python script without
forking a new process, so that I can know the shell script is done
from within the same python script?

thanks in advance,

John
 
Reply With Quote
 
 
 
 
Nick Welch
Guest
Posts: n/a
 
      09-23-2003
You can just do os.system("some command"), it will block until the shell
command is done, and return the exit code. If you need more control,
perhaps look into the popen2 module and the Popen3/4 classes inside it.

--
Nick Welch aka mackstann | mack @ incise.org | http://incise.org
Help stamp out and abolish redundancy.

 
Reply With Quote
 
 
 
 
Donn Cave
Guest
Posts: n/a
 
      09-24-2003
In article <(E-Mail Removed) >,
http://www.velocityreviews.com/forums/(E-Mail Removed) (John Lin) wrote:

> I want to know how to tell if a forked process is done.
>
> Actually, my real question is that I want to run a shell script inside
> of a python script, and after the shell script has finished running, I
> want to do more stuff *condition* on the fact that the shell script
> has finished running, inside the same python script.
>
> The only way I can think of is to fork a process and then call the
> shell script, as in:
> pid = os.fork()
> if pid == 0:
> os.execl(shellscript_name.sh, "")
> but how can I know if the shell script is finished?
>
> In sum, my two questions are:
> 1. How can I know if a forked shell script is finished?
> 2. How can I run a shell script inside a python script without
> forking a new process, so that I can know the shell script is done
> from within the same python script?


The simplest way to do what you appear to want is
os.system("shellscript_path")

If any of the command line is actually going to come
from input data, or you have some other reason to prefer
an argument list like exec, see os.spawnv and similar,
with os.P_WAIT as first parameter. Or if your next
question is going to be how to read from a pipe between
the two processes, see os.popen() for starters.

All of these functions will fork a process, but they use
waitpid or some similar function to suspend the calling
process until the fork exits. See man 2 waitpid, which
is available from python as posix.waitpid() or os.waitpid().

Donn Cave, (E-Mail Removed)
 
Reply With Quote
 
=?iso-8859-1?Q?Fran=E7ois?= Pinard
Guest
Posts: n/a
 
      09-24-2003
[John Lin]
> I want to know how to tell if a forked process is done.


A few people suggested `os.system()' already, and I presume this is what
you want and need.

In the less usual case you want concurrency between Python and the
forked shell command, for only later checking if the forked process is
done, the usual way is to send a zero signal to the child using
`os.kill()'. The zero signal would not do any damage in case your
forked process is still running. But if the process does not exist, the
parent will get an exception for the `os.kill()', which you may
intercept. So you know if the child is running or finished.

--
François Pinard http://www.iro.umontreal.ca/~pinard

 
Reply With Quote
 
David M. Cooke
Guest
Posts: n/a
 
      09-24-2003
At some point, (E-Mail Removed) (John Lin) wrote:

> Howdy,
>
> I want to know how to tell if a forked process is done.
>
> Actually, my real question is that I want to run a shell script inside
> of a python script, and after the shell script has finished running, I
> want to do more stuff *condition* on the fact that the shell script
> has finished running, inside the same python script.
>
> The only way I can think of is to fork a process and then call the
> shell script, as in:
> pid = os.fork()
> if pid == 0:
> os.execl(shellscript_name.sh, "")
> but how can I know if the shell script is finished?


Look up os.wait and os.waitpid in the Python Library Reference. Or,
for this case, use os.system().

--
|>|\/|<
/--------------------------------------------------------------------------\
|David M. Cooke
|cookedm(at)physics(dot)mcmaster(dot)ca
 
Reply With Quote
 
Jules Dubois
Guest
Posts: n/a
 
      09-24-2003
On 23 Sep 2003 16:47:40 -0700, in article
<(E-Mail Removed) >, John Lin wrote:

> The only way I can think of is to fork a process and then call the
> shell script, as in:
> pid = os.fork()
>[...]
> In sum, my two questions are:
> 1. How can I know if a forked shell script is finished?


Under Unix, there's a wait() system call to go along with fork(). I'll bet
Python was something similar.

> 2. How can I run a shell script inside a python script without
> forking a new process, so that I can know the shell script is done
> from within the same python script?


Something like system()?
 
Reply With Quote
 
Klaus Alexander Seistrup
Guest
Posts: n/a
 
      09-24-2003
François Pinard wrote:

> In the less usual case you want concurrency between Python and the
> forked shell command, for only later checking if the forked process
> is done, the usual way is to send a zero signal to the child using
> `os.kill()'. The zero signal would not do any damage in case your
> forked process is still running. But if the process does not exist,
> the parent will get an exception for the `os.kill()', which you may
> intercept. So you know if the child is running or finished.


This will yield a false positive and potential damage if the OS has
spawned another process with the same pid, and running under your uid,
as the task you wanted to supervise.


// Klaus

--
><> unselfish actions pay back better

 
Reply With Quote
 
Jeff Epler
Guest
Posts: n/a
 
      09-24-2003

os.waitpid() can tell whether a child has exited, and return its status
if so. It can either enter a blocking wait, or it can return
immediately.

>>> pid = os.spawnv(os.P_NOWAIT, "/bin/sleep", ["sleep", "30"])


With WNOHANG, it returns immediately. The returned pid is 0 to show
that the process has not exited yet.
>>> os.waitpid(pid, os.WNOHANG)

(0, 0)

Wait for the process to return. The second number is related to the
exit status and should be managed with os.WEXITSTATUS() etc.
>>> os.waitpid(pid, 0)

(29202, 0)

Waiting again produces a traceback (no danger from another process
created with the same pid)
>>> os.waitpid(pid, 0)

Traceback (most recent call last):
File "<stdin>", line 1, in ?
OSError: [Errno 10] No child processes

If you want to use an argument list instead of an argument string, but
always want to wait for the program to complete, use os.spawn* with
P_WAIT.

Jeff

 
Reply With Quote
 
=?iso-8859-1?Q?Fran=E7ois?= Pinard
Guest
Posts: n/a
 
      09-24-2003
[Klaus Alexander Seistrup]
> François Pinard wrote:
>
> > In the less usual case you want concurrency between Python and the
> > forked shell command, for only later checking if the forked process
> > is done, the usual way is to send a zero signal to the child using
> > `os.kill()'. The zero signal would not do any damage in case your
> > forked process is still running. But if the process does not exist,
> > the parent will get an exception for the `os.kill()', which you may
> > intercept. So you know if the child is running or finished.

>
> This will yield a false positive and potential damage if the OS has
> spawned another process with the same pid, and running under your uid,
> as the task you wanted to supervise.


Granted in theory, yet this does not seem to be considered a real
problem in practice. To generate another process with the same pid, the
system would need to generate so many intermediate processes that the
process counter would overflow and come back to its current value. The
`kill(pid, 0)' trick is still the way people seem to do it.

Do you know anything reasonably simple, safer, and that does the job?

--
François Pinard http://www.iro.umontreal.ca/~pinard

 
Reply With Quote
 
Christos TZOTZIOY Georgiou
Guest
Posts: n/a
 
      09-24-2003
On Wed, 24 Sep 2003 06:27:41 +0000 (UTC), rumours say that Klaus
Alexander Seistrup <(E-Mail Removed)> might have written:

>This will yield a false positive and potential damage if the OS has
>spawned another process with the same pid, and running under your uid,
>as the task you wanted to supervise.


This *could* happen (I have seen 16-bit-pid systems with a couple of
stupid processes spawning children too rapidly), but if you are not root
and you are sure that your own processes do not breed like rabbits, then
you can be almost sure that os.kill(pid, 0) will throw an EPERM if your
child has died.
--
TZOTZIOY, I speak England very best,
Microsoft Security Alert: the Matrix began as open source.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Detecting problems in a forked process James Colannino Python 2 12-30-2005 06:24 AM
dbx/gdb Choosing Which Forked Process To Follow clusardi2k@aol.com C++ 2 04-13-2005 02:19 PM
fork(): how can I kill forked process and all ITS children, but leaveparent alive? Alexander N. Spitzer C Programming 1 11-12-2004 01:25 PM
Win32 - forked process doesn't create a Window RL Perl 2 02-06-2004 04:41 AM



Advertisments