getting results of multiple simultaneous system commands

D

David Resnick

I need to execute multiple simultaneous system() statements. I
originally did this using perl threads each invoking a system cmd,
waiting for them to be joinable, then harvesting the return status
with join(). The system commands are well behaved in that they will
finish eventually (have built in time limits). Unfortunately, I
discovered that the perl I need to use on some of our systems was
built without threads and replacing it isn't an option.

Is there a nice way to do this AND get the return values of the
processes launched in single threaded perl? Repeated system("$cmd &")
would execute all the processes, but I would need some way to get the
return status. Fork and exec would also work, but same issue. I
could run a wrapper that calls system and writes the result to a file
I guess, but that feels clunky. Any suggestions that seem nicer?
 
D

Dr.Ruud

David said:
I need to execute multiple simultaneous system() statements.

push @exit, system("$_ &") for @command;

[...] Unfortunately, I
discovered that the perl I need to use on some of our systems was
built without threads and replacing it isn't an option.

Consider it a blessing. Just fork and wait.
 
D

David Resnick

David said:
I need to execute multiple simultaneous system() statements.

push @exit, system("$_ &") for @command;
[...]  Unfortunately, I
discovered that the perl I need to use on some of our systems was
built without threads and replacing it isn't an option.

Consider it a blessing. Just fork and wait.
Forking twice and waiting for both to exit (and checking $?, which was
the thing I forgot about) seems to work nicely. Thanks very much for
your help.
 
C

C.DeRykus

push @exit, system("$_ &") for @command;
[...]  Unfortunately, I
discovered that the perl I need to use on some of our systems was
built without threads and replacing it isn't an option.
Consider it a blessing. Just fork and wait.
Forking twice and waiting for both to exit (and checking $?, which was
the thing I forgot about) seems to work nicely.  Thanks very much for
your help.

IPC::Run can process in the background, eg,
(Also options for IO, timeout, etc)

use IPC::Run qw/start/;

$ref1 = sub{ system "/path/to/foo1";
print "foo1: $?"; };
$ref2 = sub { system "/path/to/foo2";
print "foo2: $?"; };

my $h = start $ref1, '&', $ref2
or die "finish: $?";
 
C

C.DeRykus

...
IPC::Run can process in the background, eg,
(Also options for  IO, timeout, etc)

use IPC::Run qw/start/;

$ref1 = sub{ system "/path/to/foo1";
              print "foo1: $?"; };
$ref2 = sub { system "/path/to/foo2";
              print "foo2: $?"; };

my $h = start $ref1, '&', $ref2
     or die "finish: $?";

last line should be replaced with
these below:

my $h = start $ref1, '&', $ref2;
finish $h or die "finish: $?";
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top