Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

how can we tell when a background process is finished?

by Anonymous Monk
on Jun 20, 2004 at 09:32 UTC ( [id://368259]=perlquestion: print w/replies, xml ) Need Help??

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

Hi,

If we run system("program &");, is there a way to know when "program" has finished its execution?

Thank you.

Replies are listed 'Best First'.
Re: how can we tell when a background process is finished?
by dave_the_m (Monsignor) on Jun 20, 2004 at 09:51 UTC
    If we run system("program &");, is there a way to know when "program" has finished its execution?
    No.
    Instead, create a signal handler for SIGCHLD, then fork(), and do system("program") in the child.

    Dave.

      Or you can do exec("program") in the child, saving another process, since system() incorporates a fork+exec.

      --
      integral, resident of freenode's #perl
      

      Thanks...do you know where I can find an example of this? I understand how to fork and then do system("program") in the child, but I'm not sure how to handle the "signal handler" for SIGCHLD. I think I will have to use waitpid()?

        Take a look at perlmonkey's MP3 server with IO::Socket tutorial.
        It explains in the fifth paragraph exactly what you're asking about processes.

        Best regards,

        my ($author_nickname, $author_email) = ("DaWolf","erabbott\@terra.com.br") if ($author_name eq "Er Galvão Abbott");
Re: how can we tell when a background process is finished?
by dfaure (Chaplain) on Jun 20, 2004 at 10:43 UTC

    You may use the trick running

    my $pid = (`program &;echo $!`)[0];

    which gives you the id of background process. Then you may wait for this process to end.

    Not a very clever solution but you may have a try.

    [approx. syntax, please check...]

    --
    HTH, Dominique
    If the only tool you have is a hammer, you will see every problem as a nail.
    -- Abraham Maslow

Re: how can we tell when a background process is finished?
by vek (Prior) on Jun 20, 2004 at 20:00 UTC

    Perhaps you could explain what you are trying to do. For example, if you just need to wait for a program to finish, why place it in the background to begin with? A simple system("program"); would suffice. That way you always know when the program has finished because system will return.

    -- vek --

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2024-04-19 16:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found