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

Hello Monkish Ones,

Please assist youthful grasshopper on his quest. I am trying to detect when a process actually ends AFTER the kill is sent.

I have tried the following:

while (system "/bin/ps -p $a") {
log_err "2. waiting for /bin/ps -p $a to clear\n";
sleep 10;
}

But this does not work because (system "/bin/ps -p $a") does not return a true when the process is still running.

What is the proper way to code this?

TNX.

Replies are listed 'Best First'.
Re: Detect Process End
by brian_d_foy (Abbot) on Nov 27, 2004 at 05:38 UTC
    If you know the process ID, use the built-in kill() command with the 0 signal.
    while( kill( 0, $pid ) ) { ... }
    Or did you already try that? What's your process doing after you try to kill it?
    --
    brian d foy <bdfoy@cpan.org>
      After I send a kill 15, $a, it takes a long time for the process to end -- like 20 to 30 minutes. So, I need to pause program execution until the process REALLY ends.

      I have not tried( kill( 0, $pid ) ) { ... }

        What is kill 0 (instead of kill 15)???
Re: Detect Process End
by ikegami (Patriarch) on Nov 27, 2004 at 05:07 UTC
    system "/bin/ps -p $a" does not return a true when the process is still running

    Specifically, system doesn't return at all while the child process is still running. Also, it doesn't provide the output in a variable, which I think you want. Try open(HANDLE, "/bin/ps -p $a |") if you wish to get the info as ps prints it, or use backticks ($info = `/bin/ps -p $a`) if you just want the output when ps is done running.

      Please excuse my ignorance, I am really a beginner at this. I don't really want any output at all. I don't want any information back from ps. I just want to get a "true" if the process is still running. TNX if you can get me over this hump.
        Here is a simpler explanation of ikegami(++)'s post:

        What you are trying to do is to keep running "ps -p $a" until you get a null output.

        In order to do that, you need to look at the output of that command.

        The "system" command does NOT provide the output.

        The backticks ($info = `/bin/ps -p $a`) DO provide that, capturing the output into $info.

        You then need to look for a null $info.

            ...each is assigned his own private delusion but he cannot see the baggage on his own back.

Re: Detect Process End
by Zaxo (Archbishop) on Nov 27, 2004 at 08:10 UTC

    You've got good advice above, here are a couple more possibilities.

    You can set up a $SIG{CHLD} handler to respond to a child process's exit, whatever the reason for it. If it's ok to sleep for a bit after some processing, waitpid will do that.

    After Compline,
    Zaxo

Re: Detect Process End
by QM (Parson) on Nov 27, 2004 at 17:45 UTC
    I posted some code here that demonstrates how to wait for child processes. That root node may give you some other ideas and places to look too.

    If your question is not about child processes, you may want to dig into IPC and interprocess signalling.

    -QM
    --
    Quantum Mechanics: The dreams stuff is made of

Re: Detect Process End
by Anonymous Monk on Nov 27, 2004 at 13:42 UTC
    I'm going to assume (bad idea, I know) that you're wanting to run this on linux. As such, linux stores process information in /proc/<pid>/. You could simply stat() the directory. If it exists, the process is running. If it fails, the process has ended.
      FYI -- other os's have the same or similar services. I know Solaris also has a /proc file system that can be used in the same way.

      --DrWhy

      "If God had meant for us to think for ourselves he would have given us brains. Oh, wait..."

Re: Detect Process End
by geektron (Curate) on Nov 30, 2004 at 01:34 UTC
    one thing no one else noted ... and i hope it's just because you posted pseudocode ...

    $a is a reserved variable for  sort. that could be anoother possible explanation.