How can I execute an external Windows command and instantly return to Perl?

I tried using system () with fork (), tried exec () and still didn't get what I need.

I want to write a Perl script that runs, say, another Perl script 5 times in a row (sending it different parameter values), but execute it at the same time. I understand that I can turn my script into a .pm file and reference it, but I would prefer to keep the script child element independent of the parent ...

  • system () works, but executes commands sequentially (makes sense on doc)
  • exec () doesn't work - it only executes the first method (makes sense in the document)
  • I added fork () to a child perl script and then tried using system ()
  • that didn't work either.
  • The backtick command command says it works just like system () ...

Is there a simple way in Perl (im using WindowsXP) to execute the process and not care about the return values ​​or antyhing and just continue to the next line of the parent script?

+5
source share
5 answers

On Windows, you can provide a super secret flag to 1 system, IIRC.

system 1, @cmd;

A quick Google search on this subject on perlmonks gives: http://www.perlmonks.org/?node_id=639814

Hope this helps.

+8
source

you can do it like this (fork in parent, exec in child):

for my $cmd qw(command1 command2 command3) {
       exec $cmd unless fork
}

, exec $cmd unless fork, , fork ( ) , exec $cmd , fork false (, ).

, :

my @procs;

for my $cmd qw(cmd1 cmd2 cmd3) {

   open my $handle, '-|', $cmd or die $!;

   push @procs, $handle;
}

@procs, .

cpan, Forks::Super, fork.

+12

.

:

foreach my $cmd (@cmds)
{
    `start $cmd`;
}

Unix:

foreach my $cmd (@cmds)
{
     `$cmd &`;
}
+3

fork , exec . , A - script, B - , 5 :

                A1
fork in A1   -> A1 A2
exec B in A2 -> A1 B2
fork in A1   -> A1 A3 B2
exec B in A3 -> A1 B3 B2

.

+2

The module for this task is redundant. You want to forkcreate a new process and then execrun the command in a new process. To execute the five commands, you need something like:

 for (my $i=0; $i<5; $i++) {
     if (fork() == 0) {
         exec($command[$i]);   # runs command, doesn't return
     }
 }
+2
source

All Articles