Perl Daemon Not Working with Sleep()
I wrote a simple test daemon using Proc::Daemon . Here is the daemon:
#!/usr/bin/perl
use Proc::Daemon;
$daemon = Proc::Daemon->new(
work_dir => '/scripts/',
child_STDOUT => '/scripts/child.log',
child_STDERR => '+>>debugchild.txt',
pid_file => 'pid.txt',
exec_command => 'perl /scripts/test.pl'
);
foreach(@ARGV)
{
if ( /install/i )
{
$Kid_1_PID = $daemon->Init;
}
elsif (/remove/i)
{
$stopped = $daemon->Kill_Daemon();
}
}
And here is test that it executes:
#!/usr/local/bin/perl
while (1) {
print "t开发者_运维知识库est\n";
sleep(1);
}
The while loop works fine with just the print statement, but when I add sleep(); the log is empty. Why would this be?
Perl will not automatically flush the buffer so your write to the file will only happen after quite a while. When you don't have the sleep your buffer will be almost automatically flushing due to the volume of data being written.
Try this:
$| = 1;
while (1) {
print "test\n";
sleep(1);
}
Output buffering and not enough patience? With a sleep(1)
in the loop, it could take a while for enough data to accumulate and be flushed to the log file.
Put a $|=1;
statement at the top of your script.
you can also use:
to flush the print on console:
select()->flush();
to flush in to the file with the fileHandle $FH
$FH->autoflush;
精彩评论