Title: systemd · Issue #132 · codcodog/Blog · GitHub
Open Graph Title: systemd · Issue #132 · codcodog/Blog
X Title: systemd · Issue #132 · codcodog/Blog
Description: systemd systemd 是 Linux 控制系统和管理服务的工具,基本上所有的发行版都支持使用. 基础 启动 / 停止 # 启动服务 $ systemctl start application.service # 停止服务 $ systemctl stop application.service # 重启服务 $ systemctl restart application.service # 查看服务状态 $ systemctl status applicati...
Open Graph Description: systemd systemd 是 Linux 控制系统和管理服务的工具,基本上所有的发行版都支持使用. 基础 启动 / 停止 # 启动服务 $ systemctl start application.service # 停止服务 $ systemctl stop application.service # 重启服务 $ systemctl restart application.servi...
X Description: systemd systemd 是 Linux 控制系统和管理服务的工具,基本上所有的发行版都支持使用. 基础 启动 / 停止 # 启动服务 $ systemctl start application.service # 停止服务 $ systemctl stop application.service # 重启服务 $ systemctl restart application.servi...
Opengraph URL: https://github.com/codcodog/Blog/issues/132
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"systemd","articleBody":"systemd\r\n========\r\n\r\n`systemd` 是 Linux 控制系统和管理服务的工具,基本上所有的发行版都支持使用.\r\n\r\n### 基础\r\n\r\n#### 启动 / 停止\r\n\r\n```bash\r\n# 启动服务\r\n$ systemctl start application.service\r\n\r\n# 停止服务\r\n$ systemctl stop application.service\r\n\r\n# 重启服务\r\n$ systemctl restart application.service\r\n\r\n# 查看服务状态\r\n$ systemctl status application.service\r\n\r\n# 杀死服务的所有子进程\r\n$ systemctl kill application.service\r\n```\r\n\r\n#### 重新加载配置\r\n```bash\r\n# 重新加载某服务配置文件\r\n$ systemctl reload application.service\r\n\r\n# 重新加载所有 unit 服务的配置文件\r\n$ systemctl daemon-reload\r\n```\r\n\r\n##### `reload` 和 `daemon-reload` 的区别\r\n\r\n`reload` 是重新加载某服务的配置,例如:apache 有 `httpd.conf` 配置, `reload` 的时候,`systemd` 会发送一个 `SIGHUP` 信号让 `apache` 重新加载 `httpd.conf` 配置. \r\n\r\n`daemon-reload` 则是重新加载 `systemd` 的配置文件,例如:某服务 `application.service` 文件在 `/etc/systemd/systemd/` 目录下,设置5秒超时时间,则需要在 `application.service` 文件添加 `TimeoutSec`\r\n```\r\n$ systemctl cat application.service\r\n[Unit]\r\nDescription=ATD daemon\r\n\r\n[Service]\r\nType=forking\r\nExecStart=/usr/bin/atd\r\nTimeoutSec=5\r\n\r\n[Install]\r\nWantedBy=multi-user.target\r\n```\r\n修改完之后,使用 `daemon-reload` 重新加载下 `systemd` 服务文件配置,则5秒超时时间才会生效。\r\n\u003e `/etc/systemd/systemd/` 目录下文件发生变更之后,都应该 `daemon-reload` 一下\r\n\r\n#### 开机启动\r\n\r\n```bash\r\n# 开机启动\r\n$ systemctl enable application.service\r\n\r\n# 禁止开机启动\r\n$ systemctl disable application.service\r\n```\r\n\r\n`enable` 通常是从 `/lib/systemd/system/` 或者 `/etc/systemd/system/` \r\n创建一个软链接到 `/etc/systemd/system/some_target.target.wants`\r\n```\r\n~ $ ll /etc/systemd/system/multi-user.target.wants/\r\ntotal 0\r\nlrwxrwxrwx 1 root root 40 Feb 13 18:25 openntpd.service -\u003e /usr/lib/systemd/system/openntpd.service\r\nlrwxrwxrwx 1 root root 40 Feb 13 18:19 remote-fs.target -\u003e /usr/lib/systemd/system/remote-fs.target\r\n```\r\n\r\n#### 查看日志\r\n\r\n```bash\r\n# 查看系统所有服务日志\r\n$ journalctl\r\n\r\n# 查看某服务日志\r\n$ journalctl -u application.service\r\n\r\n# 实时滚动日志\r\n$ journalctl -u application.service -f\r\n\r\n# 查看尾部最新日志,不指定,默认10行\r\n$ journalctl -u application.service -n 20\r\n```\r\n\r\n### 配置文件\r\n\r\n配置文件主要在 `/usr/lib/systemd/system/` 或者 `/etc/systemd/system/`.\r\n\r\n```\r\n$ systemctl cat sshd.service\r\n\r\n[Unit]\r\nDescription=OpenSSH server daemon\r\nDocumentation=man:sshd(8) man:sshd_config(5)\r\nAfter=network.target sshd-keygen.service\r\nWants=sshd-keygen.service\r\n\r\n[Service]\r\nEnvironmentFile=/etc/sysconfig/sshd\r\nExecStart=/usr/sbin/sshd -D $OPTIONS\r\nExecReload=/bin/kill -HUP $MAINPID\r\nType=simple\r\nKillMode=process\r\nRestart=on-failure\r\nRestartSec=42s\r\n\r\n[Install]\r\nWantedBy=multi-user.target\r\n```\r\n\u003e 配置文件的区块名和字段名,都是大小写敏感\r\n\r\n#### [Unit] 块\r\n\r\n- `Description` 简短描述\r\n- `Documentation` 文档地址\r\n- `Requires` 当前 Unit 依赖的其他 Unit,如果它们没有运行,当前 Unit 会启动失败\r\n- `Wants` 与当前 Unit 配合的其他 Unit,如果它们没有运行,当前 Unit 不会启动失败\r\n- `BindsTo` 与Requires类似,它指定的 Unit 如果退出,会导致当前 Unit 停止运行\r\n- `Before` 如果该字段指定的 Unit 也要启动,那么必须在当前 Unit 之后启动\r\n- `After` 如果该字段指定的 Unit 也要启动,那么必须在当前 Unit 之前启动\r\n- `Conflicts` 这里指定的 Unit 不能与当前 Unit 同时运行\r\n- ...\r\n\r\n#### [Service] 块\r\n\r\n`[Service]` 区块用来 Service 的配置,只有 Service 类型的 Unit 才有这个区块.\r\n\r\n- `Type` 定义了启动时进程的行为\r\n + simple 默认值,执行 ExecStart 指定的命令,启动主进程\r\n + forking 以 fork 方式从父进程创建子进程,创建后父进程会立即退出\r\n + oneshot 一次性进程,Systemd 会等当前服务退出,再继续往下执行\r\n + dbus 当前服务通过D-Bus启动\r\n + notify 当前服务启动完毕,会通知 Systemd,再继续往下执行\r\n + idle 若有其他任务执行完毕,当前服务才会运行\r\n- `ExecStart` 启动当前服务的命令\r\n- `ExecStartPre` 启动当前服务之前执行的命令\r\n- `ExecStartPost` 启动当前服务之后执行的命令\r\n- `ExecReload` 重启当前服务时执行的命令\r\n- `ExecStop` 停止当前服务时执行的命令\r\n- `ExecStopPost` 停止当前服务之后执行的命令\r\n- `KillMode` Systemd 如何停止服务\r\n + control-group 默认值,当前控制组里面的所有子进程,都会被杀掉\r\n + process 只杀主进程\r\n + mixed 主进程将收到 SIGTERM 信号,子进程收到 SIGKILL 信号\r\n + none 没有进程会被杀掉,只是执行服务的 stop 命令\r\n- `RestartSec` 自动重启当前服务间隔的秒数\r\n- `Restart` 定义何种情况 Systemd 会自动重启当前服务\r\n + no 默认值,退出后不会重启\r\n + always 不管是什么退出原因,总是重启\r\n + on-success 只有正常退出时(退出状态码为0),才会重启\r\n + on-failure 非正常退出时(退出状态码非0),包括被信号终止和超时,才会重启\r\n + on-abnormal 只有被信号终止和超时,才会重启\r\n + on-abort 只有在收到没有捕捉到的信号终止时,才会重启\r\n + on-watchdog 超时退出,才会重启\r\n- `TimeoutSec` 定义 Systemd 停止当前服务之前等待的秒数\r\n- `Environment` 指定环境变量\r\n\r\n\r\n#### [Install] 块\r\n\r\n定义如何启动,以及是否开机启动.\r\n\r\n- `WantedBy` 它的值是一个或多个 Target,当前 Unit 激活时(enable)符号链接会放入 `/etc/systemd/system` 目录下面以 Target 名 + .wants后缀构成的子目录中\r\n- `RequiredBy` 它的值是一个或多个 Target,当前 Unit 激活时,符号链接会放入 `/etc/systemd/system` 目录下面以 Target 名 + .required后缀构成的子目录中\r\n- `Alias` 当前 Unit 可用于启动的别名\r\n- `Also` 当前 Unit 激活(enable)时,会被同时激活的其他 Unit\r\n\r\n### Target\r\n\r\n启动计算机的时候,需要启动大量的 Unit。如果每一次启动,都要一一写明本次启动需要哪些 Unit,显然非常不方便。Systemd 的解决方案就是 `Target`.\r\n\r\n简单说,`Target` 就是一个 Unit 组,包含许多相关的 Unit。 启动某个 `Target` 的时候,Systemd 就会启动里面所有的 Unit.\r\n\r\n### 参考\r\n\r\n- man systemctl\r\n- man 5 systemd.unit\r\n- [How To Use Systemctl to Manage Systemd Services and Units](https://www.digitalocean.com/community/tutorials/how-to-use-systemctl-to-manage-systemd-services-and-units)\r\n- [Systemd 入门教程:命令篇](https://www.ruanyifeng.com/blog/2016/03/systemd-tutorial-commands.html)\r\n","author":{"url":"https://github.com/codcodog","@type":"Person","name":"codcodog"},"datePublished":"2022-02-15T15:54:59.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":0},"url":"https://github.com/132/Blog/issues/132"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:397a7d43-f9c3-dc85-7d80-4e888d9654fc |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B2DA:2EEF02:46E7E2:5E3FD0:6A4C2F7C |
| html-safe-nonce | 37abf2b90cce675453ce1bda4d4acfa063b6e8e6811deae7415790a82737b93f |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCMkRBOjJFRUYwMjo0NkU3RTI6NUUzRkQwOjZBNEMyRjdDIiwidmlzaXRvcl9pZCI6IjU4OTAyNTQ5NTkxMTEwNTcyNzYiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | ea03ae62a7c980fd4e923a769244275be501358d19d39f9557d395d20722fcbd |
| hovercard-subject-tag | issue:1138882652 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/codcodog/Blog/132/issue_layout |
| twitter:image | https://opengraph.githubassets.com/9e030de8e105988caaca185d6088d8f92a9e8d0238b713958eefb3dba6d568f1/codcodog/Blog/issues/132 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/9e030de8e105988caaca185d6088d8f92a9e8d0238b713958eefb3dba6d568f1/codcodog/Blog/issues/132 |
| og:image:alt | systemd systemd 是 Linux 控制系统和管理服务的工具,基本上所有的发行版都支持使用. 基础 启动 / 停止 # 启动服务 $ systemctl start application.service # 停止服务 $ systemctl stop application.service # 重启服务 $ systemctl restart application.servi... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | codcodog |
| hostname | github.com |
| expected-hostname | github.com |
| None | 1b6b16d04026f131a36d57e3b01d0f4d26a51800edf48bf5ed0256e0ac905511 |
| turbo-cache-control | no-preview |
| go-import | github.com/codcodog/Blog git https://github.com/codcodog/Blog.git |
| octolytics-dimension-user_id | 18098145 |
| octolytics-dimension-user_login | codcodog |
| octolytics-dimension-repository_id | 75478736 |
| octolytics-dimension-repository_nwo | codcodog/Blog |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 75478736 |
| octolytics-dimension-repository_network_root_nwo | codcodog/Blog |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 9ee13484b32cf23e15fde191da4c9aa47d41201c |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width