timeout 命令详解

| 选择喜欢的代码风格  

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

timeout 命令安装:


-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 命令作用是运行指定命令,如果在指定时间后在运行则杀死该进程。timeout 命令是 GNU 核心实用程序软件包的一部分,该软件包几乎安装在所有 Linux 发行版中

timeout 命令场景:


当我们想让一个定时的 crontab 任务运行运行一段时间后,如何自动终止?一般有两种方案:

  1. 启动一个进程任务,然后在启动一个杀死进程任务
  2. 使用 Linux 中的 timeout 命令

timeout 命令语法:


timeout [OPTION] DURATION COMMAND [ARG]...
timeout [OPTION]

timeout 命令选项:


s – 秒 (默认)
m – 分钟
h – 小时
d – 天

如果不添加任何单位,默认是秒。如果 DURATION 为 0,则关联的超时是禁用的。默认情况下,timeout 在后台运行托管命令。如果要在前台运行该命令,请使用 --foreground 选项

timeout --foreground 5m ./yangyongyu.sh

timeout 命令 Sub COMMAND:


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.

timeout 命令退出码 - Exit Code Status:


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 命令实例:


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 

timeout 扩展阅读:




timeout 命令评论