杀死ssh上的特定进程超过一分钟


Kill specific process on ssh greater than one minute

我有一些shell脚本,如下所示,

我只想运行这个过程1分钟。

所以我想检查进程时间,如果进程运行超过1分钟,就终止进程。有人能帮我吗?

var=`ps -eaf | pgrep -f getShippingPriceUK.php | wc -l`
if [ $var -lt "1" ]; then
echo "Prozess läuft nicht"
wget -q --timeout=0 --delete-after http://xxxx/001_yakodo/getShippingPrice12.php > /dev/null 2&>1 &
else
echo "Prozess läuft"
fi

如果安装了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;