tags:

views:

307

answers:

2

Hi guys,

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.

+2  A: 

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.

Guss
Really? Even with the missing semi-colon on line 9? My experiment gave this: `syntax error at daemon line 11, near ") {"` and on and on.
Telemachus
Yea, obviously I fixed that - I just didn't think its important to note as the compiler will immediately alert you to the problem.
Guss
Fair enough, and I didn't downvote you for it. However, you say "Your script looks fine to me" which was odd to me as his program doesn't compile. Perl tells you "I wet the bed", but it doesn't say exactly where. (It's easy to find, but the compiler doesn't literally say, "Hey you need a semi-colon at the end of that anonymous subroutine on line 9.") For me the bigger issue is that leaving off a semi-colon for anonymous subs is a pretty common gotcha in Perl, so I think it's worth mentioning. The problem the compiler hints at is only easy to find if you *know* need a semi-colon there.
Telemachus
All of that said, your diagnosis seems right to me, and my answer is really just yours with some added help on how the OP might confirm this for himself.
Telemachus
My perl 5.8.8 said "syntax error at line 11" which, as line 11 has no errors, clearly indicates a missing semi-color on the previous code line. PHP has the same behavior - a missing semi-color is reported as an error on the following line.Sorry for not putting more effort into the answer - I was on working hours.
Guss
+5  A: 

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.

Telemachus
I always find strace(1) to be a great help in tracking down these situations without altering the original code.
Dominic Mitchell
@Dominic: although "without altering the original code" sounds very tempting to me, I can't lie to you. I couldn't decipher the output of `strace` (or `dtrace` and `dtruss` for anyone else who goes looking for it on a Mac) to save my life. I should really learn how.
Telemachus