Perl 守护进程不与 Sleep() 一起工作
我使用 Proc::Daemon < 编写了一个简单的测试守护进程/a>.这是守护进程:
#!/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();
}
}
这是它执行的测试:
#!/usr/local/bin/perl
while (1) {
print "test\n";
sleep(1);
}
while 循环仅使用 print 语句就可以正常工作,但是当我添加 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 "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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
更多
发布评论
评论(3)
Perl 不会自动刷新缓冲区,因此您对文件的写入只会在相当长的一段时间后发生。当您没有睡眠时,由于写入的数据量很大,您的缓冲区几乎会自动刷新。
试试这个:
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:
输出缓冲不够耐心?在循环中使用
sleep(1)
时,可能需要一段时间才能积累足够的数据并将其刷新到日志文件中。将
$|=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.您还可以使用:
刷新控制台上的打印:
使用 fileHandle $FH 刷新到文件
you can also use:
to flush the print on console:
to flush in to the file with the fileHandle $FH