[20201123]timeout 命令学习笔记.txt
--//rh 7.0以上coreutils 包含几个以前没有见过的命令在coreutils,stdbuf以及timeout。今天学习一下timeout。
1.环境:
# cat /etc/redhat-release
CentOS Linux release 7.7.1908 (Core)
2.测试:
# rpm -qif $(which timeout)
Name : coreutils
Version : 8.22
Release : 11.el7
Architecture: x86_64
Install Date: Tue 25 Feb 2020 08:20:43 PM CST
Group : System Environment/Base
Size : 14567245
License : GPLv3+
Signature : RSA/SHA256, Fri 04 Jul 2014 09:01:27 AM CST, Key ID 24c6a8a7f4a80eb5
Source RPM : coreutils-8.22-11.el7.src.rpm
Build Date : Tue 10 Jun 2014 09:08:24 AM CST
Build Host : worker1.bsys.centos.org
Relocations : (not relocatable)
Packager : CentOS BuildSystem <http://bugs.centos.org>
Vendor : CentOS
URL : http://www.gnu.org/software/coreutils/
Summary : A set of basic GNU tools commonly used in shell scripts
Description :
These are the GNU core utilities. This package is the combination of
the old GNU fileutils, sh-utils, and textutils packages.
# timeout 1 ping 192.168.100.78 | ts.awk
[2020-11-23 16:32:42] PING 192.168.100.78 (192.168.100.78) 56(84) bytes of data.
[2020-11-23 16:32:42] 64 bytes from 192.168.100.78: icmp_seq=1 ttl=64 time=0.374 ms
Terminated
--//实际上就是给某个命令限制执行时间。
# echo $?
143
# timeout -s SIGKILL 1 ping 192.168.100.78 | ts.awk
[2020-11-23 16:34:14] PING 192.168.100.78 (192.168.100.78) 56(84) bytes of data.
[2020-11-23 16:34:14] 64 bytes from 192.168.100.78: icmp_seq=1 ttl=64 time=0.367 ms
Killed
# timeout -s 9 1 ping 192.168.100.78 | ts.awk
[2020-11-23 16:36:33] PING 192.168.100.78 (192.168.100.78) 56(84) bytes of data.
[2020-11-23 16:36:33] 64 bytes from 192.168.100.78: icmp_seq=1 ttl=64 time=0.395 ms
Killed
# echo $?
137
--//注意如果这样写报错:
# timeout 1 -s SIGKILL ping 192.168.100.78 | ts.awk
timeout: failed to run command '-s': No such file or directory
--//注意两者发的信号不一样,缺省是Terminated,至于 SIGNAL 可以执行kill -l确定。
137-9 = 128
143-128 = 15
# 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
--//缺省发的SIGNAL是15 ,SIGTERM.
# timeout 1m ping -c 5 192.168.100.78
PING 192.168.100.78 (192.168.100.78) 56(84) bytes of data.
64 bytes from 192.168.100.78: icmp_seq=1 ttl=64 time=0.384 ms
^C
--- 192.168.100.78 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.384/0.384/0.384/0.000 ms
# echo $?
0
--//你也可以使用--preserve-status,保留命令退出状态。
exit with the same status as COMMAND, even when the command times out
$ timeout --preserve-status 1 ping -c 5 192.168.100.71
PING 192.168.100.71 (192.168.100.71) 56(84) bytes of data.
$ echo $?
143
$ timeout 1 ping -c 5 192.168.100.71
PING 192.168.100.71 (192.168.100.71) 56(84) bytes of data.
$ echo $?
124
--//不理解--preserve-status参数含义。
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/267265/viewspace-2736159/ ,如需转载,请注明出处,否则将追究法律责任。