Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Re: Daemon possessed perl

by duct_tape (Hermit)
on May 22, 2003 at 15:29 UTC ( #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?

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (6)
As of 2019-09-20 01:52 GMT
Find Nodes?
    Voting Booth?
    The room is dark, and your next move is ...

    Results (253 votes). Check out past polls.