602 字
3 分钟
systemd 使用笔记
常用命令
列出已配置为开机启用的 service unit
systemctl list-unit-files --type=service --state=enabled查看当前正在运行的服务:
systemctl list-units --type=service --state=running分别判断开机启动和当前运行状态:
systemctl is-enabled nginx.servicesystemctl is-active nginx.service开启,关闭,重启,设为开机自启,查看状态
systemctl start nginxsystemctl stop nginxsystemctl restart nginxsystemctl enable nginxsystemctl status nginx创建一个自定义的Springboot service
cat <<'EOF' >/etc/systemd/system/hello.service[Unit]Description=Spring Boot HelloWorldAfter=syslog.targetAfter=network.target
[Service]User=usernameType=simpleExecStart=/usr/bin/java -jar /root/hello.jarRestart=alwaysStandardOutput=syslogStandardError=syslogSyslogIdentifier=helloworld
[Install]WantedBy=multi-user.targetEOFservice文件的位置(详情参考 man systemd.unit)
Table 1. Load path when running in system mode (--system). ┌────────────────────┬─────────────────────────────┐ │Path │ Description │ ├────────────────────┼─────────────────────────────┤ │/etc/systemd/system │ Local configuration │ ├────────────────────┼─────────────────────────────┤ │/run/systemd/system │ Runtime units │ ├────────────────────┼─────────────────────────────┤ │/lib/systemd/system │ Units of installed packages │ └────────────────────┴─────────────────────────────┘[!NOTE] 注释
/etc/systemd/system文件夹里的是本地创建的服务。/lib/systemd/system路径里的是软件包创建的服务。(/usr/lib/systemd/system)/run/systemd/system用于运行时临时配置
修改 unit 后重新加载
systemctl daemon-reload只要创建或修改了 unit 文件,就应该运行它。它不会自动重启服务,通常还需要:
systemctl restart hello.service检查 unit 文件语法
systemd-analyze verify /etc/systemd/system/hello.service查看失败的 unit
systemctl --failed仅查看失败的服务:
systemctl --failed --type=service查看服务日志
journalctl -u hello.servicejournalctl -u hello.service -bjournalctl -u hello.service -fjournalctl -u hello.service --since "30 minutes ago"清除 failed 状态
修复问题后,如果服务仍显示 failed:
systemctl reset-failed hello.service然后重新启动:
systemctl start hello.service推荐使用 drop-in,而不是修改软件包文件
不要直接修改:
/lib/systemd/system/nginx.service因为软件升级可能覆盖修改。
使用:
systemctl edit nginx.service例如:
[Service]Restart=on-failureRestartSec=5s查看合并后的结果:
systemctl cat nginx.service恢复并删除本地覆盖:
systemctl revert nginx.service查看/lib/systemd/system路径下的服务是由什么软件包创建的
dpkg-query -S /lib/systemd/system/* | sort -uOutput:
dpkg-query: no path found matching pattern /lib/systemd/system/ecs_mq.servicedpkg-query: no path found matching pattern /lib/systemd/system/SplunkForwarder.servicedpkg-query: no path found matching pattern /lib/systemd/system/system-systemd\x2dcryptsetup.sliceaccountsservice: /lib/systemd/system/accounts-daemon.serviceapparmor: /lib/systemd/system/apparmor.serviceapt: /lib/systemd/system/apt-daily.serviceapt: /lib/systemd/system/apt-daily.timerapt: /lib/systemd/system/apt-daily-upgrade.serviceapt: /lib/systemd/system/apt-daily-upgrade.timerat: /lib/systemd/system/atd.serviceauditd: /lib/systemd/system/auditd.servicebase-files: /lib/systemd/system/motd-news.servicebase-files: /lib/systemd/system/motd-news.timer[!NOTE] 注释
dpkg-query找不到这些文件,表示 dpkg 数据库中没有软件包声明拥有该路径。它们可能是手工创建、由第三方安装脚本创建、由软件包安装脚本动态生成,或由 systemd generator 生成。仅仅使用dpkg -i手动安装.deb,通常不会导致dpkg-query -S无法找到包内文件。
systemd 使用笔记
https://blog.drcharon.com/zh/posts/systemd-note/