{"id":1250,"date":"2019-12-31T13:14:50","date_gmt":"2019-12-31T05:14:50","guid":{"rendered":"https:\/\/blog.jsjs.org\/?p=1250"},"modified":"2019-12-31T13:14:50","modified_gmt":"2019-12-31T05:14:50","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:\/\/blog.jsjs.org\/?p=1250","title":{"rendered":"Systemd \u5165\u95e8\u6559\u7a0b\uff1a\u547d\u4ee4\u7bc7"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u4f5c\u8005\uff1a&nbsp;<a href=\"http:\/\/www.ruanyifeng.com\/\">\u962e\u4e00\u5cf0<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u65e5\u671f\uff1a\u00a0<a href=\"http:\/\/www.ruanyifeng.com\/blog\/2016\/03\/\"><abbr title=\"2016-03-07T14:37:24+08:00\">2016\u5e743\u6708 7\u65e5<\/abbr><\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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\" rel=\"noreferrer noopener\">\u5b88\u62a4\u8fdb\u7a0b<\/a>\uff0c\u5df2\u6210\u4e3a\u5927\u591a\u6570\u53d1\u884c\u7248\u7684\u6807\u51c6\u914d\u7f6e\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\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\" rel=\"noreferrer noopener\">\u4e0b\u4e00\u7bc7<\/a>\u4ecb\u7ecd\u5982\u4f55\u7528\u4e8e\u5b9e\u6218\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"http:\/\/www.ruanyifeng.com\/blogimg\/asset\/2016\/bg2016030701.gif\" alt=\"\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e00\u3001\u7531\u6765<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5386\u53f2\u4e0a\uff0c<a href=\"http:\/\/www.ruanyifeng.com\/blog\/2013\/08\/linux_boot_process.html\" target=\"_blank\" rel=\"noreferrer noopener\">Linux \u7684\u542f\u52a8<\/a>\u4e00\u76f4\u91c7\u7528<a href=\"https:\/\/en.wikipedia.org\/wiki\/Init\" target=\"_blank\" rel=\"noreferrer noopener\"><code>init<\/code><\/a>\u8fdb\u7a0b\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0b\u9762\u7684\u547d\u4ee4\u7528\u6765\u542f\u52a8\u670d\u52a1\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n$ sudo \/etc\/init.d\/apache2 start\n# \u6216\u8005\n$ service apache2 start\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u79cd\u65b9\u6cd5\u6709\u4e24\u4e2a\u7f3a\u70b9\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\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\n\n\n<p class=\"wp-block-paragraph\">\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\n\n\n<h2 class=\"wp-block-heading\">\u4e8c\u3001Systemd \u6982\u8ff0<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">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\n\n\n<p class=\"wp-block-paragraph\">\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\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"http:\/\/www.ruanyifeng.com\/blogimg\/asset\/2016\/bg2016030702.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\uff08\u4e0a\u56fe\u4e3a Systemd \u4f5c\u8005&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Lennart_Poettering\" target=\"_blank\" rel=\"noreferrer noopener\">Lennart Poettering<\/a>\uff09<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\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\n\n\n<pre class=\"wp-block-code\"><code>\n$ systemctl --version\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0a\u9762\u7684\u547d\u4ee4\u67e5\u770b Systemd \u7684\u7248\u672c\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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&#8221;keep simple, keep stupid&#8221;\u7684<a href=\"http:\/\/www.ruanyifeng.com\/blog\/2009\/06\/unix_philosophy.html\" target=\"_blank\" rel=\"noreferrer noopener\">Unix \u54f2\u5b66<\/a>\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"http:\/\/www.ruanyifeng.com\/blogimg\/asset\/2016\/bg2016030703.png\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\uff08\u4e0a\u56fe\u4e3a Systemd \u67b6\u6784\u56fe\uff09<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e09\u3001\u7cfb\u7edf\u7ba1\u7406<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">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\n\n\n<h3 class=\"wp-block-heading\">3.1 systemctl<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>systemctl<\/code>\u662f Systemd \u7684\u4e3b\u547d\u4ee4\uff0c\u7528\u4e8e\u7ba1\u7406\u7cfb\u7edf\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n# \u91cd\u542f\u7cfb\u7edf\n$ sudo systemctl reboot\n\n# \u5173\u95ed\u7cfb\u7edf\uff0c\u5207\u65ad\u7535\u6e90\n$ sudo systemctl poweroff\n\n# CPU\u505c\u6b62\u5de5\u4f5c\n$ sudo systemctl halt\n\n# \u6682\u505c\u7cfb\u7edf\n$ sudo systemctl suspend\n\n# \u8ba9\u7cfb\u7edf\u8fdb\u5165\u51ac\u7720\u72b6\u6001\n$ sudo systemctl hibernate\n\n# \u8ba9\u7cfb\u7edf\u8fdb\u5165\u4ea4\u4e92\u5f0f\u4f11\u7720\u72b6\u6001\n$ sudo systemctl hybrid-sleep\n\n# \u542f\u52a8\u8fdb\u5165\u6551\u63f4\u72b6\u6001\uff08\u5355\u7528\u6237\u72b6\u6001\uff09\n$ sudo systemctl rescue\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3.2 systemd-analyze<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>systemd-analyze<\/code>\u547d\u4ee4\u7528\u4e8e\u67e5\u770b\u542f\u52a8\u8017\u65f6\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n# \u67e5\u770b\u542f\u52a8\u8017\u65f6\n$ systemd-analyze\n\n# \u67e5\u770b\u6bcf\u4e2a\u670d\u52a1\u7684\u542f\u52a8\u8017\u65f6\n$ systemd-analyze blame\n\n# \u663e\u793a\u7011\u5e03\u72b6\u7684\u542f\u52a8\u8fc7\u7a0b\u6d41\n$ systemd-analyze critical-chain\n\n# \u663e\u793a\u6307\u5b9a\u670d\u52a1\u7684\u542f\u52a8\u6d41\n$ systemd-analyze critical-chain atd.service\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3.3 hostnamectl<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>hostnamectl<\/code>\u547d\u4ee4\u7528\u4e8e\u67e5\u770b\u5f53\u524d\u4e3b\u673a\u7684\u4fe1\u606f\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n# \u663e\u793a\u5f53\u524d\u4e3b\u673a\u7684\u4fe1\u606f\n$ hostnamectl\n\n# \u8bbe\u7f6e\u4e3b\u673a\u540d\u3002\n$ sudo hostnamectl set-hostname rhel7\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3.4 localectl<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>localectl<\/code>\u547d\u4ee4\u7528\u4e8e\u67e5\u770b\u672c\u5730\u5316\u8bbe\u7f6e\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n# \u67e5\u770b\u672c\u5730\u5316\u8bbe\u7f6e\n$ localectl\n\n# \u8bbe\u7f6e\u672c\u5730\u5316\u53c2\u6570\u3002\n$ sudo localectl set-locale LANG=en_GB.utf8\n$ sudo localectl set-keymap en_GB\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3.5 timedatectl<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>timedatectl<\/code>\u547d\u4ee4\u7528\u4e8e\u67e5\u770b\u5f53\u524d\u65f6\u533a\u8bbe\u7f6e\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n# \u67e5\u770b\u5f53\u524d\u65f6\u533a\u8bbe\u7f6e\n$ timedatectl\n\n# \u663e\u793a\u6240\u6709\u53ef\u7528\u7684\u65f6\u533a\n$ timedatectl list-timezones\n\n# \u8bbe\u7f6e\u5f53\u524d\u65f6\u533a\n$ sudo timedatectl set-timezone America\/New_York\n$ sudo timedatectl set-time YYYY-MM-DD\n$ sudo timedatectl set-time HH:MM:SS\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3.6 loginctl<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>loginctl<\/code>\u547d\u4ee4\u7528\u4e8e\u67e5\u770b\u5f53\u524d\u767b\u5f55\u7684\u7528\u6237\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n# \u5217\u51fa\u5f53\u524dsession\n$ loginctl list-sessions\n\n# \u5217\u51fa\u5f53\u524d\u767b\u5f55\u7528\u6237\n$ loginctl list-users\n\n# \u5217\u51fa\u663e\u793a\u6307\u5b9a\u7528\u6237\u7684\u4fe1\u606f\n$ loginctl show-user ruanyf\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u56db\u3001Unit<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">4.1 \u542b\u4e49<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">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\n\n\n<p class=\"wp-block-paragraph\">Unit \u4e00\u5171\u5206\u621012\u79cd\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Service unit\uff1a\u7cfb\u7edf\u670d\u52a1<\/li><li>Target unit\uff1a\u591a\u4e2a Unit \u6784\u6210\u7684\u4e00\u4e2a\u7ec4<\/li><li>Device Unit\uff1a\u786c\u4ef6\u8bbe\u5907<\/li><li>Mount Unit\uff1a\u6587\u4ef6\u7cfb\u7edf\u7684\u6302\u8f7d\u70b9<\/li><li>Automount Unit\uff1a\u81ea\u52a8\u6302\u8f7d\u70b9<\/li><li>Path Unit\uff1a\u6587\u4ef6\u6216\u8def\u5f84<\/li><li>Scope Unit\uff1a\u4e0d\u662f\u7531 Systemd \u542f\u52a8\u7684\u5916\u90e8\u8fdb\u7a0b<\/li><li>Slice Unit\uff1a\u8fdb\u7a0b\u7ec4<\/li><li>Snapshot Unit\uff1aSystemd \u5feb\u7167\uff0c\u53ef\u4ee5\u5207\u56de\u67d0\u4e2a\u5feb\u7167<\/li><li>Socket Unit\uff1a\u8fdb\u7a0b\u95f4\u901a\u4fe1\u7684 socket<\/li><li>Swap Unit\uff1aswap \u6587\u4ef6<\/li><li>Timer Unit\uff1a\u5b9a\u65f6\u5668<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><code>systemctl list-units<\/code>\u547d\u4ee4\u53ef\u4ee5\u67e5\u770b\u5f53\u524d\u7cfb\u7edf\u7684\u6240\u6709 Unit \u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n# \u5217\u51fa\u6b63\u5728\u8fd0\u884c\u7684 Unit\n$ systemctl list-units\n\n# \u5217\u51fa\u6240\u6709Unit\uff0c\u5305\u62ec\u6ca1\u6709\u627e\u5230\u914d\u7f6e\u6587\u4ef6\u7684\u6216\u8005\u542f\u52a8\u5931\u8d25\u7684\n$ systemctl list-units --all\n\n# \u5217\u51fa\u6240\u6709\u6ca1\u6709\u8fd0\u884c\u7684 Unit\n$ systemctl list-units --all --state=inactive\n\n# \u5217\u51fa\u6240\u6709\u52a0\u8f7d\u5931\u8d25\u7684 Unit\n$ systemctl list-units --failed\n\n# \u5217\u51fa\u6240\u6709\u6b63\u5728\u8fd0\u884c\u7684\u3001\u7c7b\u578b\u4e3a service \u7684 Unit\n$ systemctl list-units --type=service\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">4.2 Unit \u7684\u72b6\u6001<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>systemctl status<\/code>\u547d\u4ee4\u7528\u4e8e\u67e5\u770b\u7cfb\u7edf\u72b6\u6001\u548c\u5355\u4e2a Unit \u7684\u72b6\u6001\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n# \u663e\u793a\u7cfb\u7edf\u72b6\u6001\n$ systemctl status\n\n# \u663e\u793a\u5355\u4e2a Unit \u7684\u72b6\u6001\n$ sysystemctl status bluetooth.service\n\n# \u663e\u793a\u8fdc\u7a0b\u4e3b\u673a\u7684\u67d0\u4e2a Unit \u7684\u72b6\u6001\n$ systemctl -H root@rhel7.example.com status httpd.service\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\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\n\n\n<pre class=\"wp-block-code\"><code>\n# \u663e\u793a\u67d0\u4e2a Unit \u662f\u5426\u6b63\u5728\u8fd0\u884c\n$ systemctl is-active application.service\n\n# \u663e\u793a\u67d0\u4e2a Unit \u662f\u5426\u5904\u4e8e\u542f\u52a8\u5931\u8d25\u72b6\u6001\n$ systemctl is-failed application.service\n\n# \u663e\u793a\u67d0\u4e2a Unit \u670d\u52a1\u662f\u5426\u5efa\u7acb\u4e86\u542f\u52a8\u94fe\u63a5\n$ systemctl is-enabled application.service\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">4.3 Unit \u7ba1\u7406<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\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\n\n\n<pre class=\"wp-block-code\"><code>\n# \u7acb\u5373\u542f\u52a8\u4e00\u4e2a\u670d\u52a1\n$ sudo systemctl start apache.service\n\n# \u7acb\u5373\u505c\u6b62\u4e00\u4e2a\u670d\u52a1\n$ sudo systemctl stop apache.service\n\n# \u91cd\u542f\u4e00\u4e2a\u670d\u52a1\n$ sudo systemctl restart apache.service\n\n# \u6740\u6b7b\u4e00\u4e2a\u670d\u52a1\u7684\u6240\u6709\u5b50\u8fdb\u7a0b\n$ sudo systemctl kill apache.service\n\n# \u91cd\u65b0\u52a0\u8f7d\u4e00\u4e2a\u670d\u52a1\u7684\u914d\u7f6e\u6587\u4ef6\n$ sudo systemctl reload apache.service\n\n# \u91cd\u8f7d\u6240\u6709\u4fee\u6539\u8fc7\u7684\u914d\u7f6e\u6587\u4ef6\n$ sudo systemctl daemon-reload\n\n# \u663e\u793a\u67d0\u4e2a Unit \u7684\u6240\u6709\u5e95\u5c42\u53c2\u6570\n$ systemctl show httpd.service\n\n# \u663e\u793a\u67d0\u4e2a Unit \u7684\u6307\u5b9a\u5c5e\u6027\u7684\u503c\n$ systemctl show -p CPUShares httpd.service\n\n# \u8bbe\u7f6e\u67d0\u4e2a Unit \u7684\u6307\u5b9a\u5c5e\u6027\n$ sudo systemctl set-property httpd.service CPUShares=500\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">4.4 \u4f9d\u8d56\u5173\u7cfb<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">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\n\n\n<p class=\"wp-block-paragraph\"><code>systemctl list-dependencies<\/code>\u547d\u4ee4\u5217\u51fa\u4e00\u4e2a Unit \u7684\u6240\u6709\u4f9d\u8d56\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n$ systemctl list-dependencies nginx.service\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\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\n\n\n<pre class=\"wp-block-code\"><code>\n$ systemctl list-dependencies --all nginx.service\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e94\u3001Unit \u7684\u914d\u7f6e\u6587\u4ef6<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">5.1 \u6982\u8ff0<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\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\n\n\n<p class=\"wp-block-paragraph\">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\n\n\n<p class=\"wp-block-paragraph\"><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\n\n\n<pre class=\"wp-block-code\"><code>\n$ sudo systemctl enable clamd@scan.service\n# \u7b49\u540c\u4e8e\n$ sudo ln -s '\/usr\/lib\/systemd\/system\/clamd@scan.service' '\/etc\/systemd\/system\/multi-user.target.wants\/clamd@scan.service'\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\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\n\n\n<p class=\"wp-block-paragraph\">\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\n\n\n<pre class=\"wp-block-code\"><code>\n$ sudo systemctl disable clamd@scan.service\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\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\n\n\n<h3 class=\"wp-block-heading\">5.2 \u914d\u7f6e\u6587\u4ef6\u7684\u72b6\u6001<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>systemctl list-unit-files<\/code>\u547d\u4ee4\u7528\u4e8e\u5217\u51fa\u6240\u6709\u914d\u7f6e\u6587\u4ef6\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n# \u5217\u51fa\u6240\u6709\u914d\u7f6e\u6587\u4ef6\n$ systemctl list-unit-files\n\n# \u5217\u51fa\u6307\u5b9a\u7c7b\u578b\u7684\u914d\u7f6e\u6587\u4ef6\n$ systemctl list-unit-files --type=service\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e2a\u547d\u4ee4\u4f1a\u8f93\u51fa\u4e00\u4e2a\u5217\u8868\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n$ systemctl list-unit-files\n\nUNIT FILE              STATE\nchronyd.service        enabled\nclamd@.service         static\nclamd@scan.service     disabled\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e2a\u5217\u8868\u663e\u793a\u6bcf\u4e2a\u914d\u7f6e\u6587\u4ef6\u7684\u72b6\u6001\uff0c\u4e00\u5171\u6709\u56db\u79cd\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>enabled\uff1a\u5df2\u5efa\u7acb\u542f\u52a8\u94fe\u63a5<\/li><li>disabled\uff1a\u6ca1\u5efa\u7acb\u542f\u52a8\u94fe\u63a5<\/li><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><li>masked\uff1a\u8be5\u914d\u7f6e\u6587\u4ef6\u88ab\u7981\u6b62\u5efa\u7acb\u542f\u52a8\u94fe\u63a5<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\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\n\n\n<pre class=\"wp-block-code\"><code>\n$ systemctl status bluetooth.service\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\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\n\n\n<pre class=\"wp-block-code\"><code>\n$ sudo systemctl daemon-reload\n$ sudo systemctl restart httpd.service\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">5.3 \u914d\u7f6e\u6587\u4ef6\u7684\u683c\u5f0f<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\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\n\n\n<p class=\"wp-block-paragraph\"><code>systemctl cat<\/code>\u547d\u4ee4\u53ef\u4ee5\u67e5\u770b\u914d\u7f6e\u6587\u4ef6\u7684\u5185\u5bb9\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n$ systemctl cat atd.service\n\n[Unit]\nDescription=ATD daemon\n\n[Service]\nType=forking\nExecStart=\/usr\/bin\/atd\n\n[Install]\nWantedBy=multi-user.target\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\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\n\n\n<p class=\"wp-block-paragraph\">\u6bcf\u4e2a\u533a\u5757\u5185\u90e8\u662f\u4e00\u4e9b\u7b49\u53f7\u8fde\u63a5\u7684\u952e\u503c\u5bf9\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n[Section]\nDirective1=value\nDirective2=value\n\n. . .\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6ce8\u610f\uff0c\u952e\u503c\u5bf9\u7684\u7b49\u53f7\u4e24\u4fa7\u4e0d\u80fd\u6709\u7a7a\u683c\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">5.4 \u914d\u7f6e\u6587\u4ef6\u7684\u533a\u5757<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><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\n\n\n<ul class=\"wp-block-list\"><li><code>Description<\/code>\uff1a\u7b80\u77ed\u63cf\u8ff0<\/li><li><code>Documentation<\/code>\uff1a\u6587\u6863\u5730\u5740<\/li><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><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><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><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><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><li><code>Conflicts<\/code>\uff1a\u8fd9\u91cc\u6307\u5b9a\u7684 Unit \u4e0d\u80fd\u4e0e\u5f53\u524d Unit \u540c\u65f6\u8fd0\u884c<\/li><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><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><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><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\n\n\n<ul class=\"wp-block-list\"><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 +&nbsp;<code>.wants<\/code>\u540e\u7f00\u6784\u6210\u7684\u5b50\u76ee\u5f55\u4e2d<\/li><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 +&nbsp;<code>.required<\/code>\u540e\u7f00\u6784\u6210\u7684\u5b50\u76ee\u5f55\u4e2d<\/li><li><code>Alias<\/code>\uff1a\u5f53\u524d Unit \u53ef\u7528\u4e8e\u542f\u52a8\u7684\u522b\u540d<\/li><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><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><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\n\n\n<ul class=\"wp-block-list\"><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><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><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><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><li><code>Type=dbus<\/code>\uff1a\u5f53\u524d\u670d\u52a1\u901a\u8fc7D-Bus\u542f\u52a8<\/li><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><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><li><code>ExecStart<\/code>\uff1a\u542f\u52a8\u5f53\u524d\u670d\u52a1\u7684\u547d\u4ee4<\/li><li><code>ExecStartPre<\/code>\uff1a\u542f\u52a8\u5f53\u524d\u670d\u52a1\u4e4b\u524d\u6267\u884c\u7684\u547d\u4ee4<\/li><li><code>ExecStartPost<\/code>\uff1a\u542f\u52a8\u5f53\u524d\u670d\u52a1\u4e4b\u540e\u6267\u884c\u7684\u547d\u4ee4<\/li><li><code>ExecReload<\/code>\uff1a\u91cd\u542f\u5f53\u524d\u670d\u52a1\u65f6\u6267\u884c\u7684\u547d\u4ee4<\/li><li><code>ExecStop<\/code>\uff1a\u505c\u6b62\u5f53\u524d\u670d\u52a1\u65f6\u6267\u884c\u7684\u547d\u4ee4<\/li><li><code>ExecStopPost<\/code>\uff1a\u505c\u6b62\u5f53\u5176\u670d\u52a1\u4e4b\u540e\u6267\u884c\u7684\u547d\u4ee4<\/li><li><code>RestartSec<\/code>\uff1a\u81ea\u52a8\u91cd\u542f\u5f53\u524d\u670d\u52a1\u95f4\u9694\u7684\u79d2\u6570<\/li><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><li><code>TimeoutSec<\/code>\uff1a\u5b9a\u4e49 Systemd \u505c\u6b62\u5f53\u524d\u670d\u52a1\u4e4b\u524d\u7b49\u5f85\u7684\u79d2\u6570<\/li><li><code>Environment<\/code>\uff1a\u6307\u5b9a\u73af\u5883\u53d8\u91cf<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">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\" rel=\"noreferrer noopener\">\u5b98\u65b9\u6587\u6863<\/a>\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u516d\u3001Target<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\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\n\n\n<p class=\"wp-block-paragraph\">\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&#8221;\u72b6\u6001\u70b9&#8221;\uff0c\u542f\u52a8\u67d0\u4e2a Target \u5c31\u597d\u6bd4\u542f\u52a8\u5230\u67d0\u79cd\u72b6\u6001\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\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\n\n\n<pre class=\"wp-block-code\"><code>\n# \u67e5\u770b\u5f53\u524d\u7cfb\u7edf\u7684\u6240\u6709 Target\n$ systemctl list-unit-files --type=target\n\n# \u67e5\u770b\u4e00\u4e2a Target \u5305\u542b\u7684\u6240\u6709 Unit\n$ systemctl list-dependencies multi-user.target\n\n# \u67e5\u770b\u542f\u52a8\u65f6\u7684\u9ed8\u8ba4 Target\n$ systemctl get-default\n\n# \u8bbe\u7f6e\u542f\u52a8\u65f6\u7684\u9ed8\u8ba4 Target\n$ sudo systemctl set-default multi-user.target\n\n# \u5207\u6362 Target \u65f6\uff0c\u9ed8\u8ba4\u4e0d\u5173\u95ed\u524d\u4e00\u4e2a Target \u542f\u52a8\u7684\u8fdb\u7a0b\uff0c\n# systemctl isolate \u547d\u4ee4\u6539\u53d8\u8fd9\u79cd\u884c\u4e3a\uff0c\n# \u5173\u95ed\u524d\u4e00\u4e2a Target \u91cc\u9762\u6240\u6709\u4e0d\u5c5e\u4e8e\u540e\u4e00\u4e2a Target \u7684\u8fdb\u7a0b\n$ sudo systemctl isolate multi-user.target\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Target \u4e0e \u4f20\u7edf RunLevel \u7684\u5bf9\u5e94\u5173\u7cfb\u5982\u4e0b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\nTraditional runlevel      New target name     Symbolically linked to...\n\nRunlevel 0           |    runlevel0.target -> poweroff.target\nRunlevel 1           |    runlevel1.target -> rescue.target\nRunlevel 2           |    runlevel2.target -> multi-user.target\nRunlevel 3           |    runlevel3.target -> multi-user.target\nRunlevel 4           |    runlevel4.target -> multi-user.target\nRunlevel 5           |    runlevel5.target -> graphical.target\nRunlevel 6           |    runlevel6.target -> reboot.target\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5b83\u4e0e<code>init<\/code>\u8fdb\u7a0b\u7684\u4e3b\u8981\u5dee\u522b\u5982\u4e0b\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><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><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><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\n\n\n<h2 class=\"wp-block-heading\">\u4e03\u3001\u65e5\u5fd7\u7ba1\u7406<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">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\n\n\n<p class=\"wp-block-paragraph\"><code>journalctl<\/code>\u529f\u80fd\u5f3a\u5927\uff0c\u7528\u6cd5\u975e\u5e38\u591a\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n# \u67e5\u770b\u6240\u6709\u65e5\u5fd7\uff08\u9ed8\u8ba4\u60c5\u51b5\u4e0b \uff0c\u53ea\u4fdd\u5b58\u672c\u6b21\u542f\u52a8\u7684\u65e5\u5fd7\uff09\n$ sudo journalctl\n\n# \u67e5\u770b\u5185\u6838\u65e5\u5fd7\uff08\u4e0d\u663e\u793a\u5e94\u7528\u65e5\u5fd7\uff09\n$ sudo journalctl -k\n\n# \u67e5\u770b\u7cfb\u7edf\u672c\u6b21\u542f\u52a8\u7684\u65e5\u5fd7\n$ sudo journalctl -b\n$ sudo journalctl -b -0\n\n# \u67e5\u770b\u4e0a\u4e00\u6b21\u542f\u52a8\u7684\u65e5\u5fd7\uff08\u9700\u66f4\u6539\u8bbe\u7f6e\uff09\n$ sudo journalctl -b -1\n\n# \u67e5\u770b\u6307\u5b9a\u65f6\u95f4\u7684\u65e5\u5fd7\n$ sudo journalctl --since=\"2012-10-30 18:17:16\"\n$ sudo journalctl --since \"20 min ago\"\n$ sudo journalctl --since yesterday\n$ sudo journalctl --since \"2015-01-10\" --until \"2015-01-11 03:00\"\n$ sudo journalctl --since 09:00 --until \"1 hour ago\"\n\n# \u663e\u793a\u5c3e\u90e8\u7684\u6700\u65b010\u884c\u65e5\u5fd7\n$ sudo journalctl -n\n\n# \u663e\u793a\u5c3e\u90e8\u6307\u5b9a\u884c\u6570\u7684\u65e5\u5fd7\n$ sudo journalctl -n 20\n\n# \u5b9e\u65f6\u6eda\u52a8\u663e\u793a\u6700\u65b0\u65e5\u5fd7\n$ sudo journalctl -f\n\n# \u67e5\u770b\u6307\u5b9a\u670d\u52a1\u7684\u65e5\u5fd7\n$ sudo journalctl \/usr\/lib\/systemd\/systemd\n\n# \u67e5\u770b\u6307\u5b9a\u8fdb\u7a0b\u7684\u65e5\u5fd7\n$ sudo journalctl _PID=1\n\n# \u67e5\u770b\u67d0\u4e2a\u8def\u5f84\u7684\u811a\u672c\u7684\u65e5\u5fd7\n$ sudo journalctl \/usr\/bin\/bash\n\n# \u67e5\u770b\u6307\u5b9a\u7528\u6237\u7684\u65e5\u5fd7\n$ sudo journalctl _UID=33 --since today\n\n# \u67e5\u770b\u67d0\u4e2a Unit \u7684\u65e5\u5fd7\n$ sudo journalctl -u nginx.service\n$ sudo journalctl -u nginx.service --since today\n\n# \u5b9e\u65f6\u6eda\u52a8\u663e\u793a\u67d0\u4e2a Unit \u7684\u6700\u65b0\u65e5\u5fd7\n$ sudo journalctl -u nginx.service -f\n\n# \u5408\u5e76\u663e\u793a\u591a\u4e2a Unit \u7684\u65e5\u5fd7\n$ journalctl -u nginx.service -u php-fpm.service --since today\n\n# \u67e5\u770b\u6307\u5b9a\u4f18\u5148\u7ea7\uff08\u53ca\u5176\u4ee5\u4e0a\u7ea7\u522b\uff09\u7684\u65e5\u5fd7\uff0c\u5171\u67098\u7ea7\n# 0: emerg\n# 1: alert\n# 2: crit\n# 3: err\n# 4: warning\n# 5: notice\n# 6: info\n# 7: debug\n$ sudo journalctl -p err -b\n\n# \u65e5\u5fd7\u9ed8\u8ba4\u5206\u9875\u8f93\u51fa\uff0c--no-pager \u6539\u4e3a\u6b63\u5e38\u7684\u6807\u51c6\u8f93\u51fa\n$ sudo journalctl --no-pager\n\n# \u4ee5 JSON \u683c\u5f0f\uff08\u5355\u884c\uff09\u8f93\u51fa\n$ sudo journalctl -b -u nginx.service -o json\n\n# \u4ee5 JSON \u683c\u5f0f\uff08\u591a\u884c\uff09\u8f93\u51fa\uff0c\u53ef\u8bfb\u6027\u66f4\u597d\n$ sudo journalctl -b -u nginx.serviceqq\n -o json-pretty\n\n# \u663e\u793a\u65e5\u5fd7\u5360\u636e\u7684\u786c\u76d8\u7a7a\u95f4\n$ sudo journalctl --disk-usage\n\n# \u6307\u5b9a\u65e5\u5fd7\u6587\u4ef6\u5360\u636e\u7684\u6700\u5927\u7a7a\u95f4\n$ sudo journalctl --vacuum-size=1G\n\n# \u6307\u5b9a\u65e5\u5fd7\u6587\u4ef6\u4fdd\u5b58\u591a\u4e45\n$ sudo journalctl --vacuum-time=1years\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\uff08\u5b8c\uff09<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f5c\u8005\uff1a&nbsp;\u962e\u4e00\u5cf0 \u65e5\u671f\uff1a\u00a02016\u5e743\u6708 7\u65e5 Systemd \u662f Linux \u7cfb\u7edf\u5de5\u5177\uff0c\u7528\u6765\u542f\u52a8\u5b88\u62a4\u8fdb\u7a0b\uff0c\u5df2\u6210\u4e3a\u5927\u591a\u6570\u53d1\u884c\u7248\u7684\u6807\u51c6\u914d\u7f6e\u3002 \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\u4e0b\u4e00\u7bc7\u4ecb\u7ecd\u5982\u4f55\u7528\u4e8e\u5b9e\u6218\u3002 \u4e00\u3001\u7531\u6765 \u5386\u53f2\u4e0a\uff0cLinux \u7684\u542f\u52a8\u4e00\u76f4\u91c7\u7528init\u8fdb\u7a0b\u3002 \u4e0b\u9762\u7684\u547d\u4ee4\u7528\u6765\u542f\u52a8\u670d\u52a1\u3002 \u8fd9\u79cd\u65b9\u6cd5\u6709\u4e24\u4e2a\u7f3a\u70b9\u3002 \u4e00\u662f\u542f\u52a8\u65f6\u95f4\u957f\u3002init\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 \u4e8c\u662f\u542f\u52a8\u811a\u672c\u590d\u6742\u3002init\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 \u4e8c\u3001Systemd \u6982\u8ff0 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 \u6839\u636e [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1250","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=\/wp\/v2\/posts\/1250","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1250"}],"version-history":[{"count":0,"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=\/wp\/v2\/posts\/1250\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1250"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1250"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1250"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}