systemd 服务管理速查表
把 systemd 服务管理、状态查看、日志排查和 timer 定时任务按组整理成一页,服务起停和查日志时直接对照。
返回 系统与运维服务管理 6
systemctl start nginx启动服务
systemctl stop nginx停止服务
systemctl restart nginx重启服务,会断连接
systemctl reload nginx平滑重载配置,不断连接,不是所有服务都支持
systemctl enable nginx设置开机自启,加 --now 同时启动
systemctl disable nginx取消开机自启
状态查看 5
systemctl status nginx查看服务状态和最近日志,最常用
systemctl is-enabled nginx查看是否开机自启
systemctl is-active nginx查看当前是否运行
systemctl list-units --type=service列出所有已加载的服务单元
systemctl list-units --failed列出失败的服务,排查启动异常
日志排查 5
journalctl -u nginx查看指定服务日志
journalctl -u nginx -f实时跟踪服务日志
journalctl --since "10 min ago" -u nginx查看最近 10 分钟日志
journalctl -p err -b查看本次启动以来的 error 及以上级别日志
journalctl --disk-usage查看 journal 日志占用磁盘大小
timer 定时任务 5
systemctl list-timers列出所有定时任务,替代 crontab -l
systemctl status <timer>查看指定 timer 状态和下次执行时间
systemctl start <timer> && systemctl enable <timer>启用并设置开机自启 timer
journalctl -u <timer>查看 timer 触发的服务日志
systemd-analyze time查看本次启动耗时
💡 提示
- reload 是平滑重载配置不会断连接,restart 会重启进程;不是所有服务都支持 reload,nginx 支持。
- journalctl 日志默认会轮转,--disk-usage 太大时用 journalctl --vacuum-size=100M 清理。
- timer 比 crontab 更可控,能在 journalctl 里看执行日志,且支持依赖关系。