Return-Path: X-Original-To: apmail-perl-modperl-archive@www.apache.org Delivered-To: apmail-perl-modperl-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id A8744DB9F for ; Tue, 19 Jun 2012 19:16:20 +0000 (UTC) Received: (qmail 77679 invoked by uid 500); 19 Jun 2012 19:10:34 -0000 Delivered-To: apmail-perl-modperl-archive@perl.apache.org Received: (qmail 42156 invoked by uid 500); 19 Jun 2012 19:08:37 -0000 Mailing-List: contact modperl-help@perl.apache.org; run by ezmlm Precedence: bulk list-help: list-unsubscribe: List-Post: List-Id: Delivered-To: mailing list modperl@perl.apache.org Received: (qmail 78383 invoked by uid 99); 19 Jun 2012 17:17:53 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 19 Jun 2012 17:17:53 +0000 X-ASF-Spam-Status: No, hits=-0.0 required=5.0 tests=SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (athena.apache.org: local policy) Received: from [65.110.20.35] (HELO mail.inter-corporate.com) (65.110.20.35) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 19 Jun 2012 17:17:48 +0000 Received: from Spooler by mail.inter-corporate.com (Mercury/32 v4.62) ID MO0042EC; 19 Jun 2012 10:01:01 -0700 Received: from spooler by mail.inter-corporate.com (Mercury/32 v4.62); 19 Jun 2012 10:00:38 -0700 Received: from [10.1.1.182] (96.53.47.42) by mail.inter-corporate.com (Mercury/32 v4.62) with ESMTP ID MG0042EB; 19 Jun 2012 10:00:37 -0700 From: "Randolf Richardson" Organization: Inter-Corporate Computer & Network Services To: mod_perl list Date: Tue, 19 Jun 2012 10:17:23 -0700 MIME-Version: 1.0 Subject: Re: external process Reply-to: randolf@modperl.pl Message-ID: <4FE0B423.24052.2D826FF@randolf.modperl.pl> Priority: normal In-reply-to: References: X-mailer: Pegasus Mail for Windows (4.63) Content-type: text/plain; charset=US-ASCII Content-transfer-encoding: 7BIT Content-description: Mail message body X-Virus-Checked: Checked by ClamAV on apache.org > 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/