I have this perl script:
#!/bin/perl
use strict;
use warnings;
use Time::HiRes qw(usleep);
my $fd;
sub reopen {
open $fd, ">>", "file.log" or die $!;
}
$SIG{USR1} = \&reopen;
reopen();
while(1){
syswrite $fd, time . " ($$): message\n";
usleep(100000);
}
I reopen file handle in the signal handler. Is it safe?
Yes[1]. Since 5.8.1, the actual signal handler simply sets a flag saying a signal was received[2]. That flag is checked between statements. System calls (e.g. usleep) return with error $!{EINTR} when interrupted by a signal, allowing the flag to be checked.
The signal will interrupt usleep, and reopen will be called before the next statement (syswrite[3]) is evaluated.
local ($!, $^E); in case the interrupted code uses these.syswrite instead of print? Is it to avoid buffering? Use $fd->autoflush(1); to do that.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With