{"id":1830,"date":"2024-12-03T22:15:54","date_gmt":"2024-12-03T14:15:54","guid":{"rendered":"https:\/\/www.madbull.site\/?p=1830"},"modified":"2024-12-03T22:22:35","modified_gmt":"2024-12-03T14:22:35","slug":"%e4%bc%98%e9%9b%85%e5%85%b3%e9%97%ad%e8%bf%9b%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/www.madbull.site\/?p=1830","title":{"rendered":"\u4f18\u96c5\u5173\u95ed\u8fdb\u7a0b"},"content":{"rendered":"\n<p class=\"has-large-font-size\"><strong>\u4ecb\u7ecd<\/strong><\/p>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--10);margin-bottom:var(--wp--preset--spacing--10);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>\u672c\u6587\u6d89\u53ca\u5230\u8fdb\u7a0b\u5bf9\u4fe1\u53f7\u7684\u54cd\u5e94\uff0c\u5173\u4e8e\u4fe1\u53f7\u7684\u4e00\u4e9b\u57fa\u672c\u77e5\u8bc6\uff0c\u53ef\u4ee5\u53c2\u8003\u5f80\u671f\u6587\u7ae0\uff1a<a href=\"https:\/\/www.madbull.site\/?p=1759\">https:\/\/www.madbull.site\/?p=1759<\/a><\/p>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--10);margin-bottom:var(--wp--preset--spacing--10);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>\u4e00\u4e2a\u8fdb\u7a0b\u6b63\u5e38\u8fd0\u884c\u7684\u65f6\u5019\uff0c\u53ef\u4ee5\u901a\u8fc7\u7ed9\u8fdb\u7a0b\u53d1\u9001 <strong>SIGKILL 9<\/strong> \u4fe1\u53f7\uff0c\u76f4\u63a5\u5f3a\u5236\u5173\u95ed\u8fdb\u7a0b\u3002\u8fdb\u7a0b\u6240\u6301\u6709\u7684\u4e00\u4e9b\u8d44\u6e90\uff0c\u7531\u64cd\u4f5c\u7cfb\u7edf\u6765\u56de\u6536\u5904\u7406\uff0c\u8fd9\u6837\u786e\u5b9e\u5f88\u7701\u4e8b\u3002\u4f46\u662f\u8fd9\u79cd\u505a\u6cd5\u4e5f\u5f88\u66b4\u529b\uff0c\u4f1a\u5bfc\u81f4\u4e00\u4e9b\u8d44\u6e90\u4e0d\u80fd\u53ca\u65f6\u91ca\u653e\u3002\u4f8b\u5982\u76d1\u542c\u7aef\u53e3\u3001\u9700\u8981\u6e05\u7406\u7684 PID \u6587\u4ef6\u7b49\u7b49\u3002\u90a3\u4e48\u600e\u4e48\u4f18\u96c5\u5730\u5173\u95ed\u4e00\u4e2a\u6b63\u5728\u8fd0\u884c\u7684\u8fdb\u7a0b\u5462\uff1f<\/p>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--10);margin-bottom:var(--wp--preset--spacing--10);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>\u8fd9\u5c31\u9700\u8981\u7ed9\u8fdb\u7a0b\u8bbe\u7f6e\u4fe1\u53f7\u54cd\u5e94\u3002<strong>SIGKILL <\/strong>\u4fe1\u53f7\u59cb\u7ec8\u90fd\u53ef\u4ee5\u5f3a\u5236\u5173\u95ed\u8fdb\u7a0b\uff0c\u8fd9\u662f\u56e0\u4e3a <strong>SIGKILL <\/strong>\u4fe1\u53f7\u662f\u4e0d\u53ef\u88ab\u6355\u6349\uff0c\u4e5f\u5c31\u4e0d\u53ef\u4ee5\u88ab\u8fdb\u7a0b\u91cd\u65b0\u8bbe\u7f6e\u4fe1\u53f7\u5904\u7406\u51fd\u6570\u3002\u4f46\u662f\u6709\u53e6\u5916\u4e24\u4e2a\u4fe1\u53f7 <strong>SIGINT 2 <\/strong>\u548c <strong>SIGTERM 15 <\/strong>\u662f\u53ef\u4ee5\u88ab\u6355\u6349\u7684\uff0c\u4e5f\u7ecf\u5e38\u7528\u6765\u4f18\u96c5\u5730\u5173\u95ed\u8fdb\u7a0b\u3002\u6211\u4eec\u53ef\u4ee5\u8ba9\u8fdb\u7a0b\u6355\u6349\u8fd9\u4e24\u4e2a\u4fe1\u53f7\uff0c\u7136\u540e\u8bbe\u7f6e\u4fe1\u53f7\u5904\u7406\u51fd\u6570\uff0c\u6765\u4e3b\u52a8\u91ca\u653e\u8fdb\u7a0b\u6240\u6301\u6709\u7684\u8d44\u6e90\uff0c\u8ba9\u8fdb\u7a0b\u81ea\u52a8\u9000\u51fa\u3002<\/p>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--10);margin-bottom:var(--wp--preset--spacing--10);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>\u53e6\u5916 <strong>systemctl stop xxx<\/strong> \u5c31\u662f\u7ed9\u8fdb\u7a0b\u53d1\u9001 <strong>SIGTERM <\/strong>\u4fe1\u53f7\uff0c\u8ba9\u8fdb\u7a0b\u4e3b\u52a8\u9000\u51fa\u3002<\/p>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--10);margin-bottom:var(--wp--preset--spacing--10);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"has-large-font-size\"><strong>\u793a\u4f8b<\/strong><\/p>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--10);margin-bottom:var(--wp--preset--spacing--10);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>\u4ee5\u4e0b\u4ee5 python \u4e3a\u4f8b\uff0c\u7ed3\u5408 systemd \u542f\u505c python \u8fdb\u7a0b\u6765\u6d4b\u8bd5\u7684\u3002\u5176\u4e2d\u8fd8\u6d89\u53ca python \u7684\u865a\u62df\u73af\u5883\uff0c\u53ef\u4ee5\u53c2\u8003\u5f80\u671f\u6587\u7ae0\uff1a<a href=\"https:\/\/www.madbull.site\/?p=1558\">https:\/\/www.madbull.site\/?p=1558<\/a><\/p>\n\n\n\n<p>\u5f53\u7136\u4e5f\u53ef\u4ee5\u76f4\u63a5\u53d1\u9001 2 \u548c 15 \u4fe1\u53f7\u8ba9\u8fdb\u7a0b\u81ea\u52a8\u5173\u95ed\u3002\u6267\u884c\u6307\u4ee4\uff1akill -2 pid \u548c kill -15 pid \u5c31\u53ef\u4ee5\u4e86\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u8981 C \u8bed\u8a00\u5b9e\u73b0\uff0c\u4e5f\u662f\u540c\u6837\u7684\u9053\u7406\uff0c\u6355\u6349\u4fe1\u53f7\uff0c\u8bbe\u7f6e\u4fe1\u53f7\u5904\u7406\u51fd\u6570\uff0c\u4e3b\u52a8\u9000\u51fa\u3002<\/p>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--10);margin-bottom:var(--wp--preset--spacing--10);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p><strong>\u4ee3\u7801 tsys.py<\/strong><\/p>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--10);margin-bottom:var(--wp--preset--spacing--10);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<pre class=\"wp-block-code\"><code>import signal\nimport time\nfrom pid import PidFile\n\n# \u670d\u52a1\u8fd0\u884c\u72b6\u6001\uff0c\u4e3a True \u5c31\u9700\u8981\u4e3b\u52a8\u91ca\u653e\u4e00\u4e9b\u8d44\u6e90\u4e86\nexit_flag = False\n\ndef stop_handler(sig, frame) :\n    global exit_flag\n    # \u8bbe\u7f6e\u8fdb\u7a0b\u4e3a\u5f85\u5173\u95ed\u72b6\u6001\n    exit_flag = True\n    print(\"get \"+signal.strsignal(sig)+\" \"+str(sig), flush=True)\n\n\n# \u542f\u52a8\u81ea\u5df1\u7684\u670d\u52a1\ndef run_server() :\n    while True:\n        # \u5224\u65ad\u662f\u5426\u63a5\u6536\u5230\u4e2d\u65ad\u670d\u52a1\u7684\u4fe1\u53f7\n        if exit_flag :\n            ## \u7b49\u5f85\u672a\u5b8c\u6210\u7684\u670d\u52a1\u5904\u7406\u5b8c\n            ## \u56de\u6536\u8d44\u6e90\uff0c\u5173\u95ed\u6587\u4ef6\u63cf\u8ff0\u7b26\u7b49\u7b49\n            break\n        ## \u5728\u8fd9\u91cc\u8fd0\u884c\u81ea\u5df1\u9700\u8981\u7684\u670d\u52a1\uff0c\u7533\u8bf7\u8d44\u6e90\u7b49\u7b49\n        print(\"still running\", flush=True)\n        time.sleep(1)\n        pass\n\nif __name__ == \"__main__\" :\n    # \u54cd\u5e94 SIGINT 2 \u4e2d\u65ad\u4fe1\u53f7\n    signal.signal( signal.SIGINT, stop_handler )\n    # \u54cd\u5e94 SIGTERM 15 \u8bf7\u6c42\u8fdb\u7a0b\u7ec8\u6b62\u4fe1\u53f7\n    signal.signal( signal.SIGTERM, stop_handler )\n    with PidFile(pidname=\"tsys.pid\", piddir=\".\") :\n        run_server()\n<\/code><\/pre>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--10);margin-bottom:var(--wp--preset--spacing--10);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p><strong>\u914d\u7f6e tsys.service<\/strong>\uff0c\u8def\u5f84 \/usr\/lib\/systemd\/system\/tsys.service<\/p>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--10);margin-bottom:var(--wp--preset--spacing--10);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;Unit]\nDescription=Test pid file SIGTERM\nAfter=multi-user.target\n\n&#91;Service]\nType=simple\nWorkingDirectory=\/root\/test\nExecStart=bash -c \"source \/opt\/tvenv\/bin\/activate &amp;&amp; exec python tsys.py > tsys.log\"\nRestart=always\n\n&#91;Install]\nWantedBy=multi-user.target\n<\/code><\/pre>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--10);margin-bottom:var(--wp--preset--spacing--10);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"has-large-font-size\"><strong>\u6d4b\u8bd5<\/strong><\/p>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--10);margin-bottom:var(--wp--preset--spacing--10);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>\u542f\u52a8<\/p>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--10);margin-bottom:var(--wp--preset--spacing--10);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"988\" height=\"484\" src=\"https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/12\/\u56fe\u7247-4.png\" alt=\"\" class=\"wp-image-1831\" srcset=\"https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/12\/\u56fe\u7247-4.png 988w, https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/12\/\u56fe\u7247-4-300x147.png 300w, https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/12\/\u56fe\u7247-4-768x376.png 768w\" sizes=\"auto, (max-width: 988px) 100vw, 988px\" \/><\/figure>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--10);margin-bottom:var(--wp--preset--spacing--10);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>\u505c\u6b62<\/p>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--10);margin-bottom:var(--wp--preset--spacing--10);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"976\" height=\"535\" src=\"https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/12\/\u56fe\u7247-5.png\" alt=\"\" class=\"wp-image-1832\" srcset=\"https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/12\/\u56fe\u7247-5.png 976w, https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/12\/\u56fe\u7247-5-300x164.png 300w, https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/12\/\u56fe\u7247-5-768x421.png 768w\" sizes=\"auto, (max-width: 976px) 100vw, 976px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u6d89\u53ca\u5230\u8fdb\u7a0b\u5bf9\u4fe1\u53f7\u7684\u54cd\u5e94\uff0c\u5173\u4e8e\u4fe1\u53f7\u7684\u4e00\u4e9b\u57fa\u672c\u77e5\u8bc6\uff0c\u53ef\u4ee5\u53c2\u8003\u5f80\u671f\u6587\u7ae0\uff1ahttps:\/\/www.madbull.site\/?p=1759<\/p>\n<p>\u4e00\u4e2a\u8fdb\u7a0b\u6b63\u5e38\u8fd0\u884c\u7684\u65f6\u5019\uff0c\u53ef\u4ee5\u7ed9\u8fdb\u7a0b\u53d1\u9001 SIGKILL 9 \u4fe1\u53f7\uff0c\u76f4\u63a5\u5f3a\u5236\u5173\u95ed\u8fdb\u7a0b\u3002\u8fdb\u7a0b\u6240\u6301\u6709\u7684\u4e00\u4e9b\u8d44\u6e90\uff0c\u7531\u64cd\u4f5c\u7cfb\u7edf\u6765\u56de\u6536\u5904\u7406\uff0c\u8fd9\u6837\u786e\u5b9e\u5f88\u7701\u4e8b\u3002\u4f46\u662f\u8fd9\u79cd\u505a\u6cd5\u4e5f\u5f88\u66b4\u529b\uff0c\u4f1a\u5bfc\u81f4\u4e00\u4e9b\u8d44\u6e90\u4e0d\u80fd\u53ca\u65f6\u91ca\u653e\u3002\u4f8b\u5982\u76d1\u542c\u7aef\u53e3\u3001\u9700\u8981\u6e05\u7406\u7684 PID \u6587\u4ef6\u7b49\u7b49\u3002\u90a3\u4e48\u600e\u4e48\u4f18\u96c5\u7684\u5173\u95ed\u4e00\u4e2a\u6b63\u5728\u8fd0\u884c\u7684\u8fdb\u7a0b\u5462\uff1f<\/p>\n","protected":false},"author":1,"featured_media":450,"comment_status":"open","ping_status":"open","sticky":false,"template":"single-with-sidebar","format":"standard","meta":{"footnotes":""},"categories":[135,155,131,154],"tags":[596,586,587,589,594,595,598,597,544],"class_list":["post-1830","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux","category-python","category-131","category-154","tag-service","tag-sigint","tag-sigkill","tag-sigterm","tag-systemctl","tag-systemd","tag-598","tag-597","tag-544"],"_links":{"self":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts\/1830","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1830"}],"version-history":[{"count":6,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts\/1830\/revisions"}],"predecessor-version":[{"id":1839,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts\/1830\/revisions\/1839"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/media\/450"}],"wp:attachment":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1830"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1830"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1830"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}