How to tell if a forked process is done?

Donn Cave donn at u.washington.edu
Tue Sep 23 20:15:19 EDT 2003


In article <879b6a51.0309231547.428214f7 at posting.google.com>,
 joseph at ugcs.caltech.edu (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, donn at u.washington.edu




More information about the Python-list mailing list