Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Perl script to wait for a program to finish

by renzosilv (Novice)
on Sep 28, 2011 at 03:32 UTC ( #928230=perlquestion: print w/replies, xml ) Need Help??
renzosilv has asked for the wisdom of the Perl Monks concerning the following question:

Hi: I looked around and tried a few things but I couldn't figure out how to get my script to wait for a program I start within my script; with a system() call. I am starting certain program within my script and all I need to do is delete a folder which is created by this program after it gets closed by the user. When the user closes this program my script is ending even though I thought it wouldn't if I used the system() call. I know it is got to be something easy but I couldn't find a good explanation online. Thanks you for any input.
  • Comment on Perl script to wait for a program to finish

Replies are listed 'Best First'.
Re: Perl script to wait for a program to finish
by davido (Archbishop) on Sep 28, 2011 at 03:46 UTC

    system does a fork and wait. But if you want to completely disassociate the system call from the parent process (meaning spawn the system call and then just move on, letting it do its thing alone without reporting back), you probably need to read Complete Disassociation of Child from Parent, in perlipc for an example of one way.


Re: Perl script to wait for a program to finish
by ww (Archbishop) on Sep 28, 2011 at 03:46 UTC

    My guess: it'll be a lot easier to offer a good explanation if you show us your code.

    If I understand what you wrote as you wrote it (intended?), your description doesn't match the way system works -- c.f. perldoc -f system:

    system LIST
    system PROGRAM LIST
           Does exactly the same thing as "exec LIST", except that a fork
           is done first, and the parent process waits for the child
           process to exit. ....

      I agree, this is probably a "faux" problem

      If you use system to open a program, i.e:

      `gimp`; print "gimp was closed\n"; print "... lots and lots of commands next";

      the script is stopped until the first line is finished, so the second, third etc lines, are "frozen" and never run until you close manually this gimp session.

       delete a folder which is created by this program after it gets closed by the user

      It depends on your specific case, you could need to add another system line below doing the cleanup, a rmdir $dir line, or nothing

      Sorry I didn't show any code. Is just a few lines long so I didn't think it was necessary. Example: system ("vp -e " . $ENV{'VREL'}); rm /home/$ENV{'verl'}; the problem is my program is dying when the user closes my vp program. I never get to the remove directory. I tried printing for debugging purposes also but my program is dying on that line.
Re: Perl script to wait for a program to finish
by jwkrahn (Monsignor) on Sep 28, 2011 at 10:32 UTC

    The user's program is probably sending a SIGCHLD signal to your perl program which is killing it.    You probably need to ignore this signal.

      How do I go about doing that ?

        perldoc perlipc

        You may also choose to assign the strings 'IGNORE' or 'DEFAULT' + as the handler, in which case Perl will try to discard the signal or d +o the default thing. On most Unix platforms, the "CHLD" (sometimes also known as "CL +D") signal has special behavior with respect to a value of 'IGNORE'. Sett +ing $SIG{CHLD} to 'IGNORE' on such a platform has the effect of not + creating zombie processes when the parent process fails to "wait()" on i +ts child processes (i.e. child processes are automatically reaped). Cal +ling "wait()" with $SIG{CHLD} set to 'IGNORE' usually returns "-1" o +n such platforms.
        Ask Google...

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://928230]
Approved by ww
and the fire pops...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2018-05-20 14:08 GMT
Find Nodes?
    Voting Booth?