http://qs321.pair.com?node_id=412304

cidaris has asked for the wisdom of the Perl Monks concerning the following question:

I am using the following method to fork off and dissociate from the terminal for a script I want to daemonize. Now, I'm told I could just do some while(1) { sleep; } trickery, but I would think there'd be a way to do it without resorting to that. The script should last forever as is, as it uses File::Tail to watch a log. Permissions are OK, as if I remove the sighandler stuff, I can watch it output exactly what I'm looking for.
defined(my $pid = fork) or die "Can't fork: $!\n"; exit if $pid; setsid or die "Can't start a new session: $!\n"; + + $SIG{'HUP'} = $SIG{'INT'} = $SIG{'KILL'} = $SIG{'TERM'} = &sig_handler +; my $connection_log = "/var/log/qmail/qmail-smtpd/current"; if(-e $connection_log){ my $file = File::Tail->new($connection_log); my $line; while (defined($line = $file->read)) { print "Line contents: $line\n"; } } sub sig_handler { my $sig = shift; print "Received SIG $sig\n"; quit(0); }
Now, I would think that it would run forever (as long as the file was moving) and occasionally print the signals it received, except it dies immediately because the fork kills it. Does someone have a tutorial or a link they could present to do what I think I'm trying to do? Alternatively, education as to WIDIAW (why i'm doing it all wrong) would also be appreciated :) Does the file being rotated out kill File::Tail? The documentation suggests that it handles that situation. Update: It's fairly obvious why it's quitting to me (quit) Forget that part, the question still stands, am I daemonizing the wrong way?