Linux timeout命令的使用:timeout - run a command with a time limit

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

NAME
       timeout - run a command with a time limit

SYNOPSIS
       timeout [OPTION] DURATION COMMAND [ARG]...
       timeout [OPTION]
$ timeout 5 ping 127.0.0.1
PING 127.0.0.1 (127.0.0.1): 56 data bytes
64 bytes from 127.0.0.1: icmp_seq=0 ttl=128 time=0.410 ms
64 bytes from 127.0.0.1: icmp_seq=1 ttl=128 time=0.181 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=128 time=0.174 ms
64 bytes from 127.0.0.1: icmp_seq=3 ttl=128 time=0.146 ms
64 bytes from 127.0.0.1: icmp_seq=4 ttl=128 time=0.174 ms

$ timeout -s SIGKILL 5s ping 127.0.0.1
PING 127.0.0.1 (127.0.0.1): 56 data bytes
64 bytes from 127.0.0.1: icmp_seq=0 ttl=128 time=0.331 ms
64 bytes from 127.0.0.1: icmp_seq=1 ttl=128 time=0.207 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=128 time=0.385 ms
64 bytes from 127.0.0.1: icmp_seq=3 ttl=128 time=0.393 ms
64 bytes from 127.0.0.1: icmp_seq=4 ttl=128 time=0.123 ms
Killed

$ timeout -s 9 5s ping 127.0.0.1
PING 127.0.0.1 (127.0.0.1): 56 data bytes
64 bytes from 127.0.0.1: icmp_seq=0 ttl=128 time=0.281 ms
64 bytes from 127.0.0.1: icmp_seq=1 ttl=128 time=0.355 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=128 time=0.507 ms
64 bytes from 127.0.0.1: icmp_seq=3 ttl=128 time=0.215 ms
64 bytes from 127.0.0.1: icmp_seq=4 ttl=128 time=0.167 ms
Killed

$ kill -l
 1) SIGHUP       2) SIGINT       3) SIGQUIT      4) SIGILL       5) SIGTRAP
 6) SIGABRT      7) SIGEMT       8) SIGFPE       9) SIGKILL     10) SIGBUS
11) SIGSEGV     12) SIGSYS      13) SIGPIPE     14) SIGALRM     15) SIGTERM
16) SIGURG      17) SIGSTOP     18) SIGTSTP     19) SIGCONT     20) SIGCHLD
21) SIGTTIN     22) SIGTTOU     23) SIGIO       24) SIGXCPU     25) SIGXFSZ
26) SIGVTALRM   27) SIGPROF     28) SIGWINCH    29) SIGPWR      30) SIGUSR1
31) SIGUSR2     32) SIGRTMIN    33) SIGRTMIN+1  34) SIGRTMIN+2  35) SIGRTMIN+3
36) SIGRTMIN+4  37) SIGRTMIN+5  38) SIGRTMIN+6  39) SIGRTMIN+7  40) SIGRTMIN+8
41) SIGRTMIN+9  42) SIGRTMIN+10 43) SIGRTMIN+11 44) SIGRTMIN+12 45) SIGRTMIN+13
46) SIGRTMIN+14 47) SIGRTMIN+15 48) SIGRTMIN+16 49) SIGRTMAX-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

$ timeout -k 2s 1m ping 127.0.0.1
PING 127.0.0.1 (127.0.0.1): 56 data bytes
64 bytes from 127.0.0.1: icmp_seq=0 ttl=128 time=0.296 ms
64 bytes from 127.0.0.1: icmp_seq=1 ttl=128 time=0.160 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=128 time=0.121 ms
64 bytes from 127.0.0.1: icmp_seq=3 ttl=128 time=0.146 ms
64 bytes from 127.0.0.1: icmp_seq=4 ttl=128 time=0.160 ms
64 bytes from 127.0.0.1: icmp_seq=5 ttl=128 time=0.525 ms
64 bytes from 127.0.0.1: icmp_seq=6 ttl=128 time=0.142 ms
64 bytes from 127.0.0.1: icmp_seq=7 ttl=128 time=0.178 ms
64 bytes from 127.0.0.1: icmp_seq=8 ttl=128 time=0.516 ms
64 bytes from 127.0.0.1: icmp_seq=9 ttl=128 time=0.148 ms
64 bytes from 127.0.0.1: icmp_seq=10 ttl=128 time=0.134 ms
64 bytes from 127.0.0.1: icmp_seq=11 ttl=128 time=0.145 ms
64 bytes from 127.0.0.1: icmp_seq=12 ttl=128 time=0.169 ms
64 bytes from 127.0.0.1: icmp_seq=13 ttl=128 time=0.165 ms
64 bytes from 127.0.0.1: icmp_seq=14 ttl=128 time=0.181 ms
64 bytes from 127.0.0.1: icmp_seq=15 ttl=128 time=0.172 ms
64 bytes from 127.0.0.1: icmp_seq=16 ttl=128 time=0.141 ms
64 bytes from 127.0.0.1: icmp_seq=17 ttl=128 time=0.166 ms
64 bytes from 127.0.0.1: icmp_seq=18 ttl=128 time=0.185 ms
64 bytes from 127.0.0.1: icmp_seq=19 ttl=128 time=0.163 ms
64 bytes from 127.0.0.1: icmp_seq=20 ttl=128 time=0.153 ms
64 bytes from 127.0.0.1: icmp_seq=21 ttl=128 time=0.157 ms
64 bytes from 127.0.0.1: icmp_seq=22 ttl=128 time=0.165 ms
64 bytes from 127.0.0.1: icmp_seq=23 ttl=128 time=0.148 ms
64 bytes from 127.0.0.1: icmp_seq=24 ttl=128 time=0.119 ms
64 bytes from 127.0.0.1: icmp_seq=25 ttl=128 time=0.172 ms
64 bytes from 127.0.0.1: icmp_seq=26 ttl=128 time=0.177 ms
64 bytes from 127.0.0.1: icmp_seq=27 ttl=128 time=0.189 ms
64 bytes from 127.0.0.1: icmp_seq=28 ttl=128 time=0.143 ms
64 bytes from 127.0.0.1: icmp_seq=29 ttl=128 time=0.163 ms
64 bytes from 127.0.0.1: icmp_seq=30 ttl=128 time=0.155 ms
64 bytes from 127.0.0.1: icmp_seq=31 ttl=128 time=0.156 ms
64 bytes from 127.0.0.1: icmp_seq=32 ttl=128 time=0.275 ms
64 bytes from 127.0.0.1: icmp_seq=33 ttl=128 time=0.361 ms
64 bytes from 127.0.0.1: icmp_seq=34 ttl=128 time=0.379 ms
64 bytes from 127.0.0.1: icmp_seq=35 ttl=128 time=0.116 ms
64 bytes from 127.0.0.1: icmp_seq=36 ttl=128 time=0.210 ms
64 bytes from 127.0.0.1: icmp_seq=37 ttl=128 time=0.157 ms
64 bytes from 127.0.0.1: icmp_seq=38 ttl=128 time=0.148 ms
64 bytes from 127.0.0.1: icmp_seq=39 ttl=128 time=0.192 ms
64 bytes from 127.0.0.1: icmp_seq=40 ttl=128 time=0.143 ms
64 bytes from 127.0.0.1: icmp_seq=41 ttl=128 time=0.183 ms
64 bytes from 127.0.0.1: icmp_seq=42 ttl=128 time=0.185 ms
64 bytes from 127.0.0.1: icmp_seq=43 ttl=128 time=0.181 ms
64 bytes from 127.0.0.1: icmp_seq=44 ttl=128 time=0.162 ms
64 bytes from 127.0.0.1: icmp_seq=45 ttl=128 time=0.187 ms
64 bytes from 127.0.0.1: icmp_seq=46 ttl=128 time=0.165 ms
64 bytes from 127.0.0.1: icmp_seq=47 ttl=128 time=0.146 ms
64 bytes from 127.0.0.1: icmp_seq=48 ttl=128 time=0.138 ms
64 bytes from 127.0.0.1: icmp_seq=49 ttl=128 time=0.175 ms
64 bytes from 127.0.0.1: icmp_seq=50 ttl=128 time=0.154 ms
64 bytes from 127.0.0.1: icmp_seq=51 ttl=128 time=0.162 ms
64 bytes from 127.0.0.1: icmp_seq=52 ttl=128 time=0.169 ms
64 bytes from 127.0.0.1: icmp_seq=53 ttl=128 time=0.146 ms
64 bytes from 127.0.0.1: icmp_seq=54 ttl=128 time=0.170 ms
64 bytes from 127.0.0.1: icmp_seq=55 ttl=128 time=0.189 ms
64 bytes from 127.0.0.1: icmp_seq=56 ttl=128 time=0.183 ms
64 bytes from 127.0.0.1: icmp_seq=57 ttl=128 time=0.165 ms
64 bytes from 127.0.0.1: icmp_seq=58 ttl=128 time=0.176 ms
64 bytes from 127.0.0.1: icmp_seq=59 ttl=128 time=0.194 ms
发布了598 篇原创文章 · 获赞 285 · 访问量 50万+

猜你喜欢

转载自blog.csdn.net/Rong_Toa/article/details/104417786