Reputation: 42822
I write some testing code before using Proc::Daemon
, what my testing code is like:
#! /usr/bin/perl
use strict;
use warnings;
use Proc::Daemon;
Proc::Daemon::Init();
my $continue = 1;
$SIG{TERM} = sub { $continue = 0 ; }
while ( $continue ) {
sleep(5) ;
&greeting ;
}
sub greeting {
open ( FH, ">>/home/daogu/foo" ) or die "can't open it" ;
print FH "hello word\n" ;
close FH ;
}
After I start up the toy daemon, I found nothing actually has been written to "foo
". could anybody explains why does this happen? thanks.
Upvotes: 4
Views: 1221
Reputation: 19705
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.
Upvotes: 7
Reputation: 32335
Your script looks fine to me. Most likely the problem is just file permissions - make sure that you have write access to the directory where the file is supposed to be created, or if the file already exists that you have write access to the file itself.
I run your script on my machine and it works fine.
Upvotes: 3