Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Multiple system commands in parallel

by Tanktalus (Canon)
on May 09, 2016 at 20:07 UTC ( [id://1162571]=note: print w/replies, xml ) Need Help??


in reply to Multiple system commands in parallel

Since you're on windows, if you use system(1,$cmd) I think that'll spawn it off.

On unix, you'd have to fork and exec instead.

You just have to wait for them to finish if you're going to read files they create.

Personally, I use AnyEvent::Util::run_cmd to spawn subprocesses, monitor their outputs and return code, and manage all of that. But that's a lot more than you're asking for.

Replies are listed 'Best First'.
Re^2: Multiple system commands in parallel
by g_speran (Scribe) on May 09, 2016 at 20:25 UTC

    Tanktalus....Thank you...this has gotten me one step closer.

    Unfortunately, I noticed a small issue. In the event there are 15 files in the directory that need to be process and the script has currently launched, say 8. If the user <ctrl><c> out of the script prior to the script exiting on its own, the 8 that the script initially launch are killed when the user <ctrl><c> out of the script.

    Any ideas on how to rectify this?

      I'm not 100% sure about what's required on Windows. What may work is to set $SIG{INT} to "IGNORE" before spawning the children. Note that when you do so, your process won't be killed by ctrl-C, either - you'll just keep going.

      Another option may be to fork/exec yourself instead of using system, and in the "child" process, do the above - that way the parent process will still be killable partway through while the children will continue to live on their own, e.g.:

      for ($x=0; $x < scalar(@files); $x++ ) { $file=@files[$x]; chomp $file; $cmd="clone -F $file "; print "Launching Command is: $cmd\n"; my $pid = fork; if ($pid) { # parent } elsif (defined $pid) { $SIG{INT} = 'IGNORE'; exec($cmd); } else { die "fork failed."; } }
      This variation you can still hit Ctrl-C to kill the parent, but the children will inherit the ignore status on the signal handler and keep going. (This can also be done in the on_prepare callback in AnyEvent::Util::run_cmd.)

      Finally, if you have control over the clone command, you can have that program ignore the INT signal.

      I ran "help start" on Win XP. I presume that there are more options on other Window's O/S's. The "/B" option appears to do what you want? (ignore CTL-C?)
        The "/B" option appears to do what you want? (ignore CTL-C?)

        No. It doesn't:

        C:\test>perl -e" system 'start /b c:/perl64/bin/perl.exe -E\"$^|=1; sa +y ++$i while sleep 1\"'; sleep 1000" 1 2 3 4 5 Terminating on signal SIGINT(2) Terminating on signal SIGINT(2)

        That's both processes aborting.

        Contrast:

        C:\test>perl -e" system 'start /min c:/perl64/bin/perl.exe -E\"$^|=1; +say ++$i while sleep 1\"'; sleep 1000" Terminating on signal SIGINT(2)

        The initial process terminates, the second process continues to run, outputting to its own window.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
        In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1162571]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-03-29 12:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found