{"id":6926,"date":"2018-12-19T19:48:35","date_gmt":"2018-12-19T11:48:35","guid":{"rendered":"https:\/\/kyle.ai\/blog\/?p=6926"},"modified":"2018-12-19T19:48:35","modified_gmt":"2018-12-19T11:48:35","slug":"shell-%e8%84%9a%e6%9c%ac%e5%a6%82%e4%bd%95%e4%bc%a0%e9%80%92-sigterm-%e4%bf%a1%e5%8f%b7%e7%bb%99%e5%ad%90%e7%a8%8b%e5%ba%8f","status":"publish","type":"post","link":"https:\/\/kyle.ai\/blog\/6926.html","title":{"rendered":"Shell \u811a\u672c\u5982\u4f55\u4f20\u9012 SIGTERM \u4fe1\u53f7\u7ed9\u5b50\u7a0b\u5e8f"},"content":{"rendered":"<p>\u5047\u5982\u4f60\u5199\u4e86\u4e2a shell \u811a\u672c\uff0c\u6765\u8fd0\u884c\u76ee\u6807\u7a0b\u5e8f\uff0c\u6bd4\u5982 java\uff0c\u7136\u540e\u518d\u7528 supervisor \u6765\u914d\u7f6e\u542f\u52a8\u8fd9\u4e2a shell \u811a\u672c\u3002<\/p>\n<p>cat start.sh<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n#!\/bin\/bash\r\n\r\n# Prepare the JVM command line\r\n...\r\n\r\n$JAVA_EXECUTABLE $JAVA_ARGS\r\n\r\n# Clean up\r\n...\r\n<\/pre>\n<p>cat supervisor.conf<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n&#x5B;program:testjava]\r\ncommand=\/home\/kyle\/testjava\/start.sh\r\ndirectory = \/home\/kyle\/testjava\r\nautorestart=true\r\nuser=ubuntu\r\nredirect_stderr = true\r\nstdout_logfile = \/var\/log\/supervisor\/testjava.log\r\n<\/pre>\n<p>\u8fd9\u65f6\u5019\u4f1a\u6709\u4e2a\u95ee\u9898\uff0c\u5f53 supervisor stop \u4f60\u7684\u7a0b\u5e8f\u7684\u65f6\u5019\uff0cshell \u811a\u672c\u9000\u51fa\u4e86\uff0c\u7136\u540e\u7a0b\u5e8f\u5374\u8fd8\u5728\u3002<\/p>\n<p>\u8fd9\u662f\u56e0\u4e3a TERM \u4fe1\u53f7\u88ab shell \u8fdb\u7a0b\u63a5\u6536\u5230\u4e86\uff0c\u4f46\u662f shell \u5e76\u6ca1\u6709\u628a\u4fe1\u53f7\u8f6c\u7ed9\u5b50\u8fdb\u7a0b\u3002<\/p>\n<p>\u6709\u4e2a\u7b80\u5355\u7684\u89e3\u51b3\u529e\u6cd5\uff0c\u5c31\u662f\u5728\u7a0b\u5e8f\u8fd0\u884c\u547d\u4ee4\u524d\u9762\uff0c\u52a0\u4e0a exec\uff0c\u8fd9\u6837\u7a0b\u5e8f\u8fdb\u7a0b\u4f1a\u66ff\u6362\u5f53\u524d\u7684 bash shell \u8fdb\u7a0b\u3002<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n#!\/bin\/bash\r\n...\r\nexec $JAVA_EXECUTABLE $JAVA_ARGS\r\n<\/pre>\n<p>\u4f46\u662f\u8fd9\u4e2a\u65b9\u6848\u6709\u4e2a\u95ee\u9898\uff0c\u5982\u679c\u60f3\u5728 shell \u811a\u672c\u4e2d\uff0c\u6355\u6349\u5230\u7a0b\u5e8f\u9000\u51fa\uff0c\u7136\u540e\u505a\u4e00\u4e9b\u4e8b\u60c5\uff0c\u5c31\u6ca1\u6709\u529e\u6cd5\u4e86\u3002<\/p>\n<p>\u8fd9\u65f6\u5019\u53ef\u4ee5\u4f7f\u7528 wait\uff0c\u4ee5\u53ca trap \u547d\u4ee4\u6765\u5b9e\u73b0\uff0c\u5177\u4f53\u53ef\u4ee5\u641c\u7d22\u4e0b\u8fd9\u4e24\u4e2a\u547d\u4ee4\u7684\u6587\u6863\u3002<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n#!\/bin\/bash \r\n\r\n_term() { \r\n  echo &quot;Caught SIGTERM signal!&quot; \r\n  kill -TERM &quot;$child&quot; 2&gt;\/dev\/null\r\n}\r\n\r\ntrap _term SIGTERM\r\n\r\necho &quot;Doing some initial work...&quot;;\r\n\/bin\/start\/main\/server --nodaemon &amp;\r\n\r\nchild=$! \r\nwait &quot;$child&quot;\r\n<\/pre>\n<p>\u6216<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n#!\/bin\/bash\r\n...\r\ntrap 'kill -TERM $PID' TERM INT\r\n$JAVA_EXECUTABLE $JAVA_ARGS &amp;\r\nPID=$!\r\nwait $PID\r\ntrap - TERM INT\r\nwait $PID\r\nEXIT_STATUS=$?\r\n...\r\n<\/pre>\n<p>\u53c2\u8003\u8d44\u6599\uff1a<\/p>\n<ul>\n<li><a href=\"http:\/\/veithen.io\/2014\/11\/16\/sigterm-propagation.html\">How to propagate SIGTERM to a child process in a Bash script<\/a><\/li>\n<li><a href=\"https:\/\/unix.stackexchange.com\/questions\/146756\/forward-sigterm-to-child-in-bash\">Forward SIGTERM to child in Bash<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u5047\u5982\u4f60\u5199\u4e86\u4e2a shell \u811a\u672c\uff0c\u6765\u8fd0\u884c\u76ee\u6807\u7a0b\u5e8f\uff0c\u6bd4\u5982 java\uff0c\u7136\u540e\u518d\u7528 supervisor \u6765\u914d\u7f6e\u542f\u52a8\u8fd9\u4e2a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-6926","post","type-post","status-publish","format-standard","hentry","category-skill"],"_links":{"self":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/6926","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/comments?post=6926"}],"version-history":[{"count":1,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/6926\/revisions"}],"predecessor-version":[{"id":6927,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/6926\/revisions\/6927"}],"wp:attachment":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/media?parent=6926"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/categories?post=6926"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/tags?post=6926"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}