Perl: Launch Daemon and Print

I am running a script that runs 24/7. It just repeats 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"; } 

It works great. The problem is that it does not print the last line ("Finished trigger test") until AFTER a 60 second sleep. It prints the current line, runs the command, displays the output of the command, but then waits 60 seconds before printing "completed" and instantly printing the line "running".

So the following process:
sleep 60 seconds
print "trigger trigger"
trigger trigger, output redirection
sleep 60 seconds
printing completed
print works
trigger trigger

But I want this done:
sleep 60
print "trigger trigger"
trigger trigger, output redirection
print completed
sleep 60

I tried moving the sleep command to the end of the loop, did not change the situation. Although this seems very insignificant, it is actually a problem that I have encountered for some time, and it will solve several problems to fix this. I was getting using backticks to run a command and grab the output, and then print the output, but this led to the same behavior.

+4
source share
1 answer

Perhaps the output in the LOG buffered. Try

 select LOG; $| = 1; select STDOUT; 

or

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

after LOG initialized.

+9
source

All Articles