Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

TStanley++ suggests Proc::Daemon, which is likely to be just what you want.

It's a Good Thing to go through the details of exactly what to do, and why, to run as a daemon. A daemon process is one which runs forever without a controlling terminal or parent process. There are some housekeeping issues to be taken care of in launching a daemon. Stevens' Advanced Programming in the UNIX Environment provides a a list of coding rules and a C function for that which is readily translated to perl.

We'll need the POSIX system call setsid() and we'll also import Cwd's chdir to get $ENV{PWD} reset when we change directory. We define a function daemon_init() which does all the daemonic housekeeping.

use POSIX 'setsid'; use Cwd 'chdir'; sub daemon_init {
First, we fork and exit the parent. That makes the launching process think we're done and also makes sure we're not a process group leader - a necessary condition for the next step to succeed.
my $pid = fork; exit 0 if $pid; exit 1 if not defined $pid;
Second, we call setsid() which does three things. We become leader of a new session, group leader of a new process group, and become detached from any terminal. To satisfy SVR4, we do the fork dance again to shuck session leadership, which guarantees we never get a controlling terminal.
setsid(); $pid = fork; exit 0 if $pid; exit 1 if not defined $pid;
Third, we change directory to the root of all. That is a courtesy to the system, which without it would be prevented from unmounting the filesystem we started in.     chdir '/' or die $!; Fourth, we clear the permissions mask for file creation. That frees our daemon to manage its files as it sees fit.     umask 0; Finally, we close unwanted filehandles we have inherited from the parent process. That is another courtesy to the system, as well as being sparing of our own process resources. I'll take them from arguments passed to daemon_init(), just to be different from Proc::Daemon.
close $_ for @_; }
The only reason to do this by hand, as written, instead of through Proc::Daemon is if you do have file handles already open which the daemon will use. Proc::Daemon closes 'em all.

It is conventional to rewire the SIGHUP handler of a daemon to reinitialize and reload configuration files. If you set $SIG{HUP} = \&_init; for some suitably defined _init() before you fork, the initialization will be done automatically. You can repeat initialization at any time ( like after editing configuration files) with kill HUP, pid.

After Compline,
Zaxo


In reply to Re: Creating a perl daemon by Zaxo
in thread Creating a perl daemon by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (4)
As of 2024-04-26 00:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found