如果你安装了 perl,你可以使用这个 perl 脚本。
将其另存为timeout
,将其存储在 中的目录之一中$PATH
,使其可执行,然后替换所有调用
your-script arg1...
和
timeout 1m your-script arg1...
#!/usr/bin/perl
use warnings;
use strict;
$SIG{CHLD} = sub { wait; exit };
my $timeout = shift;
for ($timeout) {
if (defined and /^(\d+)([smh])$/) {
$_ = $1 * ("s" eq $2 ? 1 : "m" eq $2 ? 60 : 3600);
} elsif (!defined or /\D/) {
die "First argument must be time to wait.\n";
}
}
my @command = @ARGV or die "Must give a command to run.\n";
my $child = fork;
for ($child) {
die "$0: Cannot fork: $!\n" unless defined;
last if $child;
exec {$command[0]} @command or die "$0: Cannot exec: $!\n";
}
close STDIN;
close STDOUT;
close STDERR;
sleep $timeout;
kill TERM => $child;
sleep 5;
kill KILL => $child;