{"id":198662,"date":"2024-11-14T21:55:48","date_gmt":"2024-11-14T13:55:48","guid":{"rendered":"https:\/\/server.hk\/cnblog\/198662\/"},"modified":"2024-11-14T21:55:48","modified_gmt":"2024-11-14T13:55:48","slug":"%e3%80%8clinux%e7%b3%bb%e7%b5%b1%e5%bf%ab%e9%80%9f%e9%85%8d%e7%bd%ae%e8%87%aa%e5%95%9f%e5%8b%95%e3%80%8d-linux%e9%85%8d%e7%bd%ae%e8%87%aa%e5%95%9f%e5%8b%95","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/198662\/","title":{"rendered":"\u300cLinux\u7cfb\u7d71\u5feb\u901f\u914d\u7f6e\u81ea\u555f\u52d5\u300d (linux\u914d\u7f6e\u81ea\u555f\u52d5)"},"content":{"rendered":"<h1 id=\"linux%e7%b3%bb%e7%b5%b1%e5%bf%ab%e9%80%9f%e9%85%8d%e7%bd%ae%e8%87%aa%e5%95%9f%e5%8b%95-WixqxiIUuP\">Linux\u7cfb\u7d71\u5feb\u901f\u914d\u7f6e\u81ea\u555f\u52d5<\/h1>\n<p>\u5728Linux\u7cfb\u7d71\u4e2d\uff0c\u914d\u7f6e\u81ea\u555f\u52d5\u662f\u4e00\u9805\u91cd\u8981\u7684\u7ba1\u7406\u4efb\u52d9\uff0c\u7279\u5225\u662f\u5728\u4f3a\u670d\u5668\u74b0\u5883\u4e2d\u3002\u81ea\u555f\u52d5\u529f\u80fd\u53ef\u4ee5\u78ba\u4fdd\u5728\u7cfb\u7d71\u91cd\u555f\u5f8c\uff0c\u7279\u5b9a\u7684\u61c9\u7528\u7a0b\u5e8f\u6216\u670d\u52d9\u80fd\u5920\u81ea\u52d5\u555f\u52d5\uff0c\u5f9e\u800c\u63d0\u9ad8\u7cfb\u7d71\u7684\u53ef\u7528\u6027\u548c\u7a69\u5b9a\u6027\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u5728Linux\u7cfb\u7d71\u4e2d\u5feb\u901f\u914d\u7f6e\u81ea\u555f\u52d5\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u5be6\u7528\u7684\u793a\u4f8b\u548c\u6b65\u9a5f\u3002<\/p>\n<h2 id=\"%e8%87%aa%e5%95%9f%e5%8b%95%e7%9a%84%e5%9f%ba%e6%9c%ac%e6%a6%82%e5%bf%b5-WixqxiIUuP\">\u81ea\u555f\u52d5\u7684\u57fa\u672c\u6982\u5ff5<\/h2>\n<p>\u81ea\u555f\u52d5\u662f\u6307\u5728\u7cfb\u7d71\u555f\u52d5\u6642\uff0c\u81ea\u52d5\u904b\u884c\u7279\u5b9a\u7684\u7a0b\u5e8f\u6216\u670d\u52d9\u3002\u9019\u5c0d\u65bc\u4f3a\u670d\u5668\u4f86\u8aaa\u5c24\u70ba\u91cd\u8981\uff0c\u56e0\u70ba\u5b83\u53ef\u4ee5\u6e1b\u5c11\u4eba\u5de5\u5e72\u9810\uff0c\u78ba\u4fdd\u95dc\u9375\u670d\u52d9\u5728\u7cfb\u7d71\u91cd\u555f\u5f8c\u80fd\u5920\u6b63\u5e38\u904b\u884c\u3002Linux\u7cfb\u7d71\u901a\u5e38\u4f7f\u7528init\u7cfb\u7d71\u6216systemd\u4f86\u7ba1\u7406\u81ea\u555f\u52d5\u670d\u52d9\u3002<\/p>\n<h2 id=\"%e4%bd%bf%e7%94%a8systemd%e9%85%8d%e7%bd%ae%e8%87%aa%e5%95%9f%e5%8b%95-WixqxiIUuP\">\u4f7f\u7528systemd\u914d\u7f6e\u81ea\u555f\u52d5<\/h2>\n<p>\u5728\u73fe\u4ee3\u7684Linux\u767c\u884c\u7248\u4e2d\uff0csystemd\u662f\u6700\u5e38\u7528\u7684\u521d\u59cb\u5316\u7cfb\u7d71\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528systemd\u914d\u7f6e\u81ea\u555f\u52d5\u7684\u6b65\u9a5f\uff1a<\/p>\n<h3 id=\"1-%e5%89%b5%e5%bb%ba%e6%9c%8d%e5%8b%99%e5%96%ae%e5%85%83%e6%96%87%e4%bb%b6-WixqxiIUuP\">1. \u5275\u5efa\u670d\u52d9\u55ae\u5143\u6587\u4ef6<\/h3>\n<p>\u9996\u5148\uff0c\u60a8\u9700\u8981\u5275\u5efa\u4e00\u500b\u670d\u52d9\u55ae\u5143\u6587\u4ef6\uff0c\u9019\u500b\u6587\u4ef6\u901a\u5e38\u4f4d\u65bc\/etc\/systemd\/system\u76ee\u9304\u4e0b\u3002\u5047\u8a2d\u6211\u5011\u8981\u914d\u7f6e\u4e00\u500b\u540d\u70bamyapp\u7684\u61c9\u7528\u7a0b\u5e8f\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5275\u5efa\u670d\u52d9\u55ae\u5143\u6587\u4ef6\uff1a<\/p>\n<pre><code>sudo nano \/etc\/systemd\/system\/myapp.service<\/code><\/pre>\n<p>\u5728\u6587\u4ef6\u4e2d\uff0c\u60a8\u53ef\u4ee5\u6dfb\u52a0\u4ee5\u4e0b\u5167\u5bb9\uff1a<\/p>\n<pre><code>[Unit]\nDescription=My Application\n\n[Service]\nExecStart=\/usr\/bin\/myapp\nRestart=always\n\n[Install]\nWantedBy=multi-user.target<\/code><\/pre>\n<p>\u9019\u88e1\u7684ExecStart\u6307\u5b9a\u4e86\u61c9\u7528\u7a0b\u5e8f\u7684\u555f\u52d5\u547d\u4ee4\uff0cRestart=always\u8868\u793a\u5982\u679c\u61c9\u7528\u7a0b\u5e8f\u5d29\u6f70\uff0csystemd\u6703\u81ea\u52d5\u91cd\u555f\u5b83\u3002<\/p>\n<h3 id=\"2-%e9%87%8d%e6%96%b0%e5%8a%a0%e8%bc%89systemd%e9%85%8d%e7%bd%ae-WixqxiIUuP\">2. \u91cd\u65b0\u52a0\u8f09systemd\u914d\u7f6e<\/h3>\n<p>\u5275\u5efa\u5b8c\u670d\u52d9\u55ae\u5143\u6587\u4ef6\u5f8c\uff0c\u60a8\u9700\u8981\u91cd\u65b0\u52a0\u8f09systemd\u7684\u914d\u7f6e\uff0c\u4ee5\u4fbf\u5b83\u80fd\u5920\u8b58\u5225\u65b0\u7684\u670d\u52d9\uff1a<\/p>\n<pre><code>sudo systemctl daemon-reload<\/code><\/pre>\n<h3 id=\"3-%e5%95%9f%e7%94%a8%e6%9c%8d%e5%8b%99%e8%87%aa%e5%95%9f%e5%8b%95-WixqxiIUuP\">3. \u555f\u7528\u670d\u52d9\u81ea\u555f\u52d5<\/h3>\n<p>\u63a5\u4e0b\u4f86\uff0c\u60a8\u9700\u8981\u555f\u7528\u8a72\u670d\u52d9\uff0c\u4f7f\u5176\u5728\u7cfb\u7d71\u555f\u52d5\u6642\u81ea\u52d5\u904b\u884c\uff1a<\/p>\n<pre><code>sudo systemctl enable myapp.service<\/code><\/pre>\n<h3 id=\"4-%e5%95%9f%e5%8b%95%e6%9c%8d%e5%8b%99-WixqxiIUuP\">4. \u555f\u52d5\u670d\u52d9<\/h3>\n<p>\u6700\u5f8c\uff0c\u60a8\u53ef\u4ee5\u624b\u52d5\u555f\u52d5\u670d\u52d9\u4ee5\u78ba\u4fdd\u5176\u6b63\u5e38\u904b\u884c\uff1a<\/p>\n<pre><code>sudo systemctl start myapp.service<\/code><\/pre>\n<h2 id=\"%e4%bd%bf%e7%94%a8init-d%e9%85%8d%e7%bd%ae%e8%87%aa%e5%95%9f%e5%8b%95-WixqxiIUuP\">\u4f7f\u7528init.d\u914d\u7f6e\u81ea\u555f\u52d5<\/h2>\n<p>\u5c0d\u65bc\u4e00\u4e9b\u8f03\u820a\u7684Linux\u767c\u884c\u7248\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u4f7f\u7528init.d\u4f86\u914d\u7f6e\u81ea\u555f\u52d5\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528init.d\u914d\u7f6e\u81ea\u555f\u52d5\u7684\u6b65\u9a5f\uff1a<\/p>\n<h3 id=\"1-%e5%89%b5%e5%bb%ba%e5%95%9f%e5%8b%95%e8%85%b3%e6%9c%ac-WixqxiIUuP\">1. \u5275\u5efa\u555f\u52d5\u8173\u672c<\/h3>\n<p>\u60a8\u9700\u8981\u5728\/etc\/init.d\u76ee\u9304\u4e0b\u5275\u5efa\u4e00\u500b\u555f\u52d5\u8173\u672c\u3002\u4f8b\u5982\uff0c\u5275\u5efa\u4e00\u500b\u540d\u70bamyapp\u7684\u8173\u672c\uff1a<\/p>\n<pre><code>sudo nano \/etc\/init.d\/myapp<\/code><\/pre>\n<p>\u5728\u8173\u672c\u4e2d\uff0c\u60a8\u53ef\u4ee5\u6dfb\u52a0\u4ee5\u4e0b\u5167\u5bb9\uff1a<\/p>\n<pre><code>#!\/bin\/bash\n# \/etc\/init.d\/myapp\n\ncase \"$1\" in\n  start)\n    \/usr\/bin\/myapp &amp;\n    ;;\n  stop)\n    killall myapp\n    ;;\n  *)\n    echo \"Usage: \/etc\/init.d\/myapp {start|stop}\"\n    exit 1\n    ;;\nesac\n\nexit 0<\/code><\/pre>\n<h3 id=\"2-%e8%a8%ad%e7%bd%ae%e8%85%b3%e6%9c%ac%e5%8f%af%e5%9f%b7%e8%a1%8c%e6%ac%8a%e9%99%90-WixqxiIUuP\">2. \u8a2d\u7f6e\u8173\u672c\u53ef\u57f7\u884c\u6b0a\u9650<\/h3>\n<p>\u78ba\u4fdd\u8173\u672c\u5177\u6709\u53ef\u57f7\u884c\u6b0a\u9650\uff1a<\/p>\n<pre><code>sudo chmod +x \/etc\/init.d\/myapp<\/code><\/pre>\n<h3 id=\"3-%e4%bd%bf%e7%94%a8update-rc-d%e6%b7%bb%e5%8a%a0%e8%87%aa%e5%95%9f%e5%8b%95-WixqxiIUuP\">3. \u4f7f\u7528update-rc.d\u6dfb\u52a0\u81ea\u555f\u52d5<\/h3>\n<p>\u6700\u5f8c\uff0c\u4f7f\u7528update-rc.d\u547d\u4ee4\u5c07\u8173\u672c\u6dfb\u52a0\u5230\u81ea\u555f\u52d5\u5217\u8868\uff1a<\/p>\n<pre><code>sudo update-rc.d myapp defaults<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-WixqxiIUuP\">\u7e3d\u7d50<\/h2>\n<p>\u914d\u7f6eLinux\u7cfb\u7d71\u7684\u81ea\u555f\u52d5\u529f\u80fd\u53ef\u4ee5\u986f\u8457\u63d0\u9ad8\u7cfb\u7d71\u7684\u7a69\u5b9a\u6027\u548c\u53ef\u7528\u6027\u3002\u7121\u8ad6\u662f\u4f7f\u7528systemd\u9084\u662finit.d\uff0c\u4e86\u89e3\u5982\u4f55\u6b63\u78ba\u914d\u7f6e\u81ea\u555f\u52d5\u90fd\u662f\u6bcf\u4f4d\u7cfb\u7d71\u7ba1\u7406\u54e1\u5fc5\u5099\u7684\u6280\u80fd\u3002\u900f\u904e\u4e0a\u8ff0\u6b65\u9a5f\uff0c\u60a8\u53ef\u4ee5\u8f15\u9b06\u5730\u70ba\u60a8\u7684\u61c9\u7528\u7a0b\u5e8f\u8a2d\u7f6e\u81ea\u555f\u52d5\uff0c\u78ba\u4fdd\u5176\u5728\u7cfb\u7d71\u91cd\u555f\u5f8c\u80fd\u5920\u81ea\u52d5\u904b\u884c\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7a69\u5b9a\u7684 <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u89e3\u6c7a\u65b9\u6848\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u60a8\u7684\u9700\u6c42\u3002\u7121\u8ad6\u662f\u96f2\u4f3a\u670d\u5668\u9084\u662f\u5c08\u7528\u4f3a\u670d\u5668\uff0c\u6211\u5011\u90fd\u80fd\u70ba\u60a8\u63d0\u4f9b\u6700\u4f73\u7684\u670d\u52d9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5feb\u901f\u5b78\u7fd2\u5982\u4f55\u5728Linux\u7cfb\u7d71\u4e2d\u914d\u7f6e\u81ea\u555f\u52d5\uff0c\u7c21\u5316\u555f\u52d5\u904e\u7a0b\uff0c\u63d0\u5347\u7cfb\u7d71\u6548\u7387\u8207\u7ba1\u7406\u4fbf\u5229\u6027\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4924],"tags":[],"class_list":["post-198662","post","type-post","status-publish","format-standard","hentry","category-setup-tutorials"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/198662","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=198662"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/198662\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=198662"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=198662"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=198662"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}