Perl:运行“守护程序"和印刷 [英] Perl: Running a "Daemon" and printing

查看:41
本文介绍了Perl:运行“守护程序"和印刷的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行一个24/7运行的脚本.它只是一遍又一遍地循环,非常简单:

I am running a script that runs 24/7. It just loops through over and over, very simple:

while ($daemon) {
    sleep 60; 
    chomp(my $currentDate = `date +%c`);
    print LOG "-- Running trigger: $currentDate --\n";
        system("$triggerCmd >> $daemonLog");
        print LOG "-- Completed trigger test. --\n\n";
}

工作正常.问题在于,直到60秒钟的睡眠之后,它才打印最后一行(完成的触发测试").它会打印运行行,运行命令,打印命令输出,但是要等待60秒钟,然后再打印完成"并立即再次打印运行"行.

It works fine. The problem is that it doesn't print the last line ("Completed trigger test") until AFTER the 60 second sleep. It prints the running line, runs the command, prints command output, but then waits 60 seconds before printing "completed" and instantly printing "running" line again.

因此,其遵循的过程是这样的:
睡眠60秒
打印运行触发器"
运行触发器,重定向输出
睡眠60秒
打印完成
打印运行
运行触发器

So the process it's following is this:
sleep 60 seconds
print "running trigger"
run trigger, redirect output
sleep 60 seconds
print completed
print running
run trigger

但是我希望它这样做:
睡觉60
打印运行触发器"
运行触发器,重定向输出
打印完成"
睡觉60

But I want it to do this:
sleep 60
print "running trigger"
run trigger, redirect output
print "completed"
sleep 60

我尝试将sleep命令移动到循环的末尾,没有任何区别.尽管这似乎很小,但这实际上是我已经解决了一段时间的问题,它将解决一些问题以对其进行修复.我确实是使用反引号来运行命令并捕获输出,然后打印输出,但这导致了相同的行为.

I've tried moving the sleep command to the end of the loop, didn't make a difference. While this seems very minor, it's actually a problem I've been dealing with for a while and it would solve a few issues to fix it. I was rececntly using backticks to run the command and capture the output, then print output, but this resulted in the same behavior.

推荐答案

可能正在缓冲LOG的输出.试试

Possibly the output to LOG is being buffered. Try

select LOG;
$| = 1;
select STDOUT;

use IO::Handle;
(*LOG)->autoflush(1);

在初始化LOG之后.

这篇关于Perl:运行“守护程序"和印刷的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆