systemctl 命令可直接查、启、停、重启及设置开机自启,核心操作无需 root(用户级服务除外),不依赖脚本路径;status、is-active、is-enabled 用于状态查询,start/stop/restart/reload 用于即时控制,enable/disable 管理开机自启,daemon-reload 刷新配置,journalctl 和 list-unit-files 辅助排查。

直接用 systemctl 命令就能查、启、停、重启和启用开机自启,核心操作不依赖 root(用户级服务除外),也不需要记一堆脚本路径。
查看服务当前状态
运行以下命令可快速确认服务是否在运行、上次启动时间、最近日志片段:
-
systemctl status nginx—— 查看 nginx 服务详细状态 -
systemctl is-active nginx—— 只返回active或inactive,适合脚本判断 -
systemctl is-enabled nginx—— 返回enabled/disabled,表示是否设为开机自启
启停与重载服务
这些操作即时生效,无需重启系统:
-
systemctl start nginx—— 启动服务(不设开机自启) -
systemctl stop nginx—— 立即终止进程 -
systemctl restart nginx—— 先 stop 再 start -
systemctl reload nginx—— 仅重载配置(如修改了/etc/nginx/nginx.conf),服务不断连 -
systemctl reload-or-restart nginx—— 自动判断:支持 reload 就 reload,否则 fallback 到 restart
设置开机自启或禁用自启
这会影响系统启动时是否自动拉起该服务:
-
sudo systemctl enable nginx—— 创建软链接到/etc/systemd/system/multi-user.target.wants/,下次开机自动启动 -
sudo systemctl disable nginx—— 移除该链接,开机不再自动启动 -
sudo systemctl daemon-reload—— 修改了.service文件后必须执行,让 systemd 重新读取配置
排查常见问题
服务起不来?别急着重装,先看这几项:
- 用
systemctl status nginx查Active:行和末尾几行日志,常有明确报错(如端口被占、配置语法错误) - 用
journalctl -u nginx.service -n 50 -f实时追踪最新 50 行日志 - 检查服务是否真正加载:
systemctl list-unit-files --type=service | grep nginx - 确认 unit 文件位置是否正确:通常在
/usr/lib/systemd/system/(发行版自带)或/etc/systemd/system/(手动添加)


















