What you'll actually want to do is something more like:
my $pid = fork;
die "Error forking: $!" unless defined $pid;
if (!$pid) {
my $pid = fork;
die "Error forking: $!" unless defined $pid;
if (!$pid) {
exec("/path/to/foo");
}
else {
CORE::exit;
}
}
There are two important things in that:
- You can't just say system("foo &"); to background foo. The problem is that a shell is invoked to execute "foo &", and, even though foo, itself, gets backgrounded (by that shell process), the shell process is waiting on the backgrounded job, and your perl process's call to system() is, in turn, waiting on the shell process... so you're really not accomplishing what you want (which is that foo can run entirely disconnected from your perl process).
- I'm forking twice before launching foo. The reason for two forks is so that you can get a process that is entirely disconnected from the perl process. That is, the "foo" process won't appear as a child of the perl process, and won't send $SIG{CHLD}s back to the parent, or various other things that would otherwise be problematic (particularly in mod_perl). This double-fork with the middle-process exiting is a common idiom for completely disconnecting a child process (by making it not a child, but a grandchild, and killing the immediate child), and you can find mention of it in docs if you search for "child of init".
Anyway, hope that helps.
------------
:Wq
Not an editor command: Wq