{"id":171593,"date":"2020-02-01T13:40:43","date_gmt":"2020-02-01T05:40:43","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=171593"},"modified":"2020-02-01T13:40:43","modified_gmt":"2020-02-01T05:40:43","slug":"linux-time-limit","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/linux-time-limit.html","title":{"rendered":"Linux\u4e2d\u8fd0\u884c\u6709\u65f6\u95f4\u9650\u5236\u7684\u547d\u4ee4(timeout)"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n timeout\u662f\u4e00\u4e2a\u547d\u4ee4\u884c\u5b9e\u7528\u7a0b\u5e8f\uff0c\u5b83\u8fd0\u884c\u6307\u5b9a\u7684\u547d\u4ee4\uff0c\u5982\u679c\u5728\u7ed9\u5b9a\u7684\u65f6\u95f4\u6bb5\u540e\u4ecd\u5728\u8fd0\u884c\uff0c\u5219\u7ec8\u6b62\u8be5\u547d\u4ee4\u3002timeout\u547d\u4ee4\u662fGNU\u6838\u5fc3\u5b9e\u7528\u7a0b\u5e8f\u8f6f\u4ef6\u5305\u7684\u4e00\u90e8\u5206\uff0c\u8be5\u8f6f\u4ef6\u5305\u51e0\u4e4e\u5b89\u88c5\u5728\u6240\u6709Linux\u53d1\u884c\u7248\u4e2d<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
\u5982\u4f55\u4f7f\u7528<\/strong><\/div>\n

\u8bed\u6cd5\u683c\u5f0f\uff1a<\/p>\n

\r\ntimeout [OPTION] DURATION COMMAND [ARG]...\r\n<\/pre>\n

DURATION\u53ef\u4ee5\u662f\u6b63\u6574\u6570\u6216\u6d6e\u70b9\u6570\uff0c\u540e\u8ddf\u53ef\u9009\u7684\u540e\u7f00\uff1a<\/p>\n

    \n
  • s \u2013 \u79d2 (\u9ed8\u8ba4) <\/li>\n
  • m \u2013 \u5206\u949f<\/li>\n
  • h \u2013 \u5c0f\u65f6<\/li>\n
  • d \u2013 \u5929<\/li>\n<\/ul>\n

    \u5982\u679c\u4e0d\u6dfb\u52a0\u4efb\u4f55\u5355\u4f4d\uff0c\u9ed8\u8ba4\u662f\u79d2\u3002\u5982\u679cDURATION\u4e3a0\uff0c\u5219\u5173\u8054\u7684\u8d85\u65f6\u662f\u7981\u7528\u7684\u3002<\/p>\n

    \u5b9e\u4f8b<\/strong><\/span><\/div>\n

    5\u79d2\u540e\u7ec8\u6b62ping\u64cd\u4f5c\uff1a<\/p>\n

    \r\n[root@localhost ~]# timeout 5 ping www.baidu.com\r\nPING www.a.shifen.com (61.135.169.125) 56(84) bytes of data.\r\n64 bytes from 61.135.169.125 (61.135.169.125): icmp_seq=1 ttl=55 time=16.3 ms\r\n64 bytes from 61.135.169.125 (61.135.169.125): icmp_seq=2 ttl=55 time=16.0 ms\r\n64 bytes from 61.135.169.125 (61.135.169.125): icmp_seq=3 ttl=55 time=16.7 ms\r\n64 bytes from 61.135.169.125 (61.135.169.125): icmp_seq=4 ttl=55 time=16.0 ms\r\n64 bytes from 61.135.169.125 (61.135.169.125): icmp_seq=5 ttl=55 time=17.6 ms\r\n<\/pre>\n

    \"\"
    \n5\u5206\u949f\u4e4b\u540e\u7ec8\u6b62ping\u64cd\u4f5c\uff1a<\/p>\n

    \r\n[root@localhost ~]# timeout 5m ping www.baidu.com\r\n<\/pre>\n

    1\u5929\u4e4b\u540e\u7ec8\u6b62ping\u64cd\u4f5c\uff1a<\/p>\n

    \r\n[root@localhost ~]# timeout 1d ping www.baidu.com\r\n<\/pre>\n

    2.5\u79d2\u4e4b\u540e\u7ec8\u6b62ping\u64cd\u4f5c\uff1a<\/p>\n

    \r\n[root@localhost ~]# timeout 2.5s ping www.baidu.com\r\nPING www.a.shifen.com (61.135.169.121) 56(84) bytes of data.\r\n64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=1 ttl=55 time=14.9 ms\r\n64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=2 ttl=55 time=15.6 ms\r\n64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=3 ttl=55 time=15.6 ms\r\n<\/pre>\n

    \"\"<\/p>\n

    \u53d1\u9001\u6307\u5b9a\u7684\u4fe1\u53f7<\/strong><\/div>\n

    \u5982\u679c\u672a\u7ed9\u51fa\u4efb\u4f55\u4fe1\u53f7\uff0c\u5219\u5f53\u8fbe\u5230\u65f6\u95f4\u9650\u5236\u65f6\uff0ctimeout\u5c06SIGTERM\u4fe1\u53f7\u53d1\u9001\u5230\u53d7\u7ba1\u547d\u4ee4\u3002\u53ef\u4ee5\u4f7f\u7528-s\uff08-signal\uff09\u9009\u9879\u6307\u5b9a\u8981\u53d1\u9001\u7684\u4fe1\u53f7\u3002<\/p>\n

    \u53d1\u9001SIGKILL\u4fe1\u53f7\u7ed9ping\u547d\u4ee4\uff0c5\u79d2\u949f\u540e\u7ec8\u6b62\uff1a<\/p>\n

    \r\n[root@localhost ~]# sudo timeout -s SIGKILL 5s ping www.baidu.com\r\nPING www.a.shifen.com (61.135.169.125) 56(84) bytes of data.\r\n64 bytes from 61.135.169.125 (61.135.169.125): icmp_seq=1 ttl=55 time=17.2 ms\r\n64 bytes from 61.135.169.125 (61.135.169.125): icmp_seq=2 ttl=55 time=16.6 ms\r\n64 bytes from 61.135.169.125 (61.135.169.125): icmp_seq=3 ttl=55 time=16.7 ms\r\n64 bytes from 61.135.169.125 (61.135.169.125): icmp_seq=4 ttl=55 time=16.2 ms\r\n64 bytes from 61.135.169.125 (61.135.169.125): icmp_seq=5 ttl=55 time=16.7 ms\r\nKilled\r\n<\/pre>\n

    \"\"
    \n\u4fe1\u53f7\u53ef\u4ee5\u6307\u5b9a\u4ed6\u7684\u540d\u5b57\u4e5f\u53ef\u4ee5\u6307\u5b9a\u4ed6\u5e8f\u53f7\u3002\u4e0b\u9762\u4f7f\u7528\u7684\u4e8bSIGKILL\u7684\u5e8f\u53f7\uff0c5\u79d2\u949f\u540e\u7ec8\u6b62\u64cd\u4f5c\uff1a<\/p>\n

    \r\n[root@localhost ~]# sudo timeout -s 9 5s ping www.baidu.com\r\nPING www.a.shifen.com (61.135.169.121) 56(84) bytes of data.\r\n64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=1 ttl=55 time=15.5 ms\r\n64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=2 ttl=55 time=16.3 ms\r\n64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=3 ttl=55 time=14.9 ms\r\n64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=4 ttl=55 time=16.0 ms\r\n64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=5 ttl=55 time=22.0 ms\r\nKilled\r\n<\/pre>\n

    \"\"
    \n\u60f3\u8981\u77e5\u9053\u5168\u90e8\u53ef\u7528\u7684\u4fe1\u53f7\uff0c\u8bf7\u4f7f\u7528 kill -l<\/span>\u8be5\u547d\u4ee4\u67e5\u770b\u5168\u90e8\u7684\u4fe1\u53f7\u3002<\/p>\n

    \r\n[root@localhost ~]# kill -l\r\n 1) SIGHUP\t 2) SIGINT\t 3) SIGQUIT\t 4) SIGILL\t 5) SIGTRAP\r\n 6) SIGABRT\t 7) SIGBUS\t 8) SIGFPE\t 9) SIGKILL\t10) SIGUSR1\r\n11) SIGSEGV\t12) SIGUSR2\t13) SIGPIPE\t14) SIGALRM\t15) SIGTERM\r\n16) SIGSTKFLT\t17) SIGCHLD\t18) SIGCONT\t19) SIGSTOP\t20) SIGTSTP\r\n21) SIGTTIN\t22) SIGTTOU\t23) SIGURG\t24) SIGXCPU\t25) SIGXFSZ\r\n26) SIGVTALRM\t27) SIGPROF\t28) SIGWINCH\t29) SIGIO\t30) SIGPWR\r\n31) SIGSYS\t34) SIGRTMIN\t35) SIGRTMIN+1\t36) SIGRTMIN+2\t37) SIGRTMIN+3\r\n38) SIGRTMIN+4\t39) SIGRTMIN+5\t40) SIGRTMIN+6\t41) SIGRTMIN+7\t42) SIGRTMIN+8\r\n43) SIGRTMIN+9\t44) SIGRTMIN+10\t45) SIGRTMIN+11\t46) SIGRTMIN+12\t47) SIGRTMIN+13\r\n48) SIGRTMIN+14\t49) SIGRTMIN+15\t50) SIGRTMAX-14\t51) SIGRTMAX-13\t52) SIGRTMAX-12\r\n53) SIGRTMAX-11\t54) SIGRTMAX-10\t55) SIGRTMAX-9\t56) SIGRTMAX-8\t57) SIGRTMAX-7\r\n58) SIGRTMAX-6\t59) SIGRTMAX-5\t60) SIGRTMAX-4\t61) SIGRTMAX-3\t62) SIGRTMAX-2\r\n63) SIGRTMAX-1\t64) SIGRTMAX\t\r\n[root@localhost ~]#\r\n<\/pre>\n

    \"\"<\/p>\n

    \u505c\u6389\u5361\u4f4f\u7684\u8fdb\u7a0b<\/strong><\/div>\n

    SIGTERM\uff0c\u5f53\u8d85\u8fc7\u65f6\u95f4\u9650\u5236\u65f6\u53d1\u9001\u7684\u9ed8\u8ba4\u4fe1\u53f7\u53ef\u4ee5\u88ab\u67d0\u4e9b\u8fdb\u7a0b\u6355\u83b7\u6216\u5ffd\u7565\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u8fdb\u7a0b\u5728\u53d1\u9001\u7ec8\u6b62\u4fe1\u53f7\u540e\u7ee7\u7eed\u8fd0\u884c\u3002<\/p>\n

    \u8981\u786e\u4fdd\u88ab\u6267\u884c\u7684\u7684\u547d\u4ee4\u7ec8\u6b62\uff0c\u8bf7\u4f7f\u7528-k\uff08--kill after\uff09\u9009\u9879\uff0c\u540e\u9762\u52a0\u4e00\u4e2a\u65f6\u95f4\u3002\u5f53\u8fbe\u5230\u7ed9\u5b9a\u7684\u65f6\u95f4\u9650\u5236\u540e\u4f1a\u5f3a\u5236\u7ed3\u675f\u3002<\/p>\n

    \u5728\u4e0b\u9762\u7684\u793a\u4f8b\u4e2d\uff0ctimeout\u547d\u4ee4\u8fd0\u884c\u4e00\u5206\u949f\uff0c\u5982\u679c\u547d\u4ee4\u6ca1\u6709\u7ed3\u675f\uff0c\u5c06\u572810\u79d2\u540e\u7ec8\u6b62\u547d\u4ee4\uff1a<\/p>\n

    \r\n[root@localhost ~]# timeout -k 10s 1m sh test.sh\r\n<\/pre>\n
    \u8fd0\u884c\u5728\u524d\u53f0<\/strong><\/div>\n

    \u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0ctimeout\u5728\u540e\u53f0\u8fd0\u884c\u6258\u7ba1\u547d\u4ee4\u3002\u5982\u679c\u8981\u5728\u524d\u53f0\u8fd0\u884c\u8be5\u547d\u4ee4\uff0c\u8bf7\u4f7f\u7528--foreground\u9009\u9879\uff1a<\/p>\n

    \r\n[root@localhost ~]# timeout --foreground 5m .\/script.sh\r\n<\/pre>\n
    \u603b\u7ed3<\/strong><\/div>\n

    timeout\u547d\u4ee4\u7528\u4e8e\u8fd0\u884c\u5177\u6709\u65f6\u95f4\u9650\u5236\u7684\u547d\u4ee4\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\u53ea\u9700\u8981\u7ed9\u5b9a\u65f6\u95f4\u9650\u5236\u548c\u547d\u4ee4\u5c31\u8db3\u591f\u4e86\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

    \u8bed\u6cd5\u683c\u5f0f\uff1a timeout [OPTION] DURATION COMMAND [ARG]… DURATI […]<\/p>\n","protected":false},"author":309,"featured_media":171603,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-171593","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/171593","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/309"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=171593"}],"version-history":[{"count":4,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/171593\/revisions"}],"predecessor-version":[{"id":171605,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/171593\/revisions\/171605"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/171603"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=171593"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=171593"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=171593"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}