这回答命令行命令自动杀死一个命令后,一定的时间

建议使用1行方法使bash命令行中运行的命令超时:

( /path/to/slow command with options ) & sleep 5 ; kill $!

但是,给定的“长时间运行”命令可能会在超时之前完成。 (让我们称它为“通常运行时间长但有时很快”的命令,或者简称为tlrbsf。)

所以这个漂亮的一行方法有几个问题。 首先,睡眠不是有条件的,因此在序列完成所花费的时间上设置了一个不希望的下界。当tlrbsf命令在2秒内完成时,考虑30秒或2m甚至5m的睡眠时间——这是非常不可取的。 其次,kill是无条件的,因此该序列将尝试kill一个未运行的进程并抱怨它。

所以…

是否有一种方法可以超时一个典型的长时间运行但有时快速(“tlrbsf”)命令

有一个bash实现(另一个问题已经有Perl和C的答案) 将终止在较早的两个:TLRBSF程序终止,或超时过去 不会杀死不存在/不运行的进程(或者,可选:不会抱怨失败的杀死) 不一定非要是一行字 可以在Cygwin或Linux下运行吗

... 还有,为了加分

在前台运行TLRBSF命令 任何“睡眠”或额外的进程在后台

这样,TLRBSF命令的stdin/stdout/stderr可以重定向,就像它已经直接运行吗?

如果是,请分享你的代码。如果不是,请解释原因。

我花了一段时间试图破解前面提到的例子,但我的bash技能达到了极限。


当前回答

OS X还没有使用bash 4,也没有/usr/bin/timeout,所以这里有一个在OS X上工作的函数,没有自制或macports,类似于/usr/bin/timeout(基于Tino的回答)。参数验证、帮助、使用和对其他信号的支持是读者的练习。

# implement /usr/bin/timeout only if it doesn't exist
[ -n "$(type -p timeout 2>&1)" ] || function timeout { (
    set -m +b
    sleep "$1" &
    SPID=${!}
    ("${@:2}"; RETVAL=$?; kill ${SPID}; exit $RETVAL) &
    CPID=${!}
    wait %1
    SLEEPRETVAL=$?
    if [ $SLEEPRETVAL -eq 0 ] && kill ${CPID} >/dev/null 2>&1 ; then
      RETVAL=124
      # When you need to make sure it dies
      #(sleep 1; kill -9 ${CPID} >/dev/null 2>&1)&
      wait %2
    else
      wait %2
      RETVAL=$?
    fi
    return $RETVAL
) }

其他回答

我更喜欢“限时”,它至少在debian中有一个包。

http://devel.ringlet.net/sysutils/timelimit/

它比coreutils的“timeout”要好一点,因为它在终止进程时打印一些东西,并且在默认情况下,它还在一段时间后发送SIGKILL。

有点俗气,但很管用。不工作,如果你有其他前台进程(请帮我修复这个!)

sleep TIMEOUT & SPID=${!}; (YOUR COMMAND HERE; kill ${SPID}) & CPID=${!}; fg 1; kill ${CPID}

实际上,我认为你可以反过来,满足你的“奖金”标准:

(YOUR COMMAND HERE & SPID=${!}; (sleep TIMEOUT; kill ${SPID}) & CPID=${!}; fg 1; kill ${CPID}) < asdf > fdsa

您可能正在coreutils中寻找timeout命令。因为它是coreutils的一部分,所以技术上它是一个C解决方案,但它仍然是coreutils。信息超时查看更多详细信息。 这里有一个例子:

timeout 5 /path/to/slow/command with options

我认为这正是你所要求的:

http://www.bashcookbook.com/bashinfo/source/bash-4.0/examples/scripts/timeout3

#!/bin/bash
#
# The Bash shell script executes a command with a time-out.
# Upon time-out expiration SIGTERM (15) is sent to the process. If the signal
# is blocked, then the subsequent SIGKILL (9) terminates it.
#
# Based on the Bash documentation example.

# Hello Chet,
# please find attached a "little easier"  :-)  to comprehend
# time-out example.  If you find it suitable, feel free to include
# anywhere: the very same logic as in the original examples/scripts, a
# little more transparent implementation to my taste.
#
# Dmitry V Golovashkin <Dmitry.Golovashkin@sas.com>

scriptName="${0##*/}"

declare -i DEFAULT_TIMEOUT=9
declare -i DEFAULT_INTERVAL=1
declare -i DEFAULT_DELAY=1

# Timeout.
declare -i timeout=DEFAULT_TIMEOUT
# Interval between checks if the process is still alive.
declare -i interval=DEFAULT_INTERVAL
# Delay between posting the SIGTERM signal and destroying the process by SIGKILL.
declare -i delay=DEFAULT_DELAY

function printUsage() {
    cat <<EOF

Synopsis
    $scriptName [-t timeout] [-i interval] [-d delay] command
    Execute a command with a time-out.
    Upon time-out expiration SIGTERM (15) is sent to the process. If SIGTERM
    signal is blocked, then the subsequent SIGKILL (9) terminates it.

    -t timeout
        Number of seconds to wait for command completion.
        Default value: $DEFAULT_TIMEOUT seconds.

    -i interval
        Interval between checks if the process is still alive.
        Positive integer, default value: $DEFAULT_INTERVAL seconds.

    -d delay
        Delay between posting the SIGTERM signal and destroying the
        process by SIGKILL. Default value: $DEFAULT_DELAY seconds.

As of today, Bash does not support floating point arithmetic (sleep does),
therefore all delay/time values must be integers.
EOF
}

# Options.
while getopts ":t:i:d:" option; do
    case "$option" in
        t) timeout=$OPTARG ;;
        i) interval=$OPTARG ;;
        d) delay=$OPTARG ;;
        *) printUsage; exit 1 ;;
    esac
done
shift $((OPTIND - 1))

# $# should be at least 1 (the command to execute), however it may be strictly
# greater than 1 if the command itself has options.
if (($# == 0 || interval <= 0)); then
    printUsage
    exit 1
fi

# kill -0 pid   Exit code indicates if a signal may be sent to $pid process.
(
    ((t = timeout))

    while ((t > 0)); do
        sleep $interval
        kill -0 $$ || exit 0
        ((t -= interval))
    done

    # Be nice, post SIGTERM first.
    # The 'exit 0' below will be executed if any preceeding command fails.
    kill -s SIGTERM $$ && kill -0 $$ || exit 0
    sleep $delay
    kill -s SIGKILL $$
) 2> /dev/null &

exec "$@"

OS X还没有使用bash 4,也没有/usr/bin/timeout,所以这里有一个在OS X上工作的函数,没有自制或macports,类似于/usr/bin/timeout(基于Tino的回答)。参数验证、帮助、使用和对其他信号的支持是读者的练习。

# implement /usr/bin/timeout only if it doesn't exist
[ -n "$(type -p timeout 2>&1)" ] || function timeout { (
    set -m +b
    sleep "$1" &
    SPID=${!}
    ("${@:2}"; RETVAL=$?; kill ${SPID}; exit $RETVAL) &
    CPID=${!}
    wait %1
    SLEEPRETVAL=$?
    if [ $SLEEPRETVAL -eq 0 ] && kill ${CPID} >/dev/null 2>&1 ; then
      RETVAL=124
      # When you need to make sure it dies
      #(sleep 1; kill -9 ${CPID} >/dev/null 2>&1)&
      wait %2
    else
      wait %2
      RETVAL=$?
    fi
    return $RETVAL
) }