perl-modperl mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Randolf Richardson" <rand...@modperl.pl>
Subject Re: external process
Date Tue, 19 Jun 2012 17:17:23 GMT
> I'm having trouble with running an external program.  It seems to work the
> first few times I do it, then it gets stuck and I have to restart httpd
> with kill -9.  I've tried things a few different ways.  Here is what I'm
> currently doing:
> 
> my  $pm = new Parallel::ForkManager(6);
> 
> 
> for my $thing_to_do(@things) {
>     # Forks and returns the pid for the child:
>     my $pid = $pm->start and next;
>     my $command=q{/public_html/perl/detach.pl};
>      $command . ' ' . $thing_to_do;
> 
>     `$command`;
>      ### I've also tried: $r->spawn_proc_prog ($fc); ###
> 
>      $pm->finish; # Terminates the child process
> }
> $pm->wait_all_children;
> 
> 
> I'm sure this is "simple".  Where have I gone wrong?

	The sample code on this web page helped me:

		http://perl.apache.org/docs/2.0/api/Apache2/SubProcess.html

	I'm not sure what "$fc" is, but if it is the full command with 
arguments, then that will not work for arguments needs to be 
specified separately:

# pass @ARGV but don't ask for any communication channels
$command = "/tmp/argv.pl";
@argv = qw(foo bar);
$r->spawn_proc_prog($command, \@argv);

	The above example comes from the above page, and it will run the 
process as detached.  In some environments the command needs to be 
the perl binary itself (such as Microsoft's Widows) with the script 
being one of its arguments.

	Also note that if you shut down Apache HTTPd that your spawned 
process will also be terminated, but this is trivially resolved by 
making your process truly detached with the following two lines added 
appropriately in your "detached" script (I use this in production and 
find it works very well):

use POSIX 'setsid';
setsid();

	I'm assuming the "detached" requirement because of the name you 
chose for your script.  If you actually need to communicate with your 
script (e.g., receive its output, etc.) then detaching from it may 
not be exactly what you need, and in which case the web page I 
referenced above explains how to do that with the other examples 
under the "Synopsis" heading.

	I hope this helps.

Randolf Richardson - randolf@inter-corporate.com
Inter-Corporate Computer & Network Services, Inc.
Beautiful British Columbia, Canada
http://www.inter-corporate.com/



Mime
View raw message