Shell命令的闹钟设置超时

时间:2020-01-09 10:43:40  来源:igfitidea点击:

如何运行名为foo的命令,并在运行bash shell或者脚本的GNU/Linux下10秒后使其超时/中止?
如何在闹钟下运行命令?
您应该能够使用python/ruby/php或者perl来将闹钟计时器设置为shell命令超时。

Shell没有内置的用于超时的命令。

Shell命令的闹钟设置超时

让我们看看各种例子

Perl程序

这是一个示例perl代码:

perl -e 'alarm shift @ARGV; exec @ARGV' 5 foo arg1 arg2

您可以将其定义为shell函数:

alarm() { perl -e 'alarm shift; exec @ARGV' "$@"; }

并按如下方式调用它(在使foo命令无效之前等待20秒:)

alarm 20 foo arg1

示例Shell脚本来设置命令超时

您可以使用以下shell脚本为命令设置超时:

##########################################################################
# Shellscript:	timeout - set timeout for a command 
#               Alarm clock Set Timeout
# Author     :	Heiner Steven <[email protected]>
# Date       :	29.07.1999
# Category   :	File Utilities
# Requires   :	
# SCCS-Id.   :	@(#) timeout	1.3 03/03/18
##########################################################################
# Description
#    o	Runs a command, and terminates it (by sending a signal) after
#	a specified time period
#    o	This command first starts itself as a "watchdog" process in the
#	background, and then runs the specified command.
#	If the command did not terminate after the specified
#	number of seconds, the "watchdog" process will terminate
#	the command by sending a signal.
#
# Notes
#    o	Uses the internal command line argument "-p" to specify the
#	PID of the process to terminate after the timeout to the
#	"watchdog" process.
#    o	The "watchdog" process is invoked by the name "
wget https://www.theitroad.local/files/doalarm-0.1.7.tgz
", so # "
$ wget http://pilcrow.madison.wi.us/sw/doalarm-0.1.7.tgz
" must be a valid path to the script. # o If this script runs in the environment of the login shell # (i.e. it was invoked using ". timeout command...") it will # terminate the login session. ##########################################################################   PN=`basename "
$ tar -zxvf doalarm-0.1.7.tg
"` # Program name VER='1.3'   TIMEOUT=5 # Default [seconds]   Usage () { echo >&2 "$PN - set timeout for a command, $VER usage: $PN [-t timeout] command [argument ...] -t: timeout (in seconds, default is $TIMEOUT)" exit 1 }   Msg () { for MsgLine do echo "$PN: $MsgLine" >&2 done }   Fatal () { Msg "$@"; exit 1; }   while [ $# -gt 0 ] do case "" in -p) ParentPID=; shift;; # Used internally! -t) Timeout=""; shift;; --) shift; break;; -h) Usage;; -*) Usage;; *) break;; # First file name esac shift done   : ${Timeout:=$TIMEOUT} # Set default [seconds]   if [ -z "$ParentPID" ] then # This is the first invokation of this script. # Start "watchdog" process, and then run the command. [ $# -lt 1 ] && Fatal "please specify a command to execute" "
$ cd doalarm-0.1.7
$ make
$ ./doalarm
" -p $$ -t $Timeout & # Start watchdog #echo >&2 "DEBUG: process id is $$" exec "$@" # Run command exit 2 # NOT REACHED else # We run in "watchdog" mode, $ParentPID contains the PID # of the process we should terminate after $Timeout seconds. [ $# -ne 0 ] && Fatal "please do not use -p option interactively"   #echo >&2 "DEBUG: $$: parent PID to terminate is $ParentPID"   exec >/dev/null 0<&1 2>&1 # Suppress error messages sleep $Timeout kill $ParentPID && # Give process time to terminate (sleep 2; kill -1 $ParentPID) && (sleep 2; kill -9 $ParentPID) exit 0 fi

doalarm C程序

使用wget命令或者curl命令下载doalarm程序

Error: missing required parameter
Usage: doalarm [-hr] [-t type] sec command [arg...]
Run command under an alarm clock.

Options:
-t          Type of timer: 'real' (SIGALRM), 'virtual' (SIGVTALRM),
    --timer= 'profile' (SIGPROF), 'cpu' (SIGXCPU).  Default 'real'.
-r --recur        Recurring alarm, every sec seconds.
-h --help         Show help text (this message).
    --version      Display version.

doalarm 0.1.7 (14 Dec 2001)

或者

$ doalarm 20 foo

使用tar命令解压缩程序

command1 arg1 & sleep 10 ; kill $!
/path/to/foo bar & sleep 5 ; kill $!

编译doalarm:

timeout DURATION COMMAND
timeout DURATION COMMAND arg1 arg2
timeout 1m ping theitroad.com

输出示例:

##代码##

如下运行foo:

##代码##

主警报器在即将发生的警报下执行命令fooo。
缺省秒数以秒为单位的实际秒数到期后(通常是致命的)SIGALRM到期。
可用的过程计时器有两种类型:间隔和限制。
有关计时器和各自的信号,请参见下面的选项。
间隔计时器可能仅发出警报一次,也可能会定期发出警报(-r),每秒钟发出一次信号。
对于间隔计时器,零秒值将清除继承的警报(如果有)。
对于限制计时器,零将命令cpu限制提高到硬最大值(通常为无限制)。

如何在Linux或者Unix Shell中使命令超时

使用命令设置睡眠时间,&&条件sa如下:

##代码##

超时命令timeout

GNU/Linux附带超时命令。
它运行一个有时间限制的命令。
本教程说明如何启动命令,以及在指定的超时时间到期时将其杀死:

##代码##