Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

comment on

( #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,

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

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

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

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

    How do I use this? | Other CB clients
    Other Users?
    Others making s'mores by the fire in the courtyard of the Monastery: (7)
    As of 2021-04-23 18:05 GMT
    Find Nodes?
      Voting Booth?

      No recent polls found