{"id":602,"date":"2017-03-23T22:00:32","date_gmt":"2017-03-23T14:00:32","guid":{"rendered":"https:\/\/carlos.aboutmy.info\/blog\/?p=602"},"modified":"2017-03-24T15:43:08","modified_gmt":"2017-03-24T07:43:08","slug":"%e7%94%b1-init-%e5%88%b0-systemd","status":"publish","type":"post","link":"https:\/\/carlos.aboutmy.info\/blog\/?p=602","title":{"rendered":"\u7531 init \u5230 systemd"},"content":{"rendered":"<p>\u5728\u96fb\u8166\u4e16\u754c\uff0c\u96d6\u8981\u6709\u4e00\u4e9b\u7a0b\u5f0f\u4f86\u7ba1\u7406\u5176\u4ed6\u7a0b\u5f0f\u7684\u300c\u555f\u52d5\u300d\u3001\u300c\u91cd\u555f\u300d\u53ca\u300c\u505c\u6b62\u300d\u3002<\/p>\n<p>\u5728 Windows\u3001Linux\u3001Matrix \ud83d\ude42 \u90fd\u6709\u3002Windows \u53eb\u9019\u505a &#8220;Services&#8221;\u3002\u958b\u653e\u7684 Linux \u5c31\u6709\u6578\u7a2e\u7a0b\u5f0f\u4f86\u505a\u9019\u7a2e\u4e8b\u3002\u6bd4\u8f03\u5e38\u7528\u7684\u5c31\u6709 init\u3001upstart \u53ca systemd\u3002<\/p>\n<p>\u73fe\u5728\u77e5\u540d\u7684\u767c\u884c\u7248\uff0c\u5982 Fedora\u3001RHEL\u3001Ubuntu\u3001Debian \u90fd\u5df2\u628a init \u6539\u70ba\u7528 systemd\u3002\u9019\u500b\u8f49\u63db\u65e9\u524d\u5f15\u767c\u958b\u6e90\u754c\u5f88\u5927\u722d\u8b70\uff0c\u539f\u56e0 systemd \u6709\u9055 Unix Philosophy\uff1a<\/p>\n<ul>\n<li>Write programs that do one thing and do it well.<\/li>\n<li>Write programs to work together.<\/li>\n<li>Write programs to handle text streams, because that is a universal interface<\/li>\n<\/ul>\n<p>Systemd \u505a\u592a\u591a\u91ce\u4e86\uff0c\u4e0d\u55ae\u555f\u52d5\u6216\u91cd\u555f\u4e00\u500b\u7a0b\u5f0f\u9019\u6a23\u7c21\u55ae\u3002Systemd \u4e0d\u662f\u7d14 text process\u3002\u5c0d\u4e00\u822c users \u4f86\u8aaa systemd \u597d\u8655\u7684\u78ba\u5927\u904e\u7f3a\u9ede\uff0c\u7279\u5225\u80fd\u4e26\u884c\u555f\u52d5\u958b\u6a5f\u7a0b\u5f0f\uff0c\u4ee4\u958b\u6a5f\u6642\u9593\u5927\u5927\u7e2e\u77ed\uff0c\u53e6\u5916\u5f88\u591a software \u5982 GNOME3 \u4f9d\u8cf4 systemd\uff0c\u6240\u4ee5\u597d\u591a\u767c\u884c\u7248\u90fd\u6539\u7528\u4e86 systemd\u3002<\/p>\n<p>\u4f86\u770b init script\uff0c\u901a\u5e38\u5b83\u662f BASH script\uff0c\u542b\u6709 programming \u5143\u7d20\uff0c\u7528 case \u4f86\u9078\u64c7 start\u3001stop \u53ca restart command\uff0c\u800c restart \u901a\u5e38\u53ea\u662f stop \u5f8c\u518d start\u3002<\/p>\n<p>[bash]<br \/>\nstart(){<br \/>\n# &#8230;<br \/>\n}<\/p>\n<p>stop(){<br \/>\n# &#8230;<br \/>\n}<\/p>\n<p>case &quot;$1&quot; in<br \/>\n  start)<br \/>\n    start<br \/>\n    ;;<br \/>\n  stop)<br \/>\n    stop<br \/>\n    ;;<br \/>\n  restart)<br \/>\n    stop<br \/>\n    sleep 2<br \/>\n    start<br \/>\n    ;;<br \/>\n  *)<br \/>\n    echo &quot;Usage: $0 {start | stop | restart}&quot;<br \/>\nesac<br \/>\n[\/bash]<\/p>\n<p>\u800c systemd script \u5247\u7c21\u55ae\u5f97\u591a\uff0c\u6c92\u6709 programming \u5143\u7d20\uff0c\u4ee5 <a href=\"https:\/\/github.com\/appleboy\/gorush\">gorush<\/a> \u70ba\u4f8b\u5b50\uff0csystemd script \u5c31\u662f\u9019\u9ebc\u7c21\u55ae\u6613\u660e\u3002<br \/>\n[shell]<br \/>\n[Unit]<br \/>\nDescription=A push notification micro server using Gin framework written in Go.<br \/>\nAfter=network.target<\/p>\n<p>[Service]<br \/>\n# Run Grunt before starting the server (optional)<br \/>\n#<\/p>\n<p># Start the js-file starting the express server<br \/>\nExecStart=\/usr\/local\/bin\/gorush -c \/etc\/gorush\/gorush.yaml<br \/>\nWorkingDirectory=\/var\/lib\/gorush<br \/>\nRestart=always<br \/>\nRestartSec=5<br \/>\nStandardOutput=syslog<br \/>\nStandardError=syslog<br \/>\nSyslogIdentifier=gorush<br \/>\n# Change to a non-root user (optional, but recommended)<br \/>\nUser=gorush<br \/>\nGroup=gorush<br \/>\n# Set environment options<br \/>\n#Environment=<\/p>\n<p>[Install]<br \/>\nWantedBy=multi-user.target<br \/>\n[\/shell]<\/p>\n<p>\u628a\u4ee5\u4e0a script \u653e\u53bb <code>\/lib\/systemd\/system\/gorush.service<\/code> \u518d\u7528 systemctl \u8a2d\u5b9a\u4fbf\u53ef\u3002<br \/>\n<code language=\"bash\"><br \/>\n$ sudo systemctl daemon-reload<br \/>\n$ sudo systemctl enable gorush<br \/>\n$ sudo systemctl start gorush<br \/>\n<\/code><\/p>\n<p>\u6700\u5f8c\uff0csystemd \u4ee4\u4eba\u8208\u596e\u7684\u662f\uff0c\u4ee5\u5f80\u6211\u5011\u53ef\u80fd\u9700\u8981\u5bb6\u4e00\u500b <code>crontab<\/code> \u4f86\u4e0d\u65b7 monitor \u500b process \u6709\u6c92\u6709\u639b\u6389\uff0c\u9019\u500b systemd \u90fd\u5e6b\u6211\u5011\u505a\u4e86\u3002<\/p>\n<p><code>Restart=always<\/code><\/p>\n<p>\u5c31\u662f\u7576 process \u7121\u8ad6\u54ea\u7a2e\u539f\u56e0\uff0c\u81ea\u7136\u6216\u4e0d\u6b63\u5e38\u639b\u6389\uff0c\u90fd\u6703 restart\u3002\u9664\u4e86 always\uff0c\u9084\u6709 no\u3001on-success\u3001on-failure\u3001on-abnormal\u3001on-abort\u3001on-watchdog\uff0c\u8a73\u7d30\u53ef\u770b<a href=\"https:\/\/www.freedesktop.org\/software\/systemd\/man\/systemd.service.html#Restart=\">\u9019\u88e1<\/a>\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u96fb\u8166\u4e16\u754c\uff0c\u96d6\u8981\u6709\u4e00\u4e9b\u7a0b\u5f0f\u4f86\u7ba1\u7406\u5176\u4ed6\u7a0b\u5f0f\u7684\u300c\u555f\u52d5\u300d\u3001\u300c\u91cd\u555f\u300d\u53ca\u300c\u505c\u6b62\u300d\u3002 \u5728 Windows\u3001Linux\u3001Matrix \ud83d\ude42 \u90fd\u6709\u3002Windows \u53eb\u9019\u505a &#8220;Services&#8221;\u3002\u958b\u653e\u7684 Linux \u5c31\u6709\u6578\u7a2e\u7a0b\u5f0f\u4f86\u505a\u9019\u7a2e\u4e8b\u3002\u6bd4\u8f03\u5e38\u7528\u7684\u5c31\u6709 init\u3001upstart \u53ca systemd\u3002 \u73fe\u5728\u77e5\u540d\u7684\u767c\u884c\u7248\uff0c\u5982 Fedora\u3001RHEL\u3001Ubuntu\u3001Debian \u90fd\u5df2\u628a init \u6539\u70ba\u7528 systemd\u3002\u9019\u500b\u8f49\u63db\u65e9\u524d\u5f15\u767c\u958b\u6e90\u754c\u5f88\u5927\u722d\u8b70\uff0c\u539f\u56e0 systemd \u6709\u9055 Unix Philosophy\uff1a Write programs that do one thing and do it well. Write programs to work together. Write programs to handle text streams, because that is a universal interface Systemd \u505a\u592a\u591a\u91ce\u4e86\uff0c\u4e0d\u55ae\u555f\u52d5\u6216\u91cd\u555f\u4e00\u500b\u7a0b\u5f0f\u9019\u6a23\u7c21\u55ae\u3002Systemd \u4e0d\u662f\u7d14 text process\u3002\u5c0d\u4e00\u822c users\u2026 <span class=\"read-more\"><a href=\"https:\/\/carlos.aboutmy.info\/blog\/?p=602\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[17],"tags":[],"_links":{"self":[{"href":"https:\/\/carlos.aboutmy.info\/blog\/index.php?rest_route=\/wp\/v2\/posts\/602"}],"collection":[{"href":"https:\/\/carlos.aboutmy.info\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/carlos.aboutmy.info\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/carlos.aboutmy.info\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/carlos.aboutmy.info\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=602"}],"version-history":[{"count":7,"href":"https:\/\/carlos.aboutmy.info\/blog\/index.php?rest_route=\/wp\/v2\/posts\/602\/revisions"}],"predecessor-version":[{"id":609,"href":"https:\/\/carlos.aboutmy.info\/blog\/index.php?rest_route=\/wp\/v2\/posts\/602\/revisions\/609"}],"wp:attachment":[{"href":"https:\/\/carlos.aboutmy.info\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=602"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/carlos.aboutmy.info\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=602"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/carlos.aboutmy.info\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=602"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}