Last active
November 16, 2016 05:17
-
-
Save akanehara/49401b2d6423d3b1e673bf3acaf88e91 to your computer and use it in GitHub Desktop.
指定時間間隔で任意コマンドを繰り返し実行する(コマンド実行時間が次の周期にさしかかった場合はスキップする)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
# 指定時間間隔で任意コマンドを繰り返し実行する(コマンド実行時間が次の周期にさしかかった場合はスキップする) | |
use strict; | |
use warnings; | |
use POSIX ":sys_wait_h"; | |
my $interval = shift @ARGV; | |
sub usage { | |
warn "Usage: $0 sec command\n"; | |
} | |
local $SIG{ALRM} = sub {}; | |
my $cpid; | |
while (1) { | |
if (!defined $cpid || waitpid(-1, WNOHANG) > 0) { | |
if ($cpid = fork()) { | |
waitpid($cpid, WNOHANG); | |
} elsif (defined $cpid) { | |
exit 1 unless (exec(@ARGV)); | |
} else { | |
warn "## Cannot fork() [$!]\n"; | |
} | |
} | |
alarm($interval); | |
sleep; # SIGALRMを待つ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
exec が失敗したとき子プロセスが生き残り続けるバグ修正