Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

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
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.


    Dave

Re: Perl script to wait for a program to finish
by ww (Bishop) 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 ?
        Ask Google... http://www.schwer.us/journal/2008/02/06/perl-sigchld-ignore-system-and-you/

        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.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (10)
As of 2014-12-22 12:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (116 votes), past polls