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

Hello ! I have two perl scripts : perlscript1.pl and perlscript2.pl that are supposed to run forever in parallel. Is it possible to call these scripts from a third perl script? i.e.
perlscript3.pl ---------------------------- system('perl -w perlscript1.pl); system('perl -w perlscript2.pl);

Replies are listed 'Best First'.
Re: Running perl scripts in parallel
by kennethk (Abbot) on Jul 23, 2014 at 15:08 UTC
    Sure; simple mock up:
    system('perl -w perlscript1.pl) if !fork; system('perl -w perlscript2.pl) if !fork;
    See fork. Of course, the problem with the above is that I haven't done proper child reaping -- see perlipc. In your case, you may want to just set
    $SIG{CHLD} = 'IGNORE';
    before you invoke the forks. You'll also probably want to swap system to exec, since in the sample case you aren't waiting on a return value.

    There's a lot of additional complexity, but this is the basic concept.


    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

      I have tried this but it still won't let me continue my original script i.e. in pseudo code:
      script1.pl ------------------------- call listener script2.pl #runs forever and outputs stuff call listener script3.pl #runs forever and outputs stuff die "Done script 1";

      I tried your method butb script1.pl still wouldn't die. It is stuck at calling the listeners Thanks

        Are you sure? If I'm understanding your comment, the only scenario where this should be true is if your fork is failing. What happens when you run this code:
        use strict; use warnings; my $pid = fork(); die "Fork failed" if not defined $pid; if ($pid) { #parent print "Parent output\n"; } else { #child exec q|perl -e 'sleep(1); print "Child output\n"'|; } wait; print "Parent finishing\n";

        #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Re: Running perl scripts in parallel
by Anonymous Monk on Jul 23, 2014 at 21:32 UTC
Re: Running perl scripts in parallel
by jellisii2 (Hermit) on Jul 24, 2014 at 11:59 UTC
    Just to stir the pot a little... threads.