{"id":778,"date":"2017-03-27T11:48:31","date_gmt":"2017-03-27T03:48:31","guid":{"rendered":"http:\/\/www.sulabs.net\/?p=778"},"modified":"2017-03-27T12:05:18","modified_gmt":"2017-03-27T04:05:18","slug":"systemd-%e5%85%a5%e9%97%a8%e6%95%99%e7%a8%8b%ef%bc%9a%e5%91%bd%e4%bb%a4%e7%af%87","status":"publish","type":"post","link":"https:\/\/www.sulabs.net\/?p=778","title":{"rendered":"Systemd \u5165\u95e8\u6559\u7a0b\uff1a\u547d\u4ee4\u7bc7"},"content":{"rendered":"<p>\u8f6c\u81eahttp:\/\/www.ruanyifeng.com\/blog\/2016\/03\/systemd-tutorial-commands.html<br \/>\n\u5411\u539f\u4f5c\u8005\u8868\u793a\u611f\u8c22.<\/p>\n<p>Systemd \u662f Linux \u7cfb\u7edf\u5de5\u5177\uff0c\u7528\u6765\u542f\u52a8<a href=\"http:\/\/www.ruanyifeng.com\/blog\/2016\/02\/linux-daemon.html\" target=\"_blank\">\u5b88\u62a4\u8fdb\u7a0b<\/a>\uff0c\u5df2\u6210\u4e3a\u5927\u591a\u6570\u53d1\u884c\u7248\u7684\u6807\u51c6\u914d\u7f6e\u3002<\/p>\n<p>\u672c\u6587\u4ecb\u7ecd\u5b83\u7684\u57fa\u672c\u7528\u6cd5\uff0c\u5206\u4e3a\u4e0a\u4e0b\u4e24\u7bc7\u3002\u4eca\u5929\u4ecb\u7ecd\u5b83\u7684\u4e3b\u8981\u547d\u4ee4\uff0c<a href=\"http:\/\/www.ruanyifeng.com\/blog\/2016\/03\/systemd-tutorial-part-two.html\" target=\"_blank\">\u4e0b\u4e00\u7bc7<\/a>\u4ecb\u7ecd\u5982\u4f55\u7528\u4e8e\u5b9e\u6218\u3002<br \/>\n<!--more--><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-779\" title=\"\" src=\"https:\/\/www.sulabs.net\/wp-content\/uploads\/2017\/03\/bg2016030701.gif\" alt=\"\" width=\"600\" height=\"195\"><\/p>\n<h2>\u4e00\u3001\u7531\u6765<\/h2>\n<p>\u5386\u53f2\u4e0a\uff0c<a href=\"http:\/\/www.ruanyifeng.com\/blog\/2013\/08\/linux_boot_process.html\" target=\"_blank\">Linux \u7684\u542f\u52a8<\/a>\u4e00\u76f4\u91c7\u7528<a href=\"https:\/\/en.wikipedia.org\/wiki\/Init\" target=\"_blank\"><code>init<\/code><\/a>\u8fdb\u7a0b\u3002<\/p>\n<p>\u4e0b\u9762\u7684\u547d\u4ee4\u7528\u6765\u542f\u52a8\u670d\u52a1\u3002<\/p>\n<blockquote>\n<pre class=\" language-bash\">$ sudo \/etc\/init.d\/apache2 start\r\n# \u6216\u8005\r\n$ service apache2 start<\/pre>\n<\/blockquote>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u6709\u4e24\u4e2a\u7f3a\u70b9\u3002<\/p>\n<p>\u4e00\u662f\u542f\u52a8\u65f6\u95f4\u957f\u3002<code>init<\/code>\u8fdb\u7a0b\u662f\u4e32\u884c\u542f\u52a8\uff0c\u53ea\u6709\u524d\u4e00\u4e2a\u8fdb\u7a0b\u542f\u52a8\u5b8c\uff0c\u624d\u4f1a\u542f\u52a8\u4e0b\u4e00\u4e2a\u8fdb\u7a0b\u3002<\/p>\n<p>\u4e8c\u662f\u542f\u52a8\u811a\u672c\u590d\u6742\u3002<code>init<\/code>\u8fdb\u7a0b\u53ea\u662f\u6267\u884c\u542f\u52a8\u811a\u672c\uff0c\u4e0d\u7ba1\u5176\u4ed6\u4e8b\u60c5\u3002\u811a\u672c\u9700\u8981\u81ea\u5df1\u5904\u7406\u5404\u79cd\u60c5\u51b5\uff0c\u8fd9\u5f80\u5f80\u4f7f\u5f97\u811a\u672c\u53d8\u5f97\u5f88\u957f\u3002<\/p>\n<h2>\u4e8c\u3001Systemd \u6982\u8ff0<\/h2>\n<p>Systemd \u5c31\u662f\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e9b\u95ee\u9898\u800c\u8bde\u751f\u7684\u3002\u5b83\u7684\u8bbe\u8ba1\u76ee\u6807\u662f\uff0c\u4e3a\u7cfb\u7edf\u7684\u542f\u52a8\u548c\u7ba1\u7406\u63d0\u4f9b\u4e00\u5957\u5b8c\u6574\u7684\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<p>\u6839\u636e Linux \u60ef\u4f8b\uff0c\u5b57\u6bcd<code>d<\/code>\u662f\u5b88\u62a4\u8fdb\u7a0b\uff08daemon\uff09\u7684\u7f29\u5199\u3002 Systemd \u8fd9\u4e2a\u540d\u5b57\u7684\u542b\u4e49\uff0c\u5c31\u662f\u5b83\u8981\u5b88\u62a4\u6574\u4e2a\u7cfb\u7edf\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-780\" title=\"\" src=\"https:\/\/www.sulabs.net\/wp-content\/uploads\/2017\/03\/bg2016030702.jpg\" alt=\"\" width=\"500\" height=\"320\" srcset=\"https:\/\/www.sulabs.net\/wp-content\/uploads\/2017\/03\/bg2016030702.jpg 500w, https:\/\/www.sulabs.net\/wp-content\/uploads\/2017\/03\/bg2016030702-300x192.jpg 300w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/p>\n<p>\uff08\u4e0a\u56fe\u4e3a Systemd \u4f5c\u8005 <a href=\"https:\/\/en.wikipedia.org\/wiki\/Lennart_Poettering\" target=\"_blank\">Lennart Poettering<\/a>\uff09<\/p>\n<p>\u4f7f\u7528\u4e86 Systemd\uff0c\u5c31\u4e0d\u9700\u8981\u518d\u7528<code>init<\/code>\u4e86\u3002Systemd \u53d6\u4ee3\u4e86<code>initd<\/code>\uff0c\u6210\u4e3a\u7cfb\u7edf\u7684\u7b2c\u4e00\u4e2a\u8fdb\u7a0b\uff08PID \u7b49\u4e8e 1\uff09\uff0c\u5176\u4ed6\u8fdb\u7a0b\u90fd\u662f\u5b83\u7684\u5b50\u8fdb\u7a0b\u3002<\/p>\n<blockquote>\n<pre class=\" language-bash\">$ systemctl --version<\/pre>\n<\/blockquote>\n<p>\u4e0a\u9762\u7684\u547d\u4ee4\u67e5\u770b Systemd \u7684\u7248\u672c\u3002<\/p>\n<p>Systemd \u7684\u4f18\u70b9\u662f\u529f\u80fd\u5f3a\u5927\uff0c\u4f7f\u7528\u65b9\u4fbf\uff0c\u7f3a\u70b9\u662f\u4f53\u7cfb\u5e9e\u5927\uff0c\u975e\u5e38\u590d\u6742\u3002\u4e8b\u5b9e\u4e0a\uff0c\u73b0\u5728\u8fd8\u6709\u5f88\u591a\u4eba\u53cd\u5bf9\u4f7f\u7528 Systemd\uff0c\u7406\u7531\u5c31\u662f\u5b83\u8fc7\u4e8e\u590d\u6742\uff0c\u4e0e\u64cd\u4f5c\u7cfb\u7edf\u7684\u5176\u4ed6\u90e8\u5206\u5f3a\u8026\u5408\uff0c\u8fdd\u53cd\"keep simple, keep stupid\"\u7684<a href=\"http:\/\/www.ruanyifeng.com\/blog\/2009\/06\/unix_philosophy.html\" target=\"_blank\">Unix \u54f2\u5b66<\/a>\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-781\" title=\"\" src=\"https:\/\/www.sulabs.net\/wp-content\/uploads\/2017\/03\/bg2016030703.png\" alt=\"\" width=\"720\" height=\"405\" srcset=\"https:\/\/www.sulabs.net\/wp-content\/uploads\/2017\/03\/bg2016030703.png 720w, https:\/\/www.sulabs.net\/wp-content\/uploads\/2017\/03\/bg2016030703-300x169.png 300w\" sizes=\"auto, (max-width: 720px) 100vw, 720px\" \/><\/p>\n<p>\uff08\u4e0a\u56fe\u4e3a Systemd \u67b6\u6784\u56fe\uff09<\/p>\n<h2>\u4e09\u3001\u7cfb\u7edf\u7ba1\u7406<\/h2>\n<p>Systemd \u5e76\u4e0d\u662f\u4e00\u4e2a\u547d\u4ee4\uff0c\u800c\u662f\u4e00\u7ec4\u547d\u4ee4\uff0c\u6d89\u53ca\u5230\u7cfb\u7edf\u7ba1\u7406\u7684\u65b9\u65b9\u9762\u9762\u3002<\/p>\n<h3>3.1 systemctl<\/h3>\n<p><code>systemctl<\/code>\u662f Systemd \u7684\u4e3b\u547d\u4ee4\uff0c\u7528\u4e8e\u7ba1\u7406\u7cfb\u7edf\u3002<\/p>\n<blockquote>\n<pre class=\" language-bash\"># \u91cd\u542f\u7cfb\u7edf\r\n$ sudo systemctl reboot\r\n\r\n# \u5173\u95ed\u7cfb\u7edf\uff0c\u5207\u65ad\u7535\u6e90\r\n$ sudo systemctl poweroff\r\n\r\n# CPU\u505c\u6b62\u5de5\u4f5c\r\n$ sudo systemctl halt\r\n\r\n# \u6682\u505c\u7cfb\u7edf\r\n$ sudo systemctl suspend\r\n\r\n# \u8ba9\u7cfb\u7edf\u8fdb\u5165\u51ac\u7720\u72b6\u6001\r\n$ sudo systemctl hibernate\r\n\r\n# \u8ba9\u7cfb\u7edf\u8fdb\u5165\u4ea4\u4e92\u5f0f\u4f11\u7720\u72b6\u6001\r\n$ sudo systemctl hybrid-sleep\r\n\r\n# \u542f\u52a8\u8fdb\u5165\u6551\u63f4\u72b6\u6001\uff08\u5355\u7528\u6237\u72b6\u6001\uff09\r\n$ sudo systemctl rescue<\/pre>\n<\/blockquote>\n<h3>3.2 systemd-analyze<\/h3>\n<p><code>systemd-analyze<\/code>\u547d\u4ee4\u7528\u4e8e\u67e5\u770b\u542f\u52a8\u8017\u65f6\u3002<\/p>\n<blockquote>\n<pre class=\" language-bash\"># \u67e5\u770b\u542f\u52a8\u8017\u65f6\r\n$ systemd-analyze \r\n\r\n# \u67e5\u770b\u6bcf\u4e2a\u670d\u52a1\u7684\u542f\u52a8\u8017\u65f6\r\n$ systemd-analyze blame\r\n\r\n# \u663e\u793a\u7011\u5e03\u72b6\u7684\u542f\u52a8\u8fc7\u7a0b\u6d41\r\n$ systemd-analyze critical-chain\r\n\r\n# \u663e\u793a\u6307\u5b9a\u670d\u52a1\u7684\u542f\u52a8\u6d41\r\n$ systemd-analyze critical-chain atd.service<\/pre>\n<\/blockquote>\n<h3>3.3 hostnamectl<\/h3>\n<p><code>hostnamectl<\/code>\u547d\u4ee4\u7528\u4e8e\u67e5\u770b\u5f53\u524d\u4e3b\u673a\u7684\u4fe1\u606f\u3002<\/p>\n<blockquote>\n<pre class=\" language-bash\"># \u663e\u793a\u5f53\u524d\u4e3b\u673a\u7684\u4fe1\u606f\r\n$ hostnamectl\r\n\r\n# \u8bbe\u7f6e\u4e3b\u673a\u540d\u3002\r\n$ sudo hostnamectl set-hostname rhel7<\/pre>\n<\/blockquote>\n<h3>3.4 localectl<\/h3>\n<p><code>localectl<\/code>\u547d\u4ee4\u7528\u4e8e\u67e5\u770b\u672c\u5730\u5316\u8bbe\u7f6e\u3002<\/p>\n<blockquote>\n<pre class=\" language-bash\"># \u67e5\u770b\u672c\u5730\u5316\u8bbe\u7f6e\r\n$ localectl\r\n\r\n# \u8bbe\u7f6e\u672c\u5730\u5316\u53c2\u6570\u3002\r\n$ sudo localectl set-locale LANG=en_GB.utf8\r\n$ sudo localectl set-keymap en_GB<\/pre>\n<\/blockquote>\n<h3>3.5 timedatectl<\/h3>\n<p><code>timedatectl<\/code>\u547d\u4ee4\u7528\u4e8e\u67e5\u770b\u5f53\u524d\u65f6\u533a\u8bbe\u7f6e\u3002<\/p>\n<blockquote>\n<pre class=\" language-bash\"># \u67e5\u770b\u5f53\u524d\u65f6\u533a\u8bbe\u7f6e\r\n$ timedatectl\r\n\r\n# \u663e\u793a\u6240\u6709\u53ef\u7528\u7684\u65f6\u533a\r\n$ timedatectl list-timezones \r\n\r\n# \u8bbe\u7f6e\u5f53\u524d\u65f6\u533a\r\n$ sudo timedatectl set-timezone America\/New_York\r\n$ sudo timedatectl set-time YYYY-MM-DD\r\n$ sudo timedatectl set-time HH:MM:SS<\/pre>\n<\/blockquote>\n<h3>3.6 loginctl<\/h3>\n<p><code>loginctl<\/code>\u547d\u4ee4\u7528\u4e8e\u67e5\u770b\u5f53\u524d\u767b\u5f55\u7684\u7528\u6237\u3002<\/p>\n<blockquote>\n<pre class=\" language-bash\"># \u5217\u51fa\u5f53\u524dsession\r\n$ loginctl list-sessions\r\n\r\n# \u5217\u51fa\u5f53\u524d\u767b\u5f55\u7528\u6237\r\n$ loginctl list-users\r\n\r\n# \u5217\u51fa\u663e\u793a\u6307\u5b9a\u7528\u6237\u7684\u4fe1\u606f\r\n$ loginctl show-user ruanyf<\/pre>\n<\/blockquote>\n<h2>\u56db\u3001Unit<\/h2>\n<h3>4.1 \u542b\u4e49<\/h3>\n<p>Systemd \u53ef\u4ee5\u7ba1\u7406\u6240\u6709\u7cfb\u7edf\u8d44\u6e90\u3002\u4e0d\u540c\u7684\u8d44\u6e90\u7edf\u79f0\u4e3a Unit\uff08\u5355\u4f4d\uff09\u3002<\/p>\n<p>Unit \u4e00\u5171\u5206\u621012\u79cd\u3002<\/p>\n<blockquote>\n<ul>\n<li>Service unit\uff1a\u7cfb\u7edf\u670d\u52a1<\/li>\n<li>Target unit\uff1a\u591a\u4e2a Unit \u6784\u6210\u7684\u4e00\u4e2a\u7ec4<\/li>\n<li>Device Unit\uff1a\u786c\u4ef6\u8bbe\u5907<\/li>\n<li>Mount Unit\uff1a\u6587\u4ef6\u7cfb\u7edf\u7684\u6302\u8f7d\u70b9<\/li>\n<li>Automount Unit\uff1a\u81ea\u52a8\u6302\u8f7d\u70b9<\/li>\n<li>Path Unit\uff1a\u6587\u4ef6\u6216\u8def\u5f84<\/li>\n<li>Scope Unit\uff1a\u4e0d\u662f\u7531 Systemd \u542f\u52a8\u7684\u5916\u90e8\u8fdb\u7a0b<\/li>\n<li>Slice Unit\uff1a\u8fdb\u7a0b\u7ec4<\/li>\n<li>Snapshot Unit\uff1aSystemd \u5feb\u7167\uff0c\u53ef\u4ee5\u5207\u56de\u67d0\u4e2a\u5feb\u7167<\/li>\n<li>Socket Unit\uff1a\u8fdb\u7a0b\u95f4\u901a\u4fe1\u7684 socket<\/li>\n<li>Swap Unit\uff1aswap \u6587\u4ef6<\/li>\n<li>Timer Unit\uff1a\u5b9a\u65f6\u5668<\/li>\n<\/ul>\n<\/blockquote>\n<p><code>systemctl list-units<\/code>\u547d\u4ee4\u53ef\u4ee5\u67e5\u770b\u5f53\u524d\u7cfb\u7edf\u7684\u6240\u6709 Unit \u3002<\/p>\n<blockquote>\n<pre class=\" language-bash\"># \u5217\u51fa\u6b63\u5728\u8fd0\u884c\u7684 Unit\r\n$ systemctl list-units\r\n\r\n# \u5217\u51fa\u6240\u6709Unit\uff0c\u5305\u62ec\u6ca1\u6709\u627e\u5230\u914d\u7f6e\u6587\u4ef6\u7684\u6216\u8005\u542f\u52a8\u5931\u8d25\u7684\r\n$ systemctl list-units --all\r\n\r\n# \u5217\u51fa\u6240\u6709\u6ca1\u6709\u8fd0\u884c\u7684 Unit\r\n$ systemctl list-units --all --state=inactive\r\n\r\n# \u5217\u51fa\u6240\u6709\u52a0\u8f7d\u5931\u8d25\u7684 Unit\r\n$ systemctl list-units --failed\r\n\r\n# \u5217\u51fa\u6240\u6709\u6b63\u5728\u8fd0\u884c\u7684\u3001\u7c7b\u578b\u4e3a service \u7684 Unit\r\n$ systemctl list-units --type=service<\/pre>\n<\/blockquote>\n<h3>4.2 Unit \u7684\u72b6\u6001<\/h3>\n<p><code>systemctl status<\/code>\u547d\u4ee4\u7528\u4e8e\u67e5\u770b\u7cfb\u7edf\u72b6\u6001\u548c\u5355\u4e2a Unit \u7684\u72b6\u6001\u3002<\/p>\n<blockquote>\n<pre class=\" language-bash\"># \u663e\u793a\u7cfb\u7edf\u72b6\u6001\r\n$ systemctl status\r\n\r\n# \u663e\u793a\u5355\u4e2a Unit \u7684\u72b6\u6001\r\n$ sysystemctl status bluetooth.service\r\n\r\n# \u663e\u793a\u8fdc\u7a0b\u4e3b\u673a\u7684\u67d0\u4e2a Unit \u7684\u72b6\u6001\r\n$ systemctl -H root@rhel7.example.com status httpd.service<\/pre>\n<\/blockquote>\n<p>\u9664\u4e86<code>status<\/code>\u547d\u4ee4\uff0c<code>systemctl<\/code>\u8fd8\u63d0\u4f9b\u4e86\u4e09\u4e2a\u67e5\u8be2\u72b6\u6001\u7684\u7b80\u5355\u65b9\u6cd5\uff0c\u4e3b\u8981\u4f9b\u811a\u672c\u5185\u90e8\u7684\u5224\u65ad\u8bed\u53e5\u4f7f\u7528\u3002<\/p>\n<blockquote>\n<pre class=\" language-bash\"># \u663e\u793a\u67d0\u4e2a Unit \u662f\u5426\u6b63\u5728\u8fd0\u884c\r\n$ systemctl is-active application.service\r\n\r\n# \u663e\u793a\u67d0\u4e2a Unit \u662f\u5426\u5904\u4e8e\u542f\u52a8\u5931\u8d25\u72b6\u6001\r\n$ systemctl is-failed application.service\r\n\r\n# \u663e\u793a\u67d0\u4e2a Unit \u670d\u52a1\u662f\u5426\u5efa\u7acb\u4e86\u542f\u52a8\u94fe\u63a5\r\n$ systemctl is-enabled application.service<\/pre>\n<\/blockquote>\n<h3>4.3 Unit \u7ba1\u7406<\/h3>\n<p>\u5bf9\u4e8e\u7528\u6237\u6765\u8bf4\uff0c\u6700\u5e38\u7528\u7684\u662f\u4e0b\u9762\u8fd9\u4e9b\u547d\u4ee4\uff0c\u7528\u4e8e\u542f\u52a8\u548c\u505c\u6b62 Unit\uff08\u4e3b\u8981\u662f service\uff09\u3002<\/p>\n<blockquote>\n<pre class=\" language-bash\"># \u7acb\u5373\u542f\u52a8\u4e00\u4e2a\u670d\u52a1\r\n$ sudo systemctl start apache.service\r\n\r\n# \u7acb\u5373\u505c\u6b62\u4e00\u4e2a\u670d\u52a1\r\n$ sudo systemctl stop apache.service\r\n\r\n# \u91cd\u542f\u4e00\u4e2a\u670d\u52a1\r\n$ sudo systemctl restart apache.service\r\n\r\n# \u6740\u6b7b\u4e00\u4e2a\u670d\u52a1\u7684\u6240\u6709\u5b50\u8fdb\u7a0b\r\n$ sudo systemctl kill apache.service\r\n\r\n# \u91cd\u65b0\u52a0\u8f7d\u4e00\u4e2a\u670d\u52a1\u7684\u914d\u7f6e\u6587\u4ef6\r\n$ sudo systemctl reload apache.service\r\n\r\n# \u91cd\u8f7d\u6240\u6709\u4fee\u6539\u8fc7\u7684\u914d\u7f6e\u6587\u4ef6\r\n$ sudo systemctl daemon-reload\r\n\r\n# \u663e\u793a\u67d0\u4e2a Unit \u7684\u6240\u6709\u5e95\u5c42\u53c2\u6570\r\n$ systemctl show httpd.service\r\n\r\n# \u663e\u793a\u67d0\u4e2a Unit \u7684\u6307\u5b9a\u5c5e\u6027\u7684\u503c\r\n$ systemctl show -p CPUShares httpd.service\r\n\r\n# \u8bbe\u7f6e\u67d0\u4e2a Unit \u7684\u6307\u5b9a\u5c5e\u6027\r\n$ sudo systemctl set-property httpd.service CPUShares=500<\/pre>\n<\/blockquote>\n<h3>4.4 \u4f9d\u8d56\u5173\u7cfb<\/h3>\n<p>Unit \u4e4b\u95f4\u5b58\u5728\u4f9d\u8d56\u5173\u7cfb\uff1aA \u4f9d\u8d56\u4e8e B\uff0c\u5c31\u610f\u5473\u7740 Systemd \u5728\u542f\u52a8 A \u7684\u65f6\u5019\uff0c\u540c\u65f6\u4f1a\u53bb\u542f\u52a8 B\u3002<\/p>\n<p><code>systemctl list-dependencies<\/code>\u547d\u4ee4\u5217\u51fa\u4e00\u4e2a Unit \u7684\u6240\u6709\u4f9d\u8d56\u3002<\/p>\n<blockquote>\n<pre class=\" language-bash\">$ systemctl list-dependencies nginx.service<\/pre>\n<\/blockquote>\n<p>\u4e0a\u9762\u547d\u4ee4\u7684\u8f93\u51fa\u7ed3\u679c\u4e4b\u4e2d\uff0c\u6709\u4e9b\u4f9d\u8d56\u662f Target \u7c7b\u578b\uff08\u8be6\u89c1\u4e0b\u6587\uff09\uff0c\u9ed8\u8ba4\u4e0d\u4f1a\u5c55\u5f00\u663e\u793a\u3002\u5982\u679c\u8981\u5c55\u5f00 Target\uff0c\u5c31\u9700\u8981\u4f7f\u7528<code>--all<\/code>\u53c2\u6570\u3002<\/p>\n<blockquote>\n<pre class=\" language-bash\">$ systemctl list-dependencies --all nginx.service<\/pre>\n<\/blockquote>\n<h2>\u4e94\u3001Unit \u7684\u914d\u7f6e\u6587\u4ef6<\/h2>\n<h3>5.1 \u6982\u8ff0<\/h3>\n<p>\u6bcf\u4e00\u4e2a Unit \u90fd\u6709\u4e00\u4e2a\u914d\u7f6e\u6587\u4ef6\uff0c\u544a\u8bc9 Systemd \u600e\u4e48\u542f\u52a8\u8fd9\u4e2a Unit \u3002<\/p>\n<p>Systemd \u9ed8\u8ba4\u4ece\u76ee\u5f55<code>\/etc\/systemd\/system\/<\/code>\u8bfb\u53d6\u914d\u7f6e\u6587\u4ef6\u3002\u4f46\u662f\uff0c\u91cc\u9762\u5b58\u653e\u7684\u5927\u90e8\u5206\u6587\u4ef6\u90fd\u662f\u7b26\u53f7\u94fe\u63a5\uff0c\u6307\u5411\u76ee\u5f55<code>\/usr\/lib\/systemd\/system\/<\/code>\uff0c\u771f\u6b63\u7684\u914d\u7f6e\u6587\u4ef6\u5b58\u653e\u5728\u90a3\u4e2a\u76ee\u5f55\u3002<\/p>\n<p><code>systemctl enable<\/code>\u547d\u4ee4\u7528\u4e8e\u5728\u4e0a\u9762\u4e24\u4e2a\u76ee\u5f55\u4e4b\u95f4\uff0c\u5efa\u7acb\u7b26\u53f7\u94fe\u63a5\u5173\u7cfb\u3002<\/p>\n<blockquote>\n<pre class=\" language-bash\">$ sudo systemctl enable clamd@scan.service\r\n# \u7b49\u540c\u4e8e\r\n$ sudo ln -s &#039;\/usr\/lib\/systemd\/system\/clamd@scan.service&#039; &#039;\/etc\/systemd\/system\/multi-user.target.wants\/clamd@scan.service<\/pre>\n<\/blockquote>\n<p>\u5982\u679c\u914d\u7f6e\u6587\u4ef6\u91cc\u9762\u8bbe\u7f6e\u4e86\u5f00\u673a\u542f\u52a8\uff0c<code>systemctl enable<\/code>\u547d\u4ee4\u76f8\u5f53\u4e8e\u6fc0\u6d3b\u5f00\u673a\u542f\u52a8\u3002<\/p>\n<p>\u4e0e\u4e4b\u5bf9\u5e94\u7684\uff0c<code>systemctl disable<\/code>\u547d\u4ee4\u7528\u4e8e\u5728\u4e24\u4e2a\u76ee\u5f55\u4e4b\u95f4\uff0c\u64a4\u9500\u7b26\u53f7\u94fe\u63a5\u5173\u7cfb\uff0c\u76f8\u5f53\u4e8e\u64a4\u9500\u5f00\u673a\u542f\u52a8\u3002<\/p>\n<blockquote>\n<pre class=\" language-bash\">$ sudo systemctl disable clamd@scan.service<\/pre>\n<\/blockquote>\n<p>\u914d\u7f6e\u6587\u4ef6\u7684\u540e\u7f00\u540d\uff0c\u5c31\u662f\u8be5 Unit \u7684\u79cd\u7c7b\uff0c\u6bd4\u5982<code>sshd.socket<\/code>\u3002\u5982\u679c\u7701\u7565\uff0cSystemd \u9ed8\u8ba4\u540e\u7f00\u540d\u4e3a<code>.service<\/code>\uff0c\u6240\u4ee5<code>sshd<\/code>\u4f1a\u88ab\u7406\u89e3\u6210<code>sshd.service<\/code>\u3002<\/p>\n<h3>5.2 \u914d\u7f6e\u6587\u4ef6\u7684\u72b6\u6001<\/h3>\n<p><code>systemctl list-unit-files<\/code>\u547d\u4ee4\u7528\u4e8e\u5217\u51fa\u6240\u6709\u914d\u7f6e\u6587\u4ef6\u3002<\/p>\n<blockquote>\n<pre class=\" language-bash\"># \u5217\u51fa\u6240\u6709\u914d\u7f6e\u6587\u4ef6\r\n$ systemctl list-unit-files\r\n\r\n# \u5217\u51fa\u6307\u5b9a\u7c7b\u578b\u7684\u914d\u7f6e\u6587\u4ef6\r\n$ systemctl list-unit-files --type=service<\/pre>\n<\/blockquote>\n<p>\u8fd9\u4e2a\u547d\u4ee4\u4f1a\u8f93\u51fa\u4e00\u4e2a\u5217\u8868\u3002<\/p>\n<blockquote>\n<pre class=\" language-bash\">$ systemctl list-unit-files\r\n\r\nUNIT FILE STATE\r\nchronyd.service enabled\r\nclamd@.service static\r\nclamd@scan.service disabled<\/pre>\n<\/blockquote>\n<p>\u8fd9\u4e2a\u5217\u8868\u663e\u793a\u6bcf\u4e2a\u914d\u7f6e\u6587\u4ef6\u7684\u72b6\u6001\uff0c\u4e00\u5171\u6709\u56db\u79cd\u3002<\/p>\n<blockquote>\n<ul>\n<li>enabled\uff1a\u5df2\u5efa\u7acb\u542f\u52a8\u94fe\u63a5<\/li>\n<li>disabled\uff1a\u6ca1\u5efa\u7acb\u542f\u52a8\u94fe\u63a5<\/li>\n<li>static\uff1a\u8be5\u914d\u7f6e\u6587\u4ef6\u6ca1\u6709<code>[Install]<\/code>\u90e8\u5206\uff08\u65e0\u6cd5\u6267\u884c\uff09\uff0c\u53ea\u80fd\u4f5c\u4e3a\u5176\u4ed6\u914d\u7f6e\u6587\u4ef6\u7684\u4f9d\u8d56<\/li>\n<li>masked\uff1a\u8be5\u914d\u7f6e\u6587\u4ef6\u88ab\u7981\u6b62\u5efa\u7acb\u542f\u52a8\u94fe\u63a5<\/li>\n<\/ul>\n<\/blockquote>\n<p>\u6ce8\u610f\uff0c\u4ece\u914d\u7f6e\u6587\u4ef6\u7684\u72b6\u6001\u65e0\u6cd5\u770b\u51fa\uff0c\u8be5 Unit \u662f\u5426\u6b63\u5728\u8fd0\u884c\u3002\u8fd9\u5fc5\u987b\u6267\u884c\u524d\u9762\u63d0\u5230\u7684<code>systemctl status<\/code>\u547d\u4ee4\u3002<\/p>\n<blockquote>\n<pre class=\" language-bash\">$ systemctl status bluetooth.service<\/pre>\n<\/blockquote>\n<p>\u4e00\u65e6\u4fee\u6539\u914d\u7f6e\u6587\u4ef6\uff0c\u5c31\u8981\u8ba9 SystemD \u91cd\u65b0\u52a0\u8f7d\u914d\u7f6e\u6587\u4ef6\uff0c\u7136\u540e\u91cd\u65b0\u542f\u52a8\uff0c\u5426\u5219\u4fee\u6539\u4e0d\u4f1a\u751f\u6548\u3002<\/p>\n<blockquote>\n<pre class=\" language-bash\">$ sudo systemctl daemon-reload\r\n$ sudo systemctl restart httpd.service<\/pre>\n<\/blockquote>\n<h3>5.3 \u914d\u7f6e\u6587\u4ef6\u7684\u683c\u5f0f<\/h3>\n<p>\u914d\u7f6e\u6587\u4ef6\u5c31\u662f\u666e\u901a\u7684\u6587\u672c\u6587\u4ef6\uff0c\u53ef\u4ee5\u7528\u6587\u672c\u7f16\u8f91\u5668\u6253\u5f00\u3002<\/p>\n<p><code>systemctl cat<\/code>\u547d\u4ee4\u53ef\u4ee5\u67e5\u770b\u914d\u7f6e\u6587\u4ef6\u7684\u5185\u5bb9\u3002<\/p>\n<blockquote>\n<pre class=\" language-bash\">$ systemctl cat atd.service\r\n\r\n[Unit]\r\nDescription=ATD daemon\r\n\r\n[Service]\r\nType=forking\r\nExecStart=\/usr\/bin\/atd\r\n\r\n[Install]\r\nWantedBy=multi-user.target<\/pre>\n<\/blockquote>\n<p>\u4ece\u4e0a\u9762\u7684\u8f93\u51fa\u53ef\u4ee5\u770b\u5230\uff0c\u914d\u7f6e\u6587\u4ef6\u5206\u6210\u51e0\u4e2a\u533a\u5757\u3002\u6bcf\u4e2a\u533a\u5757\u7684\u7b2c\u4e00\u884c\uff0c\u662f\u7528\u65b9\u62ec\u53f7\u8868\u793a\u7684\u533a\u522b\u540d\uff0c\u6bd4\u5982<code>[Unit]<\/code>\u3002\u6ce8\u610f\uff0c\u914d\u7f6e\u6587\u4ef6\u7684\u533a\u5757\u540d\u548c\u5b57\u6bb5\u540d\uff0c\u90fd\u662f\u5927\u5c0f\u5199\u654f\u611f\u7684\u3002<\/p>\n<p>\u6bcf\u4e2a\u533a\u5757\u5185\u90e8\u662f\u4e00\u4e9b\u7b49\u53f7\u8fde\u63a5\u7684\u952e\u503c\u5bf9\u3002<\/p>\n<blockquote>\n<pre class=\" language-bash\">[Section]\r\nDirective1=value\r\nDirective2=value\r\n\r\n. . .<\/pre>\n<\/blockquote>\n<p>\u6ce8\u610f\uff0c\u952e\u503c\u5bf9\u7684\u7b49\u53f7\u4e24\u4fa7\u4e0d\u80fd\u6709\u7a7a\u683c\u3002<\/p>\n<h3>5.4 \u914d\u7f6e\u6587\u4ef6\u7684\u533a\u5757<\/h3>\n<p><code>[Unit]<\/code>\u533a\u5757\u901a\u5e38\u662f\u914d\u7f6e\u6587\u4ef6\u7684\u7b2c\u4e00\u4e2a\u533a\u5757\uff0c\u7528\u6765\u5b9a\u4e49 Unit \u7684\u5143\u6570\u636e\uff0c\u4ee5\u53ca\u914d\u7f6e\u4e0e\u5176\u4ed6 Unit \u7684\u5173\u7cfb\u3002\u5b83\u7684\u4e3b\u8981\u5b57\u6bb5\u5982\u4e0b\u3002<\/p>\n<blockquote>\n<ul>\n<li><code>Description<\/code>\uff1a\u7b80\u77ed\u63cf\u8ff0<\/li>\n<li><code>Documentation<\/code>\uff1a\u6587\u6863\u5730\u5740<\/li>\n<li><code>Requires<\/code>\uff1a\u5f53\u524d Unit \u4f9d\u8d56\u7684\u5176\u4ed6 Unit\uff0c\u5982\u679c\u5b83\u4eec\u6ca1\u6709\u8fd0\u884c\uff0c\u5f53\u524d Unit \u4f1a\u542f\u52a8\u5931\u8d25<\/li>\n<li><code>Wants<\/code>\uff1a\u4e0e\u5f53\u524d Unit \u914d\u5408\u7684\u5176\u4ed6 Unit\uff0c\u5982\u679c\u5b83\u4eec\u6ca1\u6709\u8fd0\u884c\uff0c\u5f53\u524d Unit \u4e0d\u4f1a\u542f\u52a8\u5931\u8d25<\/li>\n<li><code>BindsTo<\/code>\uff1a\u4e0e<code>Requires<\/code>\u7c7b\u4f3c\uff0c\u5b83\u6307\u5b9a\u7684 Unit \u5982\u679c\u9000\u51fa\uff0c\u4f1a\u5bfc\u81f4\u5f53\u524d Unit \u505c\u6b62\u8fd0\u884c<\/li>\n<li><code>Before<\/code>\uff1a\u5982\u679c\u8be5\u5b57\u6bb5\u6307\u5b9a\u7684 Unit \u4e5f\u8981\u542f\u52a8\uff0c\u90a3\u4e48\u5fc5\u987b\u5728\u5f53\u524d Unit \u4e4b\u540e\u542f\u52a8<\/li>\n<li><code>After<\/code>\uff1a\u5982\u679c\u8be5\u5b57\u6bb5\u6307\u5b9a\u7684 Unit \u4e5f\u8981\u542f\u52a8\uff0c\u90a3\u4e48\u5fc5\u987b\u5728\u5f53\u524d Unit \u4e4b\u524d\u542f\u52a8<\/li>\n<li><code>Conflicts<\/code>\uff1a\u8fd9\u91cc\u6307\u5b9a\u7684 Unit \u4e0d\u80fd\u4e0e\u5f53\u524d Unit \u540c\u65f6\u8fd0\u884c<\/li>\n<li><code>Condition...<\/code>\uff1a\u5f53\u524d Unit \u8fd0\u884c\u5fc5\u987b\u6ee1\u8db3\u7684\u6761\u4ef6\uff0c\u5426\u5219\u4e0d\u4f1a\u8fd0\u884c<\/li>\n<li><code>Assert...<\/code>\uff1a\u5f53\u524d Unit \u8fd0\u884c\u5fc5\u987b\u6ee1\u8db3\u7684\u6761\u4ef6\uff0c\u5426\u5219\u4f1a\u62a5\u542f\u52a8\u5931\u8d25<\/li>\n<\/ul>\n<\/blockquote>\n<p><code>[Install]<\/code>\u901a\u5e38\u662f\u914d\u7f6e\u6587\u4ef6\u7684\u6700\u540e\u4e00\u4e2a\u533a\u5757\uff0c\u7528\u6765\u5b9a\u4e49\u5982\u4f55\u542f\u52a8\uff0c\u4ee5\u53ca\u662f\u5426\u5f00\u673a\u542f\u52a8\u3002\u5b83\u7684\u4e3b\u8981\u5b57\u6bb5\u5982\u4e0b\u3002<\/p>\n<blockquote>\n<ul>\n<li><code>WantedBy<\/code>\uff1a\u5b83\u7684\u503c\u662f\u4e00\u4e2a\u6216\u591a\u4e2a Target\uff0c\u5f53\u524d Unit \u6fc0\u6d3b\u65f6\uff08enable\uff09\u7b26\u53f7\u94fe\u63a5\u4f1a\u653e\u5165<code>\/etc\/systemd\/system<\/code>\u76ee\u5f55\u4e0b\u9762\u4ee5 Target \u540d + <code>.wants<\/code>\u540e\u7f00\u6784\u6210\u7684\u5b50\u76ee\u5f55\u4e2d<\/li>\n<li><code>RequiredBy<\/code>\uff1a\u5b83\u7684\u503c\u662f\u4e00\u4e2a\u6216\u591a\u4e2a Target\uff0c\u5f53\u524d Unit \u6fc0\u6d3b\u65f6\uff0c\u7b26\u53f7\u94fe\u63a5\u4f1a\u653e\u5165<code>\/etc\/systemd\/system<\/code>\u76ee\u5f55\u4e0b\u9762\u4ee5 Target \u540d + <code>.required<\/code>\u540e\u7f00\u6784\u6210\u7684\u5b50\u76ee\u5f55\u4e2d<\/li>\n<li><code>Alias<\/code>\uff1a\u5f53\u524d Unit \u53ef\u7528\u4e8e\u542f\u52a8\u7684\u522b\u540d<\/li>\n<li><code>Also<\/code>\uff1a\u5f53\u524d Unit \u6fc0\u6d3b\uff08enable\uff09\u65f6\uff0c\u4f1a\u88ab\u540c\u65f6\u6fc0\u6d3b\u7684\u5176\u4ed6 Unit<\/li>\n<\/ul>\n<\/blockquote>\n<p><code>[Service]<\/code>\u533a\u5757\u7528\u6765 Service \u7684\u914d\u7f6e\uff0c\u53ea\u6709 Service \u7c7b\u578b\u7684 Unit \u624d\u6709\u8fd9\u4e2a\u533a\u5757\u3002\u5b83\u7684\u4e3b\u8981\u5b57\u6bb5\u5982\u4e0b\u3002<\/p>\n<blockquote>\n<ul>\n<li><code>Type<\/code>\uff1a\u5b9a\u4e49\u542f\u52a8\u65f6\u7684\u8fdb\u7a0b\u884c\u4e3a\u3002\u5b83\u6709\u4ee5\u4e0b\u51e0\u79cd\u503c\u3002<\/li>\n<li><code>Type=simple<\/code>\uff1a\u9ed8\u8ba4\u503c\uff0c\u6267\u884c<code>ExecStart<\/code>\u6307\u5b9a\u7684\u547d\u4ee4\uff0c\u542f\u52a8\u4e3b\u8fdb\u7a0b<\/li>\n<li><code>Type=forking<\/code>\uff1a\u4ee5 fork \u65b9\u5f0f\u4ece\u7236\u8fdb\u7a0b\u521b\u5efa\u5b50\u8fdb\u7a0b\uff0c\u521b\u5efa\u540e\u7236\u8fdb\u7a0b\u4f1a\u7acb\u5373\u9000\u51fa<\/li>\n<li><code>Type=oneshot<\/code>\uff1a\u4e00\u6b21\u6027\u8fdb\u7a0b\uff0cSystemd \u4f1a\u7b49\u5f53\u524d\u670d\u52a1\u9000\u51fa\uff0c\u518d\u7ee7\u7eed\u5f80\u4e0b\u6267\u884c<\/li>\n<li><code>Type=dbus<\/code>\uff1a\u5f53\u524d\u670d\u52a1\u901a\u8fc7D-Bus\u542f\u52a8<\/li>\n<li><code>Type=notify<\/code>\uff1a\u5f53\u524d\u670d\u52a1\u542f\u52a8\u5b8c\u6bd5\uff0c\u4f1a\u901a\u77e5<code>Systemd<\/code>\uff0c\u518d\u7ee7\u7eed\u5f80\u4e0b\u6267\u884c<\/li>\n<li><code>Type=idle<\/code>\uff1a\u82e5\u6709\u5176\u4ed6\u4efb\u52a1\u6267\u884c\u5b8c\u6bd5\uff0c\u5f53\u524d\u670d\u52a1\u624d\u4f1a\u8fd0\u884c<\/li>\n<li><code>ExecStart<\/code>\uff1a\u542f\u52a8\u5f53\u524d\u670d\u52a1\u7684\u547d\u4ee4<\/li>\n<li><code>ExecStartPre<\/code>\uff1a\u542f\u52a8\u5f53\u524d\u670d\u52a1\u4e4b\u524d\u6267\u884c\u7684\u547d\u4ee4<\/li>\n<li><code>ExecStartPost<\/code>\uff1a\u542f\u52a8\u5f53\u524d\u670d\u52a1\u4e4b\u540e\u6267\u884c\u7684\u547d\u4ee4<\/li>\n<li><code>ExecReload<\/code>\uff1a\u91cd\u542f\u5f53\u524d\u670d\u52a1\u65f6\u6267\u884c\u7684\u547d\u4ee4<\/li>\n<li><code>ExecStop<\/code>\uff1a\u505c\u6b62\u5f53\u524d\u670d\u52a1\u65f6\u6267\u884c\u7684\u547d\u4ee4<\/li>\n<li><code>ExecStopPost<\/code>\uff1a\u505c\u6b62\u5f53\u5176\u670d\u52a1\u4e4b\u540e\u6267\u884c\u7684\u547d\u4ee4<\/li>\n<li><code>RestartSec<\/code>\uff1a\u81ea\u52a8\u91cd\u542f\u5f53\u524d\u670d\u52a1\u95f4\u9694\u7684\u79d2\u6570<\/li>\n<li><code>Restart<\/code>\uff1a\u5b9a\u4e49\u4f55\u79cd\u60c5\u51b5 Systemd \u4f1a\u81ea\u52a8\u91cd\u542f\u5f53\u524d\u670d\u52a1\uff0c\u53ef\u80fd\u7684\u503c\u5305\u62ec<code>always<\/code>\uff08\u603b\u662f\u91cd\u542f\uff09\u3001<code>on-success<\/code>\u3001<code>on-failure<\/code>\u3001<code>on-abnormal<\/code>\u3001<code>on-abort<\/code>\u3001<code>on-watchdog<\/code><\/li>\n<li><code>TimeoutSec<\/code>\uff1a\u5b9a\u4e49 Systemd \u505c\u6b62\u5f53\u524d\u670d\u52a1\u4e4b\u524d\u7b49\u5f85\u7684\u79d2\u6570<\/li>\n<li><code>Environment<\/code>\uff1a\u6307\u5b9a\u73af\u5883\u53d8\u91cf<\/li>\n<\/ul>\n<\/blockquote>\n<p>Unit \u914d\u7f6e\u6587\u4ef6\u7684\u5b8c\u6574\u5b57\u6bb5\u6e05\u5355\uff0c\u8bf7\u53c2\u8003<a href=\"https:\/\/www.freedesktop.org\/software\/systemd\/man\/systemd.unit.html\" target=\"_blank\">\u5b98\u65b9\u6587\u6863<\/a>\u3002<\/p>\n<h2>\u516d\u3001Target<\/h2>\n<p>\u542f\u52a8\u8ba1\u7b97\u673a\u7684\u65f6\u5019\uff0c\u9700\u8981\u542f\u52a8\u5927\u91cf\u7684 Unit\u3002\u5982\u679c\u6bcf\u4e00\u6b21\u542f\u52a8\uff0c\u90fd\u8981\u4e00\u4e00\u5199\u660e\u672c\u6b21\u542f\u52a8\u9700\u8981\u54ea\u4e9b Unit\uff0c\u663e\u7136\u975e\u5e38\u4e0d\u65b9\u4fbf\u3002Systemd \u7684\u89e3\u51b3\u65b9\u6848\u5c31\u662f Target\u3002<\/p>\n<p>\u7b80\u5355\u8bf4\uff0cTarget \u5c31\u662f\u4e00\u4e2a Unit \u7ec4\uff0c\u5305\u542b\u8bb8\u591a\u76f8\u5173\u7684 Unit \u3002\u542f\u52a8\u67d0\u4e2a Target \u7684\u65f6\u5019\uff0cSystemd \u5c31\u4f1a\u542f\u52a8\u91cc\u9762\u6240\u6709\u7684 Unit\u3002\u4ece\u8fd9\u4e2a\u610f\u4e49\u4e0a\u8bf4\uff0cTarget \u8fd9\u4e2a\u6982\u5ff5\u7c7b\u4f3c\u4e8e\"\u72b6\u6001\u70b9\"\uff0c\u542f\u52a8\u67d0\u4e2a Target \u5c31\u597d\u6bd4\u542f\u52a8\u5230\u67d0\u79cd\u72b6\u6001\u3002<\/p>\n<p>\u4f20\u7edf\u7684<code>init<\/code>\u542f\u52a8\u6a21\u5f0f\u91cc\u9762\uff0c\u6709 RunLevel \u7684\u6982\u5ff5\uff0c\u8ddf Target \u7684\u4f5c\u7528\u5f88\u7c7b\u4f3c\u3002\u4e0d\u540c\u7684\u662f\uff0cRunLevel \u662f\u4e92\u65a5\u7684\uff0c\u4e0d\u53ef\u80fd\u591a\u4e2a RunLevel \u540c\u65f6\u542f\u52a8\uff0c\u4f46\u662f\u591a\u4e2a Target \u53ef\u4ee5\u540c\u65f6\u542f\u52a8\u3002<\/p>\n<blockquote>\n<pre class=\" language-bash\"># \u67e5\u770b\u5f53\u524d\u7cfb\u7edf\u7684\u6240\u6709 Target\r\n$ systemctl list-unit-files --type=target\r\n\r\n# \u67e5\u770b\u4e00\u4e2a Target \u5305\u542b\u7684\u6240\u6709 Unit\r\n$ systemctl list-dependencies multi-user.target\r\n\r\n# \u67e5\u770b\u542f\u52a8\u65f6\u7684\u9ed8\u8ba4 Target\r\n$ systemctl get-default\r\n\r\n# \u8bbe\u7f6e\u542f\u52a8\u65f6\u7684\u9ed8\u8ba4 Target\r\n$ sudo systemctl set-default multi-user.target\r\n\r\n# \u5207\u6362 Target \u65f6\uff0c\u9ed8\u8ba4\u4e0d\u5173\u95ed\u524d\u4e00\u4e2a Target \u542f\u52a8\u7684\u8fdb\u7a0b\uff0c\r\n# systemctl isolate \u547d\u4ee4\u6539\u53d8\u8fd9\u79cd\u884c\u4e3a\uff0c\r\n# \u5173\u95ed\u524d\u4e00\u4e2a Target \u91cc\u9762\u6240\u6709\u4e0d\u5c5e\u4e8e\u540e\u4e00\u4e2a Target \u7684\u8fdb\u7a0b\r\n$ sudo systemctl isolate multi-user.target<\/pre>\n<\/blockquote>\n<p>Target \u4e0e \u4f20\u7edf RunLevel \u7684\u5bf9\u5e94\u5173\u7cfb\u5982\u4e0b\u3002<\/p>\n<blockquote>\n<pre class=\" language-bash\">Traditional runlevel New target name Symbolically linked to...\r\n\r\nRunlevel 0 | runlevel0.target -&gt; poweroff.target\r\nRunlevel 1 | runlevel1.target -&gt; rescue.target\r\nRunlevel 2 | runlevel2.target -&gt; multi-user.target\r\nRunlevel 3 | runlevel3.target -&gt; multi-user.target\r\nRunlevel 4 | runlevel4.target -&gt; multi-user.target\r\nRunlevel 5 | runlevel5.target -&gt; graphical.target\r\nRunlevel 6 | runlevel6.target -&gt; reboot.target<\/pre>\n<\/blockquote>\n<p>\u5b83\u4e0e<code>init<\/code>\u8fdb\u7a0b\u7684\u4e3b\u8981\u5dee\u522b\u5982\u4e0b\u3002<\/p>\n<blockquote><p><strong>\uff081\uff09\u9ed8\u8ba4\u7684 RunLevel<\/strong>\uff08\u5728<code>\/etc\/inittab<\/code>\u6587\u4ef6\u8bbe\u7f6e\uff09\u73b0\u5728\u88ab\u9ed8\u8ba4\u7684 Target \u53d6\u4ee3\uff0c\u4f4d\u7f6e\u662f<code>\/etc\/systemd\/system\/default.target<\/code>\uff0c\u901a\u5e38\u7b26\u53f7\u94fe\u63a5\u5230<code>graphical.target<\/code>\uff08\u56fe\u5f62\u754c\u9762\uff09\u6216\u8005<code>multi-user.target<\/code>\uff08\u591a\u7528\u6237\u547d\u4ee4\u884c\uff09\u3002<\/p>\n<p><strong>\uff082\uff09\u542f\u52a8\u811a\u672c\u7684\u4f4d\u7f6e<\/strong>\uff0c\u4ee5\u524d\u662f<code>\/etc\/init.d<\/code>\u76ee\u5f55\uff0c\u7b26\u53f7\u94fe\u63a5\u5230\u4e0d\u540c\u7684 RunLevel \u76ee\u5f55 \uff08\u6bd4\u5982<code>\/etc\/rc3.d<\/code>\u3001<code>\/etc\/rc5.d<\/code>\u7b49\uff09\uff0c\u73b0\u5728\u5219\u5b58\u653e\u5728<code>\/lib\/systemd\/system<\/code>\u548c<code>\/etc\/systemd\/system<\/code>\u76ee\u5f55\u3002<\/p>\n<p><strong>\uff083\uff09\u914d\u7f6e\u6587\u4ef6\u7684\u4f4d\u7f6e<\/strong>\uff0c\u4ee5\u524d<code>init<\/code>\u8fdb\u7a0b\u7684\u914d\u7f6e\u6587\u4ef6\u662f<code>\/etc\/inittab<\/code>\uff0c\u5404\u79cd\u670d\u52a1\u7684\u914d\u7f6e\u6587\u4ef6\u5b58\u653e\u5728<code>\/etc\/sysconfig<\/code>\u76ee\u5f55\u3002\u73b0\u5728\u7684\u914d\u7f6e\u6587\u4ef6\u4e3b\u8981\u5b58\u653e\u5728<code>\/lib\/systemd<\/code>\u76ee\u5f55\uff0c\u5728<code>\/etc\/systemd<\/code>\u76ee\u5f55\u91cc\u9762\u7684\u4fee\u6539\u53ef\u4ee5\u8986\u76d6\u539f\u59cb\u8bbe\u7f6e\u3002<\/p><\/blockquote>\n<h2>\u4e03\u3001\u65e5\u5fd7\u7ba1\u7406<\/h2>\n<p>Systemd \u7edf\u4e00\u7ba1\u7406\u6240\u6709 Unit \u7684\u542f\u52a8\u65e5\u5fd7\u3002\u5e26\u6765\u7684\u597d\u5904\u5c31\u662f\uff0c\u53ef\u4ee5\u53ea\u7528<code>journalctl<\/code>\u4e00\u4e2a\u547d\u4ee4\uff0c\u67e5\u770b\u6240\u6709\u65e5\u5fd7\uff08\u5185\u6838\u65e5\u5fd7\u548c\u5e94\u7528\u65e5\u5fd7\uff09\u3002\u65e5\u5fd7\u7684\u914d\u7f6e\u6587\u4ef6\u662f<code>\/etc\/systemd\/journald.conf<\/code>\u3002<\/p>\n<p><code>journalctl<\/code>\u529f\u80fd\u5f3a\u5927\uff0c\u7528\u6cd5\u975e\u5e38\u591a\u3002<\/p>\n<blockquote>\n<pre class=\" language-bash\"># \u67e5\u770b\u6240\u6709\u65e5\u5fd7\uff08\u9ed8\u8ba4\u60c5\u51b5\u4e0b \uff0c\u53ea\u4fdd\u5b58\u672c\u6b21\u542f\u52a8\u7684\u65e5\u5fd7\uff09\r\n$ sudo journalctl\r\n\r\n# \u67e5\u770b\u5185\u6838\u65e5\u5fd7\uff08\u4e0d\u663e\u793a\u5e94\u7528\u65e5\u5fd7\uff09\r\n$ sudo journalctl -k\r\n\r\n# \u67e5\u770b\u7cfb\u7edf\u672c\u6b21\u542f\u52a8\u7684\u65e5\u5fd7\r\n$ sudo journalctl -b\r\n$ sudo journalctl -b -0\r\n\r\n# \u67e5\u770b\u4e0a\u4e00\u6b21\u542f\u52a8\u7684\u65e5\u5fd7\uff08\u9700\u66f4\u6539\u8bbe\u7f6e\uff09\r\n$ sudo journalctl -b -1\r\n\r\n# \u67e5\u770b\u6307\u5b9a\u65f6\u95f4\u7684\u65e5\u5fd7\r\n$ sudo journalctl --since=&quot;2012-10-30 18:17:16&quot;\r\n$ sudo journalctl --since &quot;20 min ago&quot;\r\n$ sudo journalctl --since yesterday\r\n$ sudo journalctl --since &quot;2015-01-10&quot; --until &quot;2015-01-11 03:00&quot;\r\n$ sudo journalctl --since 09:00 --until &quot;1 hour ago&quot;\r\n\r\n# \u663e\u793a\u5c3e\u90e8\u7684\u6700\u65b010\u884c\u65e5\u5fd7\r\n$ sudo journalctl -n\r\n\r\n# \u663e\u793a\u5c3e\u90e8\u6307\u5b9a\u884c\u6570\u7684\u65e5\u5fd7\r\n$ sudo journalctl -n 20\r\n\r\n# \u5b9e\u65f6\u6eda\u52a8\u663e\u793a\u6700\u65b0\u65e5\u5fd7\r\n$ sudo journalctl -f\r\n\r\n# \u67e5\u770b\u6307\u5b9a\u670d\u52a1\u7684\u65e5\u5fd7\r\n$ sudo journalctl \/usr\/lib\/systemd\/systemd\r\n\r\n# \u67e5\u770b\u6307\u5b9a\u8fdb\u7a0b\u7684\u65e5\u5fd7\r\n$ sudo journalctl _PID=1\r\n\r\n# \u67e5\u770b\u67d0\u4e2a\u8def\u5f84\u7684\u811a\u672c\u7684\u65e5\u5fd7\r\n$ sudo journalctl \/usr\/bin\/bash\r\n\r\n# \u67e5\u770b\u6307\u5b9a\u7528\u6237\u7684\u65e5\u5fd7\r\n$ sudo journalctl _UID=33 --since today\r\n\r\n# \u67e5\u770b\u67d0\u4e2a Unit \u7684\u65e5\u5fd7\r\n$ sudo journalctl -u nginx.service\r\n$ sudo journalctl -u nginx.service --since today\r\n\r\n# \u5b9e\u65f6\u6eda\u52a8\u663e\u793a\u67d0\u4e2a Unit \u7684\u6700\u65b0\u65e5\u5fd7\r\n$ sudo journalctl -u nginx.service -f\r\n\r\n# \u5408\u5e76\u663e\u793a\u591a\u4e2a Unit \u7684\u65e5\u5fd7\r\n$ journalctl -u nginx.service -u php-fpm.service --since today\r\n\r\n# \u67e5\u770b\u6307\u5b9a\u4f18\u5148\u7ea7\uff08\u53ca\u5176\u4ee5\u4e0a\u7ea7\u522b\uff09\u7684\u65e5\u5fd7\uff0c\u5171\u67098\u7ea7\r\n# 0: emerg\r\n# 1: alert\r\n# 2: crit\r\n# 3: err\r\n# 4: warning\r\n# 5: notice\r\n# 6: info\r\n# 7: debug\r\n$ sudo journalctl -p err -b\r\n\r\n# \u65e5\u5fd7\u9ed8\u8ba4\u5206\u9875\u8f93\u51fa\uff0c--no-pager \u6539\u4e3a\u6b63\u5e38\u7684\u6807\u51c6\u8f93\u51fa\r\n$ sudo journalctl --no-pager\r\n\r\n# \u4ee5 JSON \u683c\u5f0f\uff08\u5355\u884c\uff09\u8f93\u51fa\r\n$ sudo journalctl -b -u nginx.service -o json\r\n\r\n# \u4ee5 JSON \u683c\u5f0f\uff08\u591a\u884c\uff09\u8f93\u51fa\uff0c\u53ef\u8bfb\u6027\u66f4\u597d\r\n$ sudo journalctl -b -u nginx.serviceqq\r\n -o json-pretty\r\n\r\n# \u663e\u793a\u65e5\u5fd7\u5360\u636e\u7684\u786c\u76d8\u7a7a\u95f4\r\n$ sudo journalctl --disk-usage\r\n\r\n# \u6307\u5b9a\u65e5\u5fd7\u6587\u4ef6\u5360\u636e\u7684\u6700\u5927\u7a7a\u95f4\r\n$ sudo journalctl --vacuum-size=1G\r\n\r\n# \u6307\u5b9a\u65e5\u5fd7\u6587\u4ef6\u4fdd\u5b58\u591a\u4e45\r\n$ sudo journalctl --vacuum-time=1years<\/pre>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>\u8f6c\u81eahttp:\/\/www.ruanyifeng.com\/blog\/2016\/03 &hellip;<\/p>\n<p class=\"read-more\"><a href=\"https:\/\/www.sulabs.net\/?p=778\">\u7ee7\u7eed\u9605\u8bfb &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":779,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-778","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.sulabs.net\/index.php?rest_route=\/wp\/v2\/posts\/778","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sulabs.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sulabs.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sulabs.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sulabs.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=778"}],"version-history":[{"count":4,"href":"https:\/\/www.sulabs.net\/index.php?rest_route=\/wp\/v2\/posts\/778\/revisions"}],"predecessor-version":[{"id":785,"href":"https:\/\/www.sulabs.net\/index.php?rest_route=\/wp\/v2\/posts\/778\/revisions\/785"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.sulabs.net\/index.php?rest_route=\/wp\/v2\/media\/779"}],"wp:attachment":[{"href":"https:\/\/www.sulabs.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=778"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sulabs.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=778"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sulabs.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=778"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}