美文网首页Linux
Linux中运行有时间限制的命令(timeout)

Linux中运行有时间限制的命令(timeout)

作者: 易霂 | 来源:发表于2020-02-01 23:58 被阅读0次

timeout是一个命令行实用程序,它运行指定的命令,如果在给定的时间段后仍在运行,则终止该命令。timeout命令是GNU核心实用程序软件包的一部分,该软件包几乎安装在所有Linux发行版中

如何使用

语法格式:

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

DURATION可以是正整数或浮点数,后跟可选的后缀:

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

如果不添加任何单位,默认是秒。如果DURATION为0,则关联的超时是禁用的。

实例

5秒后终止ping操作:

[root@localhost ~]# timeout 5 ping www.baidu.com
PING www.a.shifen.com (61.135.169.125) 56(84) bytes of data.
64 bytes from 61.135.169.125 (61.135.169.125): icmp_seq=1 ttl=55 time=16.3 ms
64 bytes from 61.135.169.125 (61.135.169.125): icmp_seq=2 ttl=55 time=16.0 ms
64 bytes from 61.135.169.125 (61.135.169.125): icmp_seq=3 ttl=55 time=16.7 ms
64 bytes from 61.135.169.125 (61.135.169.125): icmp_seq=4 ttl=55 time=16.0 ms
64 bytes from 61.135.169.125 (61.135.169.125): icmp_seq=5 ttl=55 time=17.6 ms

5分钟之后终止ping操作:

[root@localhost ~]# timeout 5m ping www.baidu.com

1天之后终止ping操作:

[root@localhost ~]# timeout 1d ping www.baidu.com

2.5秒之后终止ping操作:

[root@localhost ~]# timeout 2.5s ping www.baidu.com
PING www.a.shifen.com (61.135.169.121) 56(84) bytes of data.
64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=1 ttl=55 time=14.9 ms
64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=2 ttl=55 time=15.6 ms
64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=3 ttl=55 time=15.6 ms

发送指定的信号

如果未给出任何信号,则当达到时间限制时,timeout将SIGTERM信号发送到受管命令。可以使用-s(-signal)选项指定要发送的信号。

发送SIGKILL信号给ping命令,5秒钟后终止:

[root@localhost ~]# sudo timeout -s SIGKILL 5s ping www.baidu.com
PING www.a.shifen.com (61.135.169.125) 56(84) bytes of data.
64 bytes from 61.135.169.125 (61.135.169.125): icmp_seq=1 ttl=55 time=17.2 ms
64 bytes from 61.135.169.125 (61.135.169.125): icmp_seq=2 ttl=55 time=16.6 ms
64 bytes from 61.135.169.125 (61.135.169.125): icmp_seq=3 ttl=55 time=16.7 ms
64 bytes from 61.135.169.125 (61.135.169.125): icmp_seq=4 ttl=55 time=16.2 ms
64 bytes from 61.135.169.125 (61.135.169.125): icmp_seq=5 ttl=55 time=16.7 ms
Killed

信号可以指定他的名字也可以指定他序号。下面使用的事SIGKILL的序号,5秒钟后终止操作:

[root@localhost ~]# sudo timeout -s 9 5s ping www.baidu.com
PING www.a.shifen.com (61.135.169.121) 56(84) bytes of data.
64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=1 ttl=55 time=15.5 ms
64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=2 ttl=55 time=16.3 ms
64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=3 ttl=55 time=14.9 ms
64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=4 ttl=55 time=16.0 ms
64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=5 ttl=55 time=22.0 ms
Killed

想要知道全部可用的信号,请使用 kill -l该命令查看全部的信号。

[root@localhost ~]# kill -l
 1) SIGHUP   2) SIGINT   3) SIGQUIT  4) SIGILL   5) SIGTRAP
 6) SIGABRT  7) SIGBUS   8) SIGFPE   9) SIGKILL 10) SIGUSR1
11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM
16) SIGSTKFLT   17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP
21) SIGTTIN 22) SIGTTOU 23) SIGURG  24) SIGXCPU 25) SIGXFSZ
26) SIGVTALRM   27) SIGPROF 28) SIGWINCH    29) SIGIO   30) SIGPWR
31) SIGSYS  34) SIGRTMIN    35) SIGRTMIN+1  36) SIGRTMIN+2  37) SIGRTMIN+3
38) SIGRTMIN+4  39) SIGRTMIN+5  40) SIGRTMIN+6  41) SIGRTMIN+7  42) SIGRTMIN+8
43) SIGRTMIN+9  44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13
48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12
53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9  56) SIGRTMAX-8  57) SIGRTMAX-7
58) SIGRTMAX-6  59) SIGRTMAX-5  60) SIGRTMAX-4  61) SIGRTMAX-3  62) SIGRTMAX-2
63) SIGRTMAX-1  64) SIGRTMAX    
[root@localhost ~]#

停掉卡住的进程

SIGTERM,当超过时间限制时发送的默认信号可以被某些进程捕获或忽略。在这种情况下,进程在发送终止信号后继续运行。

要确保被执行的的命令终止,请使用-k(--kill after)选项,后面加一个时间。当达到给定的时间限制后会强制结束。

在下面的示例中,timeout命令运行一分钟,如果命令没有结束,将在10秒后终止命令:

[root@localhost ~]# timeout -k 10s 1m sh test.sh

运行在前台

默认情况下,timeout在后台运行托管命令。如果要在前台运行该命令,请使用--foreground选项:

[root@localhost ~]# timeout --foreground 5m ./script.sh

总结

timeout命令用于运行具有时间限制的命令。通常情况下只需要给定时间限制和命令就足够了。

相关文章

  • Linux中运行有时间限制的命令(timeout)

    timeout是一个命令行实用程序,它运行指定的命令,如果在给定的时间段后仍在运行,则终止该命令。timeout命...

  • 如何在Linux中运行具有时间限制(Timeout)的命令

    Linux附带了许多命令,每个命令都是唯一的,并在特定情况下使用。Linux的目标是帮助你尽快高效地工作。Linu...

  • 传播小知识:timeout限制时间命令

    timeout是一个命令行实用程序,它运行指定的命令,如果在给定的时间段后仍在运行,则终止该命令。timeout命...

  • Linux sudo 漏洞可能导致未经授权的特权访问

    在 Linux 中利用新发现的 sudo 漏洞可以使某些用户以 root 身份运行命令,尽管对此还有所限制。 su...

  • linux下运行jar包连接断开继续运行

    # Linux中jar包启动和jar包后台运行 Linux 运行jar包命令如下: 方式一: ...

  • ps

    Linux中的ps命令是Process Status的缩写。 ps命令用来列出系统中当前运行的那些进程。 ps命令...

  • MySQL打开文件限制

    Linux系统资源限制 Linux会对用户所占用的系统资源进行限制,MySQL运行在Linux系统下也会受此限制。...

  • linux下的ps命令

    Linux中的ps命令是Process Status的缩写。ps命令用来列出系统中当前运行的那些进程。ps命令列出...

  • 20220816笔记

    Linux常用操作 Linux查看文件内容命令 运行以及停止jar包命令(Linux) 写入文件命令 maven常...

  • kill

    Linux中的kill命令用来终止指定的进程(terminate a process)的运行,是Linux下进程管...

网友评论

    本文标题:Linux中运行有时间限制的命令(timeout)

    本文链接:https://www.haomeiwen.com/subject/xnjfxhtx.html