in reply to launching external program with SYSTEM causes script to hang until program closed

Instead of doing system(), you could do "fork()" and have the child do "exec()". The parent process will continue to do whatever it's doing, because "fork()" returns immediately.

See perlipc for details, and the perlfunc section on fork for a brief description. A simple example:

my $child_pid = fork(); if ( ! defined( $child_pid )) { warn "fork failed\n"; } elsif ( $child_pid == 0 ) { # true for the child process exec( $cmd, @args ); } # the parent gets here when the fork succeeds...
  • Comment on Re: launching external program with SYSTEM causes script to hang until program closed
  • Download Code

Replies are listed 'Best First'.
Re^2: launching external program with SYSTEM causes script to hang until program closed
by erroneousBollock (Curate) on Sep 27, 2007 at 03:19 UTC
    That's great for UNIX-y systems.

    fork() uses threads.pm on Windows, so (in some situations) you may wish to use Win32::Process::Create() instead.

    -David

      Even easier on Win32 is system( 1, $cmd ). It is unfortunate that this easy way to "spawn" (which the Perl C code knows how to do portably) was never exposed for Perl scripts to do portably.

      - tye        

Re^2: launching external program with SYSTEM causes script to hang until program closed
by perlofwisdom (Pilgrim) on Sep 27, 2007 at 15:13 UTC
    I have had good success with fork on Windows (perl release 5.6+). However, you can also use a piped open command which will not wait for the output. If the trailing character is a pipe symbol, "open" launches a new process with a read-only filehandle connected to it. For example:
    open(NET, "netstat -i -n |") or die "can't fork: $!"; # You could go off and do other things here for a bit, # and then come back to check on your results. while (<NET>) { # your code here... } close(NET) or die "can't close netstat: $!/$?";
    Please note that when you explictly close the piped filehandle, it will cause the parent process to wait for the child to finish (and I recommend closing it).