http://www.perlmonks.org?node_id=889984


in reply to Getting back control..to main script

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

  • Comment on Re: Getting back control..to main script

Replies are listed 'Best First'.
Re^2: Getting back control..to main script
by chrestomanci (Priest) on Feb 24, 2011 at 14:51 UTC

    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);
Re^2: Getting back control..to main script
by shayak (Novice) on Feb 24, 2011 at 14:20 UTC
    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.

        From the exec documentation:

        The exec function executes a system command and never returns.
        And when executing the following script
        exec "dir /s d:\\temp\\"; print "done\n"; exit 0;
        I get the warning
        Statement unlikely to be reached at test6.pl line 2. (Maybe you meant system() when you said exec()?)
        So exec isn't likely the correct answer to "getting back control". ;-)