Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Daemon possessed perl

by duct_tape (Hermit)
on May 22, 2003 at 15:29 UTC ( [id://260109]=note: print w/replies, xml ) Need Help??


in reply to Daemon possessed perl

A quick and dirty way to have it detach and stay in the background is to fork(), and then have the parent exit. I have used this a few times in the past for some simple programs and haven't had any issues.

Here's a short example:

# do startup/initialization code ... if (my $pid = fork()) { print "Sending to background...\n"; exit(0); } ... # main application code here
Proc::Daemon pretty much does the same as this, but re-opens STDIN,STDOUT,STDERR and a few other things. You might want to check the source for that module, if you're interested. Also it's probably best to use that module if this is going to be production code. I just thought I'd show the non-module way (for learning purposes).

Replies are listed 'Best First'.
Re: Re: Daemon possessed perl
by mr_mischief (Monsignor) on May 23, 2003 at 01:48 UTC
    I ++ed your node, but please note that on some systems you'd have to fork() and exit() twice to completely dissociate from the original process's resources. fork(), exit(), and POSIX::setsid() is even better.

    Proc::Daemon has its good points, but why would I want it to reopen three file handles if I'm going to turn around and close them anyway? That doesn't seem like a very useful thing to do in the generic case. Still, for production code it wouldn't hurt to use it just for the fact that someone else has already debugged it.

    Christopher E. Stith
    use coffee;
Re: Re: Daemon possessed perl
by Anonymous Monk on May 22, 2003 at 15:58 UTC
    On Linux, you could just be a slacker and use chkconfig. This buys you process control and integration into WebMin, RedHat's Services Tab, etc, etc. Might as well use the OS tools when they are decent! --Attila (who is not logged in)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2024-04-24 10:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found