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

How do i get back control to a the parent perl script if the application executing through the system() command of that script stops responding. The system command only returns back the control when the application its running is closed. I want the control back to the parent script as soon as the system command starts the application.. Any Help..?

Replies are listed 'Best First'.
Re: Getting back control..to main script
by lamprecht (Friar) on Feb 24, 2011 at 15:35 UTC

    If you are on Win32:

    "system(1, @args)" spawns an external process and immediately returns its process designator, without waiting for it to terminate.
    perlport

    Cheers, CHristoph
Re: Getting back control..to main script
by Anonymous Monk on Feb 24, 2011 at 14:16 UTC
Re: Getting back control..to main script
by ELISHEVA (Prior) on Feb 24, 2011 at 14:17 UTC

    Use exec instead of system. (Click on the links for documentation).

    Update: I forgot to include that this only works in combination with fork. For a nice PM node discussing how to use fork and exec together to get back control immediately after starting a child process see Fork and exec. This chapter that O'Reilly has posted from one of their books, has some good examples: see 10.10 Forking/Spawning Child Processes

      ELISHEVA: I think you have made a mistake. exec() on it's own will replace the currently running perl process with the new program, so a sucessfull call to exec will never return.

      Perhaps you are thinking of a fork/exec or one of the many inter process comumnication methods described in perlipc.

      To answer the original question, it depened on what shayak means by 'stops responding' Assuming that he can deduce that, then I would say the simplest method would be to start the process with open or fork/exec eg:

      my $child_pid = open $handle, '-|', '/path/to/program' or die "Error s +tarting program $!"; # Some time later the program stops responding kill($child_pid);

      or:

      my $child_pid = fork(); if( 0 == $child_pid ) { # Child exec '/path/to/program'; die "error starting program $!"; } # Some time later the program stops responding kill($child_pid);
      Exec didnt work..

        Exec should give you back control. If it didn't we're going to have to brainstorm together and that is quite hard to do with something as vague as "didn't work".

        Could you be more precise about what you mean by "didn't work". Didn't work as in "didn't run"? Didn't work as in "didn't get back control"? If you know your script started, how did you know that? What did you see/look for? If you didn't get back control, how did you know that?

        At this point, I think it might help if you reduced your code to the smallest possible number of lines that illustrate the problem and then post those lines (preferrably by updating your original post so everyone can see it). How exactly did you make the call to exec? What went before? What came after?

        If you are lucky, getting things down to the smallest number of lines that illustrate the problem (and I do mean smallest), might actually tell you why it didn't work. Otherwise, we'll all be in a better position to brainstorm with you.

Re: Getting back control..to main script
by hsinclai (Deacon) on Feb 24, 2011 at 14:36 UTC
    Will this node help (threads) ...
Re: Getting back control..to main script
by fidesachates (Monk) on Feb 24, 2011 at 15:58 UTC
    EDIT: I need to read my fellow monk's post more closely. The same suggestion was already posted by jethro. Sorry
    Ok I guess I'll throw my 2 cents in here. Reading through all the posts, there's no mention of what os you're running. Just in case you're in a *nix system, you'll want to use system("command&"). The system command unlike exec will return control to the perl program once the external command leaves the foreground which the & at the end of the command will do. Thus control is returned immediately after issuing the command.

    On Windows, I'd go with the fork+system/exec suggestions the rest of my fellow monks have suggested.
Re: Getting back control..to main script
by shayak (Acolyte) on Feb 24, 2011 at 15:01 UTC
    See, i will tell u what exactly is my problem.. I have a perl script. Thru dat perl script im calling a batch script which will launch an application in vmware. Now once that application starts, i want the control back to my perl script. The perl script gets back the control only after i close the application which is running in the vmware. I want the control back as soon as the application starts or gets launched..

      You can do this in perl with fork()+exec(), either directly or through a module like the aforementioned Proc::Background or Parallel::ForkManager. Or you can let the shell do the same trick with backgrounding the process:

      perl -e 'system("emacs &"); print "yes";'