$ min-interval-exec 5 date
Wed Mar 21 00:08:38 JST 2018
$ sleep 3
$ min-interval-exec 5 date
$ sleep 2
$ min-interval-exec 5 date
Wed Mar 21 00:08:44 JST 2018
Last active
March 20, 2018 15:09
-
-
Save fujiwara/85373393052bf3d7b36e3efa335722df to your computer and use it in GitHub Desktop.
min interval exec wrapper
This file contains 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 File::stat; | |
use Digest::SHA qw/sha256_hex/; | |
my $tmp = $ENV{TMPDIR} || "/var/tmp"; | |
my $interval = int(shift); | |
my @args = @ARGV; | |
my $key = sha256_hex(join("\0", $0, @args)); | |
my $file = "$tmp/$key"; | |
my $stat = stat($file); | |
if ($stat && $stat->mtime + $interval <= time || !$stat) { | |
touch($file); | |
exec @args; | |
} | |
exit 0; | |
sub touch { | |
my $file = shift; | |
open my $fh, ">", $file or die $!; | |
print $fh scalar(localtime), "\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment