Perl Daemon Not Working with Sleep()

霸气de小男生 提交于 2019-12-11 02:42:47

问题


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 "test\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?


回答1:


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);
}



回答2:


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.



来源:https://stackoverflow.com/questions/6442661/perl-daemon-not-working-with-sleep

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!