Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Using 'kill' to see if a process is stil alive

by fletcher_the_dog (Friar)
on Jul 29, 2004 at 18:37 UTC ( #378459=perlquestion: print w/replies, xml ) Need Help??

fletcher_the_dog has asked for the wisdom of the Perl Monks concerning the following question:

I have a script that launches an application and then monitors to see if the application is has been closed. Here is a greatly simplified version:
use strict; my $pid = fork(); if (!$pid) { print "I am the child process\n"; system "emacs"; # this is just an example I am not really launching +emacs print "Child process should end now\n"; exit 0; } else { print "I am the parent of $pid\n"; while (kill 0,$pid) { print "My child is still alive\n"; sleep(3); # wait a little bit } print "Parent process done\n"; } __OUTPUT__ $ perl script.pl I am the child process I am the parent of 1324 My child is still alive My child is still alive My child is still alive Child process should end now My child is still alive My child is still alive etc.
The problem is that even after the child process is dead the parent process keeps going. According to the documentation for kill:

If SIGNAL is zero, no signal is sent to the process. This is a useful way to check that the process is alive and hasn't changed its UID. See perlport for notes on the portability of this construct.

So I thought "kill" would be a good way to see if the process was still going, but it doesn't appear to work. What am I missing? Is there a better way to do this?

Replies are listed 'Best First'.
Re: Using 'kill' to see if a process is stil alive
by Zaxo (Archbishop) on Jul 29, 2004 at 19:22 UTC

    anbrus is almost right, but you don't need wait to get SIGCHLD. The os always sends you that when the child exits. You just need to handle it. Try setting $SIG{CHLD} = 'IGNORE'; before launching the child.

    After Compline,
    Zaxo

Re: Using 'kill' to see if a process is stil alive
by ambrus (Abbot) on Jul 29, 2004 at 19:05 UTC

    If a process exits (or dies in any way), it becomes a zombie. When this happens, the parent gets a SIGCHLD. The parent process of the zombie can harvest the child with waitpid(2) or wait(2) or wait4(2).

    You have to use the builtin function waitpid to see if the child process is still living.

    For an example about this, see perldoc perlipc.

    Edit: clarified first para, as Zaxo notes in his reply. Original text was:

    If a process exits (or dies in any way), it becomes a zombie. The parent process of the zombie can harvest it with waitpid(2) or wait(2) or wait4(2). When this happens, the parent gets a SIGCHLD.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://378459]
Approved by Limbic~Region
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (2)
As of 2021-09-23 00:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?