{"id":235,"date":"2025-01-25T17:05:28","date_gmt":"2025-01-25T09:05:28","guid":{"rendered":"https:\/\/blog.keruone.cn\/?p=235"},"modified":"2025-02-06T16:13:11","modified_gmt":"2025-02-06T08:13:11","slug":"235","status":"publish","type":"post","link":"https:\/\/blog.keruone.cn\/index.php\/2025\/01\/25\/235\/","title":{"rendered":"\u8fdc\u7a0b\u8fd0\u884c\u6587\u4ef6"},"content":{"rendered":"<h1>\u8fdc\u7a0b\u8fd0\u884c\u6587\u4ef6<\/h1>\n<p>Keruone<\/p>\n\n<hr \/>\n<h2>1. \u4f7f\u7528 <code>nohup<\/code><\/h2>\n<h3>1.1 \u4e00\u822c\u4f7f\u7528<\/h3>\n<ul>\n<li>\u5047\u8bbe\u6709\u6587\u4ef6 <code>test.py<\/code>\uff0c\u9700\u8981\u53c2\u6570 <code>1 2 3<\/code>\uff0c\u5728\u8fdc\u7a0b\u670d\u52a1\u5668\u4e0a\u8fd0\u884c\uff0c\u53ef\u4ee5\u4f7f\u7528 nohup \u547d\u4ee4\uff1a\n<pre><code class=\"language-bash line-numbers\">nohup python test.py 1 2 3 &amp;\n<\/code><\/pre>\n<p>\u5176\u4e2d <code>&amp;<\/code> \u8868\u793a\u5728\u540e\u53f0\u8fd0\u884c\u3002<br \/>\n<\/p>\n<\/li>\n<li>\n<p>\u5982\u679c\u9700\u8981\u5728\u8fdc\u7a0b\u670d\u52a1\u5668\u4e0a\u67e5\u770b\u8fd0\u884c\u65e5\u5fd7\uff0c\u53ef\u4ee5\u4f7f\u7528 tail \u547d\u4ee4\uff1a<\/p>\n<pre><code class=\"language-bash line-numbers\">tail -f nohup.out\n<\/code><\/pre>\n<p>\u5176\u4e2d <code>nohup.out<\/code> \u662f nohup \u547d\u4ee4\u751f\u6210\u7684\u65e5\u5fd7\u6587\u4ef6\u3002<\/p>\n<blockquote><p>\n  nohup.out \u4fdd\u5b58\u5728\u5f53\u524d\u76ee\u5f55\u4e0b<br \/>\n  \n<\/p><\/blockquote>\n<\/li>\n<\/ul>\n<h3>1.2 \u91cd\u5b9a\u5411\u8f93\u51fa<\/h3>\n<ul>\n<li>\u5982\u679c\u9700\u8981\u5c06\u8f93\u51fa\u91cd\u5b9a\u5411\u5230\u6307\u5b9a\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>&gt;<\/code> \u7b26\u53f7\uff1a\n<pre><code class=\"language-bash line-numbers\">nohup python test.py 1 2 3 &gt; output.log 2&gt;&amp;1 &amp;\n<\/code><\/pre>\n<p>\u5176\u4e2d<\/p>\n<ul>\n<li><code>&gt;<\/code> \u8868\u793a\u5c06\u8f93\u51fa\u91cd\u5b9a\u5411\u5230\u6307\u5b9a\u6587\u4ef6\u3002<\/li>\n<li><code>2&gt;&amp;1<\/code> \u8868\u793a\u5c06\u6807\u51c6\u9519\u8bef\u8f93\u51fa\u91cd\u5b9a\u5411\u5230\u6807\u51c6\u8f93\u51fa\u3002<\/li>\n<li><code>output.log<\/code> \u662f\u6307\u5b9a\u7684\u8f93\u51fa\u6587\u4ef6\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3>1.3 \u67e5\u770b\u8fdb\u7a0b<\/h3>\n<ul>\n<li>\u67e5\u770b\u8fdb\u7a0b\n<pre><code class=\"language-bash line-numbers\">ps aux | grep test.py\n<\/code><\/pre>\n<p>\u5176\u4e2d<\/p>\n<ul>\n<li><code>ps aux<\/code> \u8868\u793a\u67e5\u770b\u6240\u6709\u8fdb\u7a0b\u3002<\/li>\n<li><code>grep test.py<\/code> \u8868\u793a\u8fc7\u6ee4\u51fa\u5305\u542b <code>test.py<\/code> \u7684\u8fdb\u7a0b\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3>1.4 \u6740\u6b7b\u8fdb\u7a0b<\/h3>\n<ul>\n<li>\u6740\u6b7b\u8fdb\u7a0b\n<pre><code class=\"language-bash line-numbers\">kill -9 &lt;pid&gt;\n<\/code><\/pre>\n<p>\u5176\u4e2d<\/p>\n<ul>\n<li><code>kill -9<\/code> \u8868\u793a\u5f3a\u5236\u6740\u6b7b\u8fdb\u7a0b\uff0c\u4e5f\u53ef\u4ee5\u4e0d\u4f7f\u7528 <code>-9<\/code>\uff0c\u4f7f\u7528\u9ed8\u8ba4\u3002<\/li>\n<li><code>&lt;pid&gt;<\/code> \u662f\u8fdb\u7a0b\u7684 ID\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\u5047\u8bbe\u4f60\u7684\u6587\u4ef6\u8fd8\u4f1a\u751f\u6210\u591a\u4e2a\u5b50\u8fdb\u7a0b\uff0c\u4f60\u60f3\u5168\u90e8\u6740\u6b7b\uff0c\u53ef\u4ee5\u4f7f\u7528\n<pre><code>kill &dollar;(ps aux | grep test.py | grep -v grep | awk '{print &dollar;2}')<\/code><\/pre>\n<p>\u5176\u4e2d<\/p>\n<ul>\n<li><code>ps aux<\/code> \u8868\u793a\u67e5\u770b\u6240\u6709\u8fdb\u7a0b\u3002<\/li>\n<li><code>grep test.py<\/code> \u8868\u793a\u8fc7\u6ee4\u51fa\u5305\u542b <code>test.py<\/code> \u7684\u8fdb\u7a0b\u3002<\/li>\n<li><code>grep -v grep<\/code> \u8868\u793a\u8fc7\u6ee4\u51fa\u4e0d\u5305\u542b <code>grep<\/code> \u7684\u8fdb\u7a0b<\/li>\n<li><code>awk '{print $2}'<\/code> \u8868\u793a\u6253\u5370\u51fa\u8fdb\u7a0b\u7684 ID\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><!-- \u4f7f\u7528detail --><\/p>\n<details>\n<summary>\u5177\u4f53\u6f14\u793a\u793a\u4f8b<\/summary>\n<h4><strong>\u5047\u8bbe\u7cfb\u7edf\u4e2d\u6709\u4ee5\u4e0b\u8fdb\u7a0b<\/strong>\uff1a<\/h4>\n<pre><code class=\"line-numbers\">USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND\nuser       123  0.0  0.1  12345  6789 pts\/0    S+   10:00   0:00 python test.py\nuser       124  0.0  0.1  12345  6789 pts\/1    S+   10:01   0:00 python test.py\nuser       125  0.0  0.1  12345  6789 pts\/2    S+   10:02   0:00 python test.py\nuser       126  0.0  0.1  12345  6789 pts\/3    S+   10:03   0:00 python test.py\nuser       127  0.0  0.0  12345  6789 pts\/4    S+   10:04   0:00 grep test.py\n<\/code><\/pre>\n<hr \/>\n<h4><strong>\u547d\u4ee4\u6267\u884c\u8fc7\u7a0b<\/strong><\/h4>\n<h5>1 <strong><code>ps aux<\/code><\/strong><\/h5>\n<ul>\n<li>\u5217\u51fa\u6240\u6709\u8fdb\u7a0b\uff0c\u8f93\u51fa\u5982\u4e0b\uff1a\n<pre><code class=\"line-numbers\">user       123  0.0  0.1  12345  6789 pts\/0    S+   10:00   0:00 python test.py\nuser       124  0.0  0.1  12345  6789 pts\/1    S+   10:01   0:00 python test.py\nuser       125  0.0  0.1  12345  6789 pts\/2    S+   10:02   0:00 python test.py\nuser       126  0.0  0.1  12345  6789 pts\/3    S+   10:03   0:00 python test.py\nuser       127  0.0  0.0  12345  6789 pts\/4    S+   10:04   0:00 grep test.py\n<\/code><\/pre>\n<\/li>\n<\/ul>\n<h5>2 <strong><code>grep test.py<\/code><\/strong><\/h5>\n<ul>\n<li>\u8fc7\u6ee4\u51fa\u5305\u542b <code>test.py<\/code> \u7684\u8fdb\u7a0b\uff0c\u8f93\u51fa\u5982\u4e0b\uff1a\n<pre><code class=\"line-numbers\">user       123  0.0  0.1  12345  6789 pts\/0    S+   10:00   0:00 python test.py\nuser       124  0.0  0.1  12345  6789 pts\/1    S+   10:01   0:00 python test.py\nuser       125  0.0  0.1  12345  6789 pts\/2    S+   10:02   0:00 python test.py\nuser       126  0.0  0.1  12345  6789 pts\/3    S+   10:03   0:00 python test.py\nuser       127  0.0  0.0  12345  6789 pts\/4    S+   10:04   0:00 grep test.py\n<\/code><\/pre>\n<\/li>\n<\/ul>\n<h5>3 <strong><code>grep -v grep<\/code><\/strong><\/h5>\n<ul>\n<li>\u6392\u9664\u5305\u542b <code>grep<\/code> \u7684\u8fdb\u7a0b\uff0c\u8f93\u51fa\u5982\u4e0b\uff1a\n<pre><code class=\"line-numbers\">user       123  0.0  0.1  12345  6789 pts\/0    S+   10:00   0:00 python test.py\nuser       124  0.0  0.1  12345  6789 pts\/1    S+   10:01   0:00 python test.py\nuser       125  0.0  0.1  12345  6789 pts\/2    S+   10:02   0:00 python test.py\nuser       126  0.0  0.1  12345  6789 pts\/3    S+   10:03   0:00 python test.py\n<\/code><\/pre>\n<\/li>\n<\/ul>\n<h5>4 <strong><code>awk '{print $2}'<\/code><\/strong><\/h5>\n<ul>\n<li>\u63d0\u53d6\u8fdb\u7a0b ID\uff08<code>PID<\/code>\uff09\uff0c\u8f93\u51fa\u5982\u4e0b\uff1a\n<pre><code class=\"line-numbers\">123\n124\n125\n126\n<\/code><\/pre>\n<\/li>\n<\/ul>\n<h5>5 <strong><code>kill $(...)<\/code><\/strong><\/h5>\n<ul>\n<li>\u5c06\u63d0\u53d6\u5230\u7684 <code>PID<\/code> \u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9 <code>kill<\/code>\uff0c\u6700\u7ec8\u6267\u884c\u7684\u547d\u4ee4\u4e3a\uff1a\n<pre><code class=\"language-bash line-numbers\">kill 123 124 125 126\n<\/code><\/pre>\n<\/li>\n<\/ul>\n<hr \/>\n<h4><strong>\u6700\u7ec8\u6548\u679c<\/strong><\/h4>\n<ul>\n<li>\u547d\u4ee4\u4f1a\u5411\u8fdb\u7a0b <code>123<\/code>\u3001<code>124<\/code>\u3001<code>125<\/code> \u548c <code>126<\/code> \u53d1\u9001 <code>SIGTERM<\/code> \u4fe1\u53f7\uff08\u9ed8\u8ba4\u4fe1\u53f7\uff09\uff0c\u8bf7\u6c42\u5b83\u4eec\u7ec8\u6b62\u3002<\/li>\n<li>\u5982\u679c\u8fd9\u4e9b\u8fdb\u7a0b\u6b63\u786e\u5904\u7406\u4e86 <code>SIGTERM<\/code> \u4fe1\u53f7\uff0c\u5b83\u4eec\u4f1a\u6267\u884c\u6e05\u7406\u64cd\u4f5c\u5e76\u9000\u51fa\u3002<\/li>\n<li>\u5982\u679c\u8fdb\u7a0b\u6ca1\u6709\u54cd\u5e94 <code>SIGTERM<\/code>\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>kill -9<\/code> \u5f3a\u5236\u7ec8\u6b62\uff1a\n<p>&#8220;`bash<br \/>\nkill -9 123 124 125 126<br \/>\n&lt;\/li&gt;<br \/>\n&lt;\/ul&gt;<\/p>\n<p>&lt;h2&gt;<br \/>\n&#8220;`<br \/>\n<\/h2>\n<\/details>\n<hr \/>\n<p><em>PS: \u6211\u53ea\u4f7f\u7528\u8fc7 nohup\uff0c\u5269\u4e0b\u7684\u6ca1\u4f7f\u7528\u8fc7\uff0c\u6b22\u8fce\u8865\u5145<\/em><\/p>\n<h2>2. \u4f7f\u7528 <code>screen<\/code><\/h2>\n<p><code>screen<\/code> \u662f\u4e00\u4e2a\u7ec8\u7aef\u591a\u8def\u590d\u7528\u5668\uff0c\u53ef\u4ee5\u8ba9\u4f60\u5728\u4e00\u4e2a\u4f1a\u8bdd\u4e2d\u8fd0\u884c\u591a\u4e2a\u7ec8\u7aef\u3002\u4f7f\u7528\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<ol>\n<li>\u542f\u52a8 <code>screen<\/code>\uff1a\n<pre><code class=\"language-bash line-numbers\">screen\n<\/code><\/pre>\n<\/li>\n<li>\u8fd0\u884c\u4f60\u7684 Python \u811a\u672c\uff1a\n<pre><code class=\"language-bash line-numbers\">python your_script.py\n<\/code><\/pre>\n<\/li>\n<li>\u6309 <code>Ctrl + A<\/code> \u7136\u540e\u6309 <code>D<\/code> \u952e\u5c06\u4f1a\u8bdd\u5206\u79bb\uff0c\u7a0b\u5e8f\u5c06\u7ee7\u7eed\u5728\u540e\u53f0\u8fd0\u884c\u3002<\/p>\n<\/li>\n<li>\n<p>\u4f60\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u91cd\u65b0\u8fde\u63a5\u5230\u4f1a\u8bdd\uff1a<\/p>\n<pre><code class=\"language-bash line-numbers\">screen -r\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<h2>3. \u4f7f\u7528 <code>tmux<\/code><\/h2>\n<p><code>tmux<\/code> \u548c <code>screen<\/code> \u7c7b\u4f3c\uff0c\u4e5f\u662f\u4e00\u4e2a\u7ec8\u7aef\u590d\u7528\u5de5\u5177\u3002\u4f7f\u7528\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<ol>\n<li>\u542f\u52a8 <code>tmux<\/code>\uff1a\n<pre><code class=\"language-bash line-numbers\">tmux\n<\/code><\/pre>\n<\/li>\n<li>\u8fd0\u884c\u4f60\u7684 Python \u811a\u672c\uff1a\n<pre><code class=\"language-bash line-numbers\">python your_script.py\n<\/code><\/pre>\n<\/li>\n<li>\u6309 <code>Ctrl + B<\/code> \u7136\u540e\u6309 <code>D<\/code> \u952e\u5c06\u4f1a\u8bdd\u5206\u79bb\u3002<\/p>\n<\/li>\n<li>\n<p>\u91cd\u65b0\u8fde\u63a5\u5230\u4f1a\u8bdd\uff1a<\/p>\n<pre><code class=\"language-bash line-numbers\">tmux attach\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<h2>4. \u4f7f\u7528 <code>supervisor<\/code><\/h2>\n<p>\u5982\u679c\u4f60\u9700\u8981\u66f4\u590d\u6742\u7684\u8fdb\u7a0b\u7ba1\u7406\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528 <code>supervisor<\/code>\u3002\u5b89\u88c5\u548c\u914d\u7f6e\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<ol>\n<li>\u5b89\u88c5 <code>supervisor<\/code>\uff1a\n<pre><code class=\"language-bash line-numbers\">pip install supervisor\n<\/code><\/pre>\n<\/li>\n<li>\u521b\u5efa\u914d\u7f6e\u6587\u4ef6\uff0c\u4f8b\u5982 <code>your_script.conf<\/code>\uff1a\n<pre><code class=\"line-numbers\">[program:your_script]\ncommand=python \/path\/to\/your_script.py\nautostart=true\nautorestart=true\n<\/code><\/pre>\n<\/li>\n<li>\u542f\u52a8 <code>supervisor<\/code>\uff1a\n<pre><code class=\"language-bash line-numbers\">supervisord -c \/path\/to\/your_script.conf\n<\/code><\/pre>\n<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>\u8fdc\u7a0b\u8fd0\u884c\u6587\u4ef6 Keruone 1. \u4f7f\u7528 nohup 1.1 \u4e00\u822c\u4f7f\u7528 \u5047\u8bbe\u6709\u6587\u4ef6 test.py\uff0c\u9700\u8981\u53c2\u6570  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[17],"tags":[],"class_list":["post-235","post","type-post","status-publish","format-standard","hentry","category-small-tools"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/blog.keruone.cn\/index.php\/wp-json\/wp\/v2\/posts\/235","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.keruone.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.keruone.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.keruone.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.keruone.cn\/index.php\/wp-json\/wp\/v2\/comments?post=235"}],"version-history":[{"count":16,"href":"https:\/\/blog.keruone.cn\/index.php\/wp-json\/wp\/v2\/posts\/235\/revisions"}],"predecessor-version":[{"id":263,"href":"https:\/\/blog.keruone.cn\/index.php\/wp-json\/wp\/v2\/posts\/235\/revisions\/263"}],"wp:attachment":[{"href":"https:\/\/blog.keruone.cn\/index.php\/wp-json\/wp\/v2\/media?parent=235"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.keruone.cn\/index.php\/wp-json\/wp\/v2\/categories?post=235"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.keruone.cn\/index.php\/wp-json\/wp\/v2\/tags?post=235"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}