Skip to content

Instantly share code, notes, and snippets.

@akanehara
Last active November 16, 2016 05:17
Show Gist options
  • Save akanehara/49401b2d6423d3b1e673bf3acaf88e91 to your computer and use it in GitHub Desktop.
Save akanehara/49401b2d6423d3b1e673bf3acaf88e91 to your computer and use it in GitHub Desktop.
指定時間間隔で任意コマンドを繰り返し実行する(コマンド実行時間が次の周期にさしかかった場合はスキップする)
#!/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を待つ
}
@akanehara
Copy link
Author

exec が失敗したとき子プロセスが生き残り続けるバグ修正

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment