傳播小知識:timeout限制時間命令
timeout是一個 行實用程式,它執行指定的 ,如果在給定的時間段後仍在執行,則終止該命令。timeout命令是GNU核心實用程式軟體包的一部分,該軟體包幾乎安裝在所有 發行版中 |
如何使用
語法格式:
timeout [OPTION] DURATION COMMAND [ARG]...
DURATION可以是正整數或浮點數,後跟可選的字尾:
- s – 秒 (預設)
- m – 分鐘
- h – 小時
- d – 天
如果不新增任何單位,預設是秒。如果DURATION為0,則關聯的超時是禁用的。
例項
5秒後終止ping操作:
[root@localhost ~]# timeout 5 ping PING (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
1天之後終止ping操作:
[root@localhost ~]# timeout 1d ping
2.5秒之後終止ping操作:
[root@localhost ~]# timeout 2.5s ping PING (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 PING (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 PING (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命令用於執行具有時間限制的命令。通常情況下只需要給定時間限制和命令就足夠了。
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/31524109/viewspace-2719103/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- oracle小知識點14--xtts傳輸表空間OracleTTS
- nginx限制上傳大小和超時時間設定說明/php限制上傳大小NginxPHP
- Oracle表空間小知識Oracle
- oracle小知識點12--傳輸表空間通過rmanOracle
- oracle小知識點10--表空間時間點恢復(TSPITR)Oracle
- oracle小知識點11--傳輸表空間通過impdp/expdpOracle
- 彙編必知小知識點及常用debug命令
- 知識論(一): 知識傳遞
- 華為 組播理論知識
- 【Linux小知識】在排除網路故障時,這些命令一定要會!Linux
- 知識小匯
- iOS小知識iOS
- canvas小知識Canvas
- IPC小知識
- 感冒小知識
- baota小知識
- Android小知識-定時任務ScheduledThreadPoolExecutorAndroidthread
- grep命令的知識.
- 處暑 | 節氣小知識+安全播報,別樣金秋,異彩紛呈!
- pcl常用小知識
- java小知識點Java
- Python小知識Python
- js小知識點JS
- Spring小知識Spring
- Flex知識小結Flex
- 小知識點1
- JavaSE小知識(二)Java
- 前端小知識點前端
- mongo 小知識點Go
- javascript小知識點JavaScript
- PHP小知識點PHP
- Oracle知識小記Oracle
- 小知識總結
- SAP GUI小知識GUI
- 【知識分享】Linux檔案傳輸常用的9個命令!Linux
- 寶付知識普及時間丨有關征信的知識點,你真的清楚嗎?
- Vuejs基本知識(八)【頁面間的引數傳遞】VueJS
- 不要讓“知識”限制了你的思維