"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