Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Keeping grandchild alive when mod_perl process is killed

by etcshadow (Priest)
on Nov 20, 2004 at 22:36 UTC ( #409292=note: print w/replies, xml ) Need Help??


in reply to Keeping grandchild alive when mod_perl process is killed

So, for the record, after applying Zaxo's help, this is what you get:
use Apache::SubProcess; my $pid = fork; if (defined $pid && !$pid) { Apache->request->cleanup_for_exec(); $pid = fork; if (defined $pid && !$pid) { close STDERR; close STDIN; close STDOUT; use POSIX(); POSIX::setsid(); ########## # do long running stuff here ########## CORE::exit; } else { CORE::exit; } } waitpid($pid, 0);
Although before anyone else plows into this, you should probably look up some info on daemonization to understand it. Also, you'll almost certainly want to reopen STDOUT and STDERR to some log files or something. In short, this is a very small piece of the whole puzzle, so please don't take that snippet of code out of context and think that you've got an entire solution to the problem of long-running processes spawned by a web server.

Oh, and if your fork fails (returns undef), you should really trap that and deal with it appropriately... I was just posting the miminal amount of code to express my question. I don't mean for this to be really exemplar code.

------------ :Wq Not an editor command: Wq

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://409292]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (8)
As of 2019-05-22 11:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you enjoy 3D movies?



    Results (139 votes). Check out past polls.

    Notices?
    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!