A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
-bash/zsh: timeout command not found # Windows (WSL2) sudo apt-get update sudo apt-get install coreutils # Debian apt-get install coreutils # Ubuntu apt-get install coreutils # Alpine apk add coreutils # Arch Linux pacman -S coreutils # Kali Linux apt-get install coreutils # CentOS yum install coreutils # Fedora dnf install coreutils # OS X brew install coreutils # Raspbian apt-get install coreutils # Dockerfile dockerfile.run/timeout # Docker docker run cmd.cat/timeout timeout
timeout 命令作用是运行指定命令,如果在指定时间后在运行则杀死该进程。timeout 命令是 GNU 核心实用程序软件包的一部分,该软件包几乎安装在所有 Linux 发行版中
当我们想让一个定时的 crontab 任务运行运行一段时间后,如何自动终止?一般有两种方案:
timeout [OPTION] DURATION COMMAND [ARG]... timeout [OPTION]
s – 秒 (默认) m – 分钟 h – 小时 d – 天
如果不添加任何单位,默认是秒。如果 DURATION 为 0,则关联的超时是禁用的。默认情况下,timeout 在后台运行托管命令。如果要在前台运行该命令,请使用 --foreground 选项:
timeout --foreground 5m ./yangyongyu.sh
Start COMMAND, and kill it if still running after DURATION. Mandatory arguments to long options are mandatory for short options too. -f, --foreground when not running timeout directly from a shell prompt, allow COMMAND to read from the TTY and get TTY signals; in this mode, children of COMMAND will not be timed out -k, --kill-after=DURATION also send a KILL signal if COMMAND is still running this long after the initial signal was sent -p, --preserve-status exit with the same status as COMMAND, even when the command times out -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 -v, --verbose diagnose to stderr any signal sent upon timeout --help display this help and exit --version output version information and exit DURATION is a floating point number with an optional suffix: 's' for seconds (the default), 'm' for minutes, 'h' for hours or 'd' for days. A duration of 0 disables the associated timeout. Upon timeout, send the TERM signal to COMMAND, if no other SIGNAL specified. The TERM signal kills any process that does not block or catch that signal. It may be necessary to use the KILL signal, since this signal can't be caught.
124 if COMMAND times out, and --preserve-status is not specified 125 if the timeout command itself fails 126 if COMMAND is found but cannot be invoked 127 if COMMAND cannot be found 137 if COMMAND (or timeout itself) is sent the KILL (9) signal (128+9) - the exit status of COMMAND otherwise
timeout 运行 sleep 10 并终止它(如果它运行超过 3 秒):
timeout 3s sleep 10
timeout 发送指定的信号,如 --signal INT 5s
timeout --signal INT 5s sleep 10 # 发送 SIGKILL 信号给 ping 命令,5秒钟后终止: timeout -s SIGKILL 5s ping www.baidu.com