Perl alternatives for asynchronous script execution when forking is disallowed and backticks and system() do not work?

183 Views Asked by At

Update: This has been resolved for system() and backticks by specifying the full path to the external script. I will further investigate the fork issue on my own. Thanks to all for assisting.

At How do I run a Perl script from within a Perl script? one answer offers either of these two methods of calling an external Perl script to run asynchronously:

`myscript.pl &`

system ('myscript.pl &')

I have tried using both of these in my main CGI script, but neither of them will execute the external script, although I can execute the external script successfully from the web browser. The external script is just a test to print a "hello" to the screen and to open an output text file with a timestamp and a hello in its content.

I don't doubt the validity of these suggested methods, but perhaps the virtual web hosting service I use (some sort of Unix on Apache server) is configured not to allow them (?) I have no idea why these don't work for me. If anyone knows, the insight would be appreciated.

Moving on, though, the reason I am looking at these two methods is that the web hosting service told me that I should not use forking, saying that "Forking is a complicated process that requires extensive knowledge, and CGI scripts are not true processes...you are asking for trouble if you fork in your CGI scripts."

In fact, I had already built a CGI script that used fork() and exec() to process multiple external jobs simultaneously. However, I'd also discovered that -- after some random number of forks (e.g. after 15-17 fork iterations in the exact same foreach loop) -- Perl started assigning the parent process ID as a new child process ID (as though it was recycling the main process in mid process?), which caused the main script to exec and thereby kill itself. I was only asking the hosting service how that could possibly be happening. That was their answer, so I figure I'd better not do what they advised against.

Without forking, I'm at a loss as to what I can do if the two above mentioned methods are simply not doing anything.

1

There are 1 best solutions below

2
On

I tested this with the XAMPP Apache distribution on Ubuntu 20.04:

use feature qw(say);
use strict;
use warnings;
use CGI;

my $cgi = CGI->new();
print $cgi->header( -type => 'text/plain' );
my $res = system "date";
say "system('date') returned value: $res";
my $date = qx'date';
say "The date is: $date";
exit;

and it works fine here. The output in the browser is:

Sun Dec 13 09:18:25 CET 2020
system('date') returned value: 0
The date is: Sun Dec 13 09:18:25 CET 2020