Currently in my Perl script I make a call like the following:
system(" ./long_program1 & ./long_program2 & ./long_program3 & wait ");
I would like to be able to log when each of the long running commands executes while still executing them asyncronously. I know that the system call causes perl to make a fork, so is something like this possible? Could this be replaced by multiple perl fork() and exec() calls?
Please help me find a better solution.
Two alternatives:
I use
open3
since it it's shorter than a even trivialfork
+exec
and since it doesn't misattributeexec
errors to the command you launch like a trivialfork
+exec
.Yes, definitely. You can fork off a child process for each of the programs to be executed.
You can either do
system()
orexec()
after forking, depending on how much processing you want your Perl code to do after the system call finishes (since exec() is very similar in functionality tosystem(); exit $rc;
)Please note that if you need to do a lot of forks, you are better off controlling them via
Parallel::ForkManager
instead of doing forking by hand.