BLOGTIMES
2013/11/25

制限時間つけてコマンドを実行する

  sh  cli  centos 
このエントリーをはてなブックマークに追加

先日 bash で簡易なテストドライバを書いたのですが、プログラムの出来が悪いと永久ループしたりりすることがあるので、コマンドの1回の実行にに時間制限をつけて永久ループをするようなプログラムでも安全にテストできるようにしてみました。この時間制限は所謂、タイムアウトというやつなので、試しにコマンドラインで timeout と打ってみたら使えそうなコマンドが見つかりました

$ timeout --help Usage: timeout [OPTION] NUMBER[SUFFIX] COMMAND [ARG]... or: timeout [OPTION] Start COMMAND, and kill it if still running after NUMBER seconds. SUFFIX may be `s' for seconds (the default), `m' for minutes, `h' for hours or `d' for days. 長いオプションに必須の引数は短いオプションにも必須です. -s, --signal=SIGNAL specify the signal to be sent on timeout. SIGNAL may be a name like `HUP' or a number. See `kill -l` for a list of signals --help この使い方を表示して終了 --version バージョン情報を表示して終了 If the command times out, then exit with status 124. Otherwise, exit with the status of COMMAND. If no signal is specified, send the TERM signal upon timeout. The TERM signal kills any process that does not block or catch that signal. For other processes, it may be necessary to use the KILL (9) signal, since this signal cannot be caught. Report timeout bugs to bug-coreutils@gnu.org GNU coreutils home page: <http://www.gnu.org/software/coreutils/> General help using GNU software: <http://www.gnu.org/gethelp/> Report timeout translation bugs to <http://translationproject.org/team/> For complete documentation, run: info coreutils 'timeout invocation' $ rpm -qf /usr/bin/timeout coreutils-8.4-19.el6.x86_64

CentOS 6.x の場合には coreutils の一部としてインストールされるようですね。
使い方は timeout 5 sleep 10 のように timeout (タイムアウト値) (コマンド) となるようにすればいいので簡単ですね。

ちなみに、CentOS 5.x の coreutils には timeout が含まれていませんが、bash のパッケージとして /usr/share/doc/bash-3.2/scripts/timeout というスクリプトが含まれているので、これを使えば良いみたいです。UNTESTED と書かれているのがちょっと気になりますが、簡単なプログラムでテストした限りは上手く動きました。

$ rpm -qf /usr/share/doc/bash-3.2/scripts/timeout bash-3.2-32.el5_9.1 $ cat /usr/share/doc/bash-3.2/scripts/timeout #Newsgroups: comp.unix.admin,comp.unix.solaris,comp.unix.shell #From: gwc@root.co.uk (Geoff Clare) #Subject: Re: timeout -t <sec> <unix command> (Re: How to give rsh a shorter timeout?) #Message-ID: <EoBxrs.223@root.co.uk> #Date: Fri, 13 Feb 1998 18:23:52 GMT # # Conversion to bash v2 syntax done by Chet Ramey <chet@po.cwru.edu # UNTESTED # prog=${0##*/} usage="usage: $prog [-signal] [timeout] [:interval] [+delay] [--] <command>" SIG=-TERM # default signal sent to the process when the timer expires timeout=60 # default timeout interval=15 # default interval between checks if the process is still alive delay=2 # default delay between posting the given signal and # destroying the process (kill -KILL) while : do case $1 in --) shift; break ;; -*) SIG=$1 ;; [0-9]*) timeout=$1 ;; :*) EXPR='..\(.*\)' ; interval=`expr x"$1" : "$EXPR"` ;; +*) EXPR='..\(.*\)' ; delay=`expr x"$1" : "$EXPR"` ;; *) break ;; esac shift done case $# in 0) echo "$prog: $usage" >&2 ; exit 2 ;; esac ( for t in $timeout $delay do while (( $t > $interval )) do sleep $interval kill -0 $$ || exit t=$(( $t - $interval )) done sleep $t kill $SIG $$ && kill -0 $$ || exit SIG=-KILL done ) 2> /dev/null & exec "$@"

    トラックバックについて
    Trackback URL:
    お気軽にどうぞ。トラックバック前にポリシーをお読みください。[policy]
    このエントリへのTrackbackにはこのURLが必要です→https://blog.cles.jp/item/6293
    Trackbacks
    このエントリにトラックバックはありません
    Comments
    愛のあるツッコミをお気軽にどうぞ。[policy]
    古いエントリについてはコメント制御しているため、即時に反映されないことがあります。
    コメントはありません
    Comments Form

    コメントは承認後の表示となります。
    OpenIDでログインすると、即時に公開されます。

    OpenID を使ってログインすることができます。

    Identity URL: Yahoo! JAPAN IDでログイン