I'm taking this right out of the "Network Programming With Perl" book. The program refuses to daemonize. Doing nothing it just dies. I know that it does nothing at the moment but this thing should just drop to a daemon and do nothing regardless. No errors either and it doesn't show up on ps -aux.

#!/usr/bin/perl -wT use strict; use POSIX 'setsid'; use IO::File; sub become_daemon { die "Can't fork"unless defined (my $child = fork()); exit 0 if $child; setsid(); open(STDIN, "</dev/null"); open(STDOUT, ">/dev/null"); open(STDERR, ">&STDOUT"); chdir '/'; umask(0); $ENV{PATH} = '/bin:/sbin/usr/bin/:/usr/sbin'; return $$; } sub open_pid_file { my $file = shift; if (-e $file) { # file exists my $fh = IO::File->new($file) || return; my $pid = <$fh>; die "Server already running with PID $pid" if kill 0 => $pid; warn "Removing PID file - unclean shutdown of keeperd?.\n"; die "Can't unlink file $file" unless -w $file && unlink $file; } return IO::File->new($file, O_WRONLY|O_CREAT|O_EXCL,0644) or die "Can't create $file: $!\n"; } use constant PID_FILE => '/var/run/keeperd.pid'; $SIG{TERM} = $SIG{INT} = sub { exit 0; }; my $fh = open_pid_file(PID_FILE); my $pid = become_daemon(); print $fh $pid; close $fh; END { unlink PID_FILE if $pid = $$; }
So why is it just dying?

BMaximus

In reply to Daemon sub refuses to daemonize. Why? by BMaximus

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.