Here is the basis for a general daemon. Add code at the end to do what you want. This follows the Stevens rules.

#!/usr/bin/perl -w use strict; use POSIX qw( setsid ); my $debug = 1; my $logfile = q(.testlog); # Season to taste my @fh_unused = (\*STDIN, \*STDOUT); open \*STDERR, ">> $ENV{'HOME'}/$logfile"; select((select(\*STDERR), $| = 1)[0]); { # Daemon Rule 1) Fork and exit the parent. my $ppid = $$; my $pid = fork and exit 0; ! defined $pid and die "No Fork: ", $!; while (kill 0, $ppid) { select undef, undef, undef, .001; }; } # Daemon Rule 2) become session leader, pg leader, no term my $session_id = POSIX::setsid(); # Daemon Rule 3) cd to / chdir '/' or die "Could not cd to rootfs", $!; # Daemon Rule 4) set file creation mask to 0 my $oldmask = umask 00; # Daemon Rule 5) Close unneeded file handles close $_ or die $! for @fh_unused; # Your code here exit 0;
If you open communication channels before the fork, the child will inherit their handles.

After Compline,
Zaxo


In reply to Re: Daemons??? by Zaxo
in thread Daemons??? by toadi

Title:
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, details, 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, summary, 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.