Perl 守护进程不与 Sleep() 一起工作

发布于 2024-11-16 13:46:15 字数 807 浏览 5 评论 0原文

我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

泪是无色的血 2024-11-23 13:46:15

Perl 不会自动刷新缓冲区,因此您对文件的写入只会在相当长的一段时间后发生。当您没有睡眠时,由于写入的数据量很大,您的缓冲区几乎会自动刷新。

试试这个:

$| = 1;
while (1) {
    print "test\n";
    sleep(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);
}
っ〆星空下的拥抱 2024-11-23 13:46:15

输出缓冲不够耐心?在循环中使用 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.

一片旧的回忆 2024-11-23 13:46:15

您还可以使用:

刷新控制台上的打印

select()->flush();

使用 fileHandle $FH 刷新到文件

$FH->autoflush;

you can also use:

to flush the print on console:

select()->flush();

to flush in to the file with the fileHandle $FH

$FH->autoflush;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文