天天看点

linux history命令显示时间_Linux中运行有时间限制的命令(timeout)

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

如 何 使 用

语法格式:

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

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

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

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

实例

5秒后终止ping操作:

[[email protected] ~]# timeout 5 ping www.baidu.comPING 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 ms64 bytes from 61.135.169.125 (61.135.169.125): icmp_seq=2 ttl=55 time=16.0 ms64 bytes from 61.135.169.125 (61.135.169.125): icmp_seq=3 ttl=55 time=16.7 ms64 bytes from 61.135.169.125 (61.135.169.125): icmp_seq=4 ttl=55 time=16.0 ms64 bytes from 61.135.169.125 (61.135.169.125): icmp_seq=5 ttl=55 time=17.6 ms  
           
linux history命令显示时间_Linux中运行有时间限制的命令(timeout)

5分钟之后终止ping操作:

[[email protected] ~]# timeout 5m ping www.baidu.com
           

1天之后终止ping操作:

[[email protected] ~]# timeout 1d ping www.baidu.com
           

2.5秒之后终止ping操作:

[[email protected] ~]# timeout 2.5s ping www.baidu.comPING 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 ms64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=2 ttl=55 time=15.6 ms64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=3 ttl=55 time=15.6 ms
           
linux history命令显示时间_Linux中运行有时间限制的命令(timeout)

发送指定的信号

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

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

[[email protected] ~]# sudo timeout -s SIGKILL 5s ping www.baidu.comPING 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 ms64 bytes from 61.135.169.125 (61.135.169.125): icmp_seq=2 ttl=55 time=16.6 ms64 bytes from 61.135.169.125 (61.135.169.125): icmp_seq=3 ttl=55 time=16.7 ms64 bytes from 61.135.169.125 (61.135.169.125): icmp_seq=4 ttl=55 time=16.2 ms64 bytes from 61.135.169.125 (61.135.169.125): icmp_seq=5 ttl=55 time=16.7 msKilled
           
linux history命令显示时间_Linux中运行有时间限制的命令(timeout)

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

[[email protected] ~]# sudo timeout -s 9 5s ping www.baidu.comPING 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 ms64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=2 ttl=55 time=16.3 ms64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=3 ttl=55 time=14.9 ms64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=4 ttl=55 time=16.0 ms64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=5 ttl=55 time=22.0 msKilled
           
linux history命令显示时间_Linux中运行有时间限制的命令(timeout)

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

[[email protected] ~]# kill -l 1) SIGHUP     2) SIGINT   3) SIGQUIT  4) SIGILL   5) SIGTRAP 6) SIGABRT     7) SIGBUS   8) SIGFPE   9) SIGKILL 10) SIGUSR111) SIGSEGV    12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM16) SIGSTKFLT    17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP21) SIGTTIN    22) SIGTTOU 23) SIGURG  24) SIGXCPU 25) SIGXFSZ26) SIGVTALRM    27) SIGPROF 28) SIGWINCH    29) SIGIO   30) SIGPWR31) SIGSYS    34) SIGRTMIN    35) SIGRTMIN+1  36) SIGRTMIN+2  37) SIGRTMIN+338) SIGRTMIN+4    39) SIGRTMIN+5  40) SIGRTMIN+6  41) SIGRTMIN+7  42) SIGRTMIN+843) SIGRTMIN+9    44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+1348) SIGRTMIN+14    49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-1253) SIGRTMAX-11    54) SIGRTMAX-10 55) SIGRTMAX-9  56) SIGRTMAX-8  57) SIGRTMAX-758) SIGRTMAX-6    59) SIGRTMAX-5  60) SIGRTMAX-4  61) SIGRTMAX-3  62) SIGRTMAX-263) SIGRTMAX-1    64) SIGRTMAX    [[email protected] ~]#
           
linux history命令显示时间_Linux中运行有时间限制的命令(timeout)

停掉卡主的进程

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

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

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

[[email protected] ~]# timeout -k 10s 1m sh test.sh
           

运 行 在 前 台

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

[[email protected] ~]# timeout --foreground 5m ./script.sh
           

总 结

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