Linux command to run time-limited (timeout)

timeout is a command line utility that runs the specified command, if still running after a given period of time, then terminates the command. timeout command is part of the GNU core utilities package, the package is installed in almost all Linux distributions

how to use

Syntax:

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

DURATION can be a positive integer or floating-point number, followed by an optional suffix:

s - seconds (default)

m - min

h - h

d - Day

If you do not add any units, the default is seconds. Timeout If DURATION is 0, then the associated disabled.

Examples

5 seconds after the termination of the 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  

Here Insert Picture Description
After 5 minutes terminate the ping operation:

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

1 day after terminating the ping:

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

2.5 seconds after termination of the 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

Here Insert Picture Description
Sends the specified signal
if not given any signal, when the time limit is reached, timeout transmits the command to the managed SIGTERM signal. You can use -s (-signal) option specifies the signal to send.

Send SIGKILL to a ping command, 5 seconds after termination of:

[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

Here Insert Picture Description
His name can be specified signal can also specify his number. SIGKILL number of things used below, after 5 seconds, terminating operation:

[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

Here Insert Picture Description
Want to know all available signals, use kill -l command to view all signals.

[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 ~]#

Here Insert Picture Description
The process of turning off the main card

SIGTERM, the signal sent by default when the time limit is exceeded can be captured or ignore certain processes. In this case, the process continues to run after sending termination signal.

To ensure that the command is executed to terminate, use the -k (-kill after) option, followed by one of time. After reaching a given time limit will force the end.

In the following example, timeout command is run one minute, if the command is not ended, the command will be terminated after 10 seconds:

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

Running in the foreground

By default, timeout run managed command in the background. If you want to run this command in the foreground, use -foreground options:

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

to sum up

a command for operating a command timeout time limit. Often given time limit and just give the command is sufficient.

Reprinted from Public number:
Here Insert Picture Description

He published 183 original articles · won praise 17 · views 30000 +

Guess you like

Origin blog.csdn.net/weixin_42506599/article/details/104419184