in reply to Background Process

print $reply_to_user; die "cannot fork: $!" unless defined ( my $pid = fork() ); if ( $pid ) { #this is the parent. exit; } else { #this is the child open STDOUT, '>/dev/null' or die "Can't write to /dev/null: $!"; sleep( 10 ); #do stuff }

 

update: thanks, Randal. ( btw, you don't have to plug your column each post, you're already famous. ;)

Additional update:Intrepid: I left STDERR alone because it should be connected to the web server error log, which is probably where you want the errors from a CGI.

ncw: Well, I am certainly a lover of doing it the easy way! :) Thanks for that, I've been using a home rolled Daemon module, but I see that I don't need to. But, is this a daemon? Proc::Daemon seems to close STDERR, so then we have to reopen it somehow to get the errors back into the webserver logs where we need them.

Paris Sinclair    |    4a75737420416e6f74686572
pariss@efn.org    |    205065726c204861636b6572
I wear my Geek Code on my finger.

Replies are listed 'Best First'.
RE: Re: Background Process
by merlyn (Sage) on Jun 26, 2000 at 02:42 UTC
      Or if you are feeling lazy you can do all the above forking, closing, reopening etc with :-
      use Proc::Daemon; #... Proc::Daemon::Init();
        Could you provide a sample script? Thanks, David
RE: Re: Background Process
by Intrepid (Curate) on Jun 26, 2000 at 09:22 UTC
    make it more portable this way:
    use File::Spec; my $devnul = File::Spec->devnull(); print $reply_to_user; die "cannot fork: $!" unless defined ( my $pid = fork() ); if ( $pid ) { #this is the parent. exit; } else { #this is the child open STDOUT, ">$devnul" or die "Can't write to nowhere: $!"; open STDERR, ">$devnul" or die "Can't write to nowhere: $!"; sleep( 10 ); #do stuff }
    This is a suggestion I have not tried, but BTW on WinNT the string value in $devnul is 'nul'.

    Good Meditations,
    soren andersen (Intrepid)

    Q: What is the sound of several hundred Perler-dogs chasing cool code?
    A: YAPC - YAPC - YAPC .. ! :)