First, you need a semi-colon to terminate the assignment on line 9:
$SIG{TERM} = sub { $continue = 0 ; };
Until I added that, your program wouldn't even run, so I'm guessing you have that in the script and missed it here.
Beyond that, I suspect that Guss is right, and you have permission problems. Here's a way to check. Open a separate terminal and run top
. Start the daemon script, and you will see a Perl process show up. If the problem is permissions, you will quickly see the Perl process disappear. The script dies in the subroutine, but your attempt at a helpful error message never shows up since the daemon has no access to your terminal at that point.
A quick way to vary this test is change die
to warn
in the subroutine. If you do so, the daemon will continue to run forever (check the terminal running top
to confirm this). But if the problem is permissions, you still won't see a file get created or written to.
Edit: Yup, permissions trouble + no access to STDERR = a dead, silent daemon. Try this version, but make sure that you can write to the log you switch in for STDERR:
Proc::Daemon::Init();
my $continue = 1;
$SIG{TERM} = sub { $continue = 0 ; };
while ( $continue ) {
sleep(5);
greeting();
}
sub greeting {
open STDERR, '>>', '/Users/telemachus/log'
or die "Can't reopen STDERR to log: $!";
open my $fh, '>>', '/usr/local/foo'
or warn "Can't open /usr/local/foo for writing: $!";
print $fh "hello word\n";
close $fh;
}
You are likely to see a whole lot of this in your log:
Can't open foo for writing: Permission denied at daemon line 21.
print() on closed filehandle $fh at daemon line 22.