Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Background Process

by Aighearach (Initiate)
on Jun 26, 2000 at 01:42 UTC ( [id://19771]=note: print w/replies, xml ) Need Help??


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 .. ! :)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (5)
As of 2025-07-18 10:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.