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

I have a program that runs for 9 hours and around the 4 hour point I need to run a second program. Now I use system() but I don't want to wait for the child process to finish before continuing with the parent process. I've also tried exec() but the parent process just dies.

I've been searching for about 1/2 hour and haven't found an answer I can understand

I saw that system(1, "prog") on a windows system will work but I can't get it to run in Linux.

These programs are different so I don't see using fork() as something viable.

Am I misusing system( 1, "/program"); or is there another way to do this? perldoc on my system didn't come up with anything about the "1" option that I saw mentioned with Windows systems.

Update: I just realized that using system("/program &") runs the program in the background and doesn't make the parent program wait for the child to finish.

Replies are listed 'Best First'.
Re: Asynchronous Program Spawning
by ikegami (Patriarch) on Feb 12, 2006 at 09:16 UTC

    All of the following will do the trick:

    • IPC::Run
    • IPC::Open3 (Core module written to address this issue.)
    • IPC::Open2 (Core module written to address this issue.)
    • fork+exec (Inefficient in Windows)
    • system 1, ... (Only on Windows)
    • system "start ..." (Only on Windows. Has security issues from loading shell)
    • system "... &" (Only on unix. Has security issues from loading shell)
      Has security issues from loading shell

      Could you explain, or provide a pointer to discussion of the security issues?


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

        Due care must be spent on sanitizing and escaping the arguments if they are not hard coded.

        $file_name = 'file name'; system("program $file_name &"); # Oops!

        Without due care, the code may not work, or worse, it could be vulnerable to injection attacks.

Re: Asynchronous Program Spawning
by graff (Chancellor) on Feb 12, 2006 at 06:49 UTC
    Here is what saintmike meant when he said this is what "fork" is for:
    ... # time to start a separate process, but we want the parent to keep run +ning # (i.e. parent and child run simultaneously) my $childpid = fork(); if ( !defined( $childpid )) { die "couldn't fork: $!"; } elsif ( $childpid == 0 ) { # this is what the child does: exec( "other_program", @args ); # at this point, the other_program is running; it replaces # (has the same pid that was originally assigned to) the # "child copy" of the current script } # and here, the parent continues what it was doing...

    But as you have seen, it also works to simply include the ampersand at the end of the command line passed to "system()".

      a call to exit() should be included after the exec() so that in case the exec fails, the child doesn't continue running parent code.
Re: Asynchronous Program Spawning
by saintmike (Vicar) on Feb 12, 2006 at 03:16 UTC
    These programs are different so I don't see using fork() as something viable.
    Why not? fork is what I would recommend.
Re: Asynchronous Program Spawning
by smokemachine (Hermit) on Feb 12, 2006 at 06:00 UTC
    or you can use system("your_program &"); with "&" to run the program in background... in linux system...