You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by "Reilly, Thomas" <Th...@Dscie.Com> on 2000/06/08 10:39:16 UTC

system &

Hi,
	I'm having trouble spawning a sub-process from mod_perl. I've read
the guide and it recommends spawning, handing the info the process needs and
have it detach (close STDIN,STDOUT and STDERR + execute setsid()). I'm not
sure how to go about this correctly. If any one can give me a few lines of
code I would appreciate it.
I've successfully spawned the server using the system command from a
standalone perl program but the system command always fails in my mod_perl
script.
The command i use is:

system("./cserver &") or die "system cserver failed: $?";

what i get in the error log is:

null:system cserver failed: 0 at /home/httpd/perl/test.pl line 35

the permissions on the executable cserver located in /home/httpd/perl/ is
-rwxr-xr-x (executable by everyone!)

The reason i put it in the backround is to allow the main mod_perl script to
continue and connect to the cserver process via unix domain sockets (after a
small delay)
and exchange the required info (an XML string but thats irrelevant). The
cserver process will login to a database, retrieve info and the plan is to
send it back to the mod_perl process through the socket and all the way back
to the client. (This is asking for trouble i'm sure! overhead, denial of
service attacks etc..)
Can anyone tell me why the above system & command doesn't work from mod_perl
(it is succesfull in starting the cserver in a normal perl script)
and can anyone also tell me if i'm completely insane in the way i'm going
about this!

Apache/1.3.12 (Red Hat linux 6.2)
mod_perl/1.21
Perl 5.00503

Any help would be greatly appreciated!

Thomas Reilly
Software Consultant,

Distributed Software Consultancy Ltd.,
Ballybrit Business Park,Galway,Ireland.
Tel: +353 (91) 760541 Fax: +353 (91) 760542
e-mail: Thomas.Reilly@dscie.com
web: http://www.dscie.com



Re: system &

Posted by Barrie Slaymaker <ba...@slaysys.com>.
"Reilly, Thomas" wrote:
> If any one can give me a few lines of code I would appreciate it.

One way is:

use IPC::Run qw( run close_terminal ) ;

   run(
      sub {
         # ... your code here ...
         sleep 15 ;
      },
      init => sub {
         close_terminal ;
         exit if fork ;
      }
   ) or die "Run failed $!" ;

Note that this just fork()ed twice, once in run() then again in the init
sub.  Also note that run() has unsuprising result code semantics,
unlike system().

You can also run subcommands as daemons this way, FWIW:

   @cmd = qw( foo 1 2 3 ) ;
   run \@cmd, init => sub { ... } ;

and you can open pipes to the new daemon, too:

   $in = "stuff to send"
   run(
      sub { ...yer code...},
      init => sub { .... }, '<', $to_daemon, '>', \$from_daemon 
   ) ;
   ## now $out == "stuff to recv" ;


   run(
      sub { ...yer code...},
      init => sub { .... }, '<pipe', \*TO, '>pipe', \*FROM_DAEMON
   ) ;
   write TO_DAEMON, ... ;
   read FROM_DAEMON, ... ;


Note: IPC::Run is in beta and under active development and debugging.

Here's the latest version, I just tweaked it to expose close_terminal() for
you:

   http://slaysys.com/src/IPC-Run-0.35.tar.gz

If people would prefer something like "detach" or even "daemonize" instead,
can-do.

- Barrie

> system("./cserver &") or die "system cserver failed: $?";

This is a common misunderstanding: system() returns whatever the process
returned, not system()'s success or failure.  Also, you're not closing your
terminal or changing the session ID (group leader).

> The reason i put it in the backround is to allow the main mod_perl script to
> continue and connect to the cserver process via unix domain sockets (after a
> small delay)

run()'ll let you connect via pipes or ptys opened by the parent, if you like.

> and exchange the required info (an XML string but thats irrelevant). The
> cserver process will login to a database, retrieve info and the plan is to
> send it back to the mod_perl process through the socket and all the way back
> to the client. (This is asking for trouble i'm sure! overhead, denial of
> service attacks etc..)

Why spawn a subprocess?  Memory usage?  Will the parent process be doing
something (ie coprocessing) in the meanwhile?

Also, why pass the XML to the daemon through a pipe?  If you prebuild it,
then fork(), the daemon'll have a copy.  Any why use XML as an intermediary
language (unless some external consideration makes it necessary), since
building the XML and parsing it consumes time and memory.

- Barrie

RE: system &

Posted by Kees Vonk 7249 24549 <KE...@BGTRANSCO.CO.UK>.
> The command i use is:
> 
> system("./cserver &") or die "system cserver failed: $?";
> 
> what i get in the error log is:
> 
> null:system cserver failed: 0 at /home/httpd/perl/test.pl line 35

Are sure that cserver fails? I have come across several unix programs that 
return 0 on success. You can always do:

system("./cserver >mylogfile 2>&1 &")

to see what cserver outputs on STDOUT and STDERR. Also make sure that your 
working directory is what you expect it to be. (Try for instance Cwd.pm to 
check that.)


Hope this helps,


Kees Vonk