systemd 服务操作

高频命令

systemctl status nginx
sudo systemctl start nginx
sudo systemctl stop nginx
sudo systemctl reload nginx
sudo systemctl restart nginx
sudo systemctl enable nginx
sudo systemctl disable nginx
journalctl -u nginx --since '30 minutes ago'

修改 unit 文件后:

sudo systemctl daemon-reload
sudo systemctl restart myapp

daemon-reload 只是让 systemd 重新读取 unit,不等于重启业务。

失败排查

systemctl status myapp --no-pager
journalctl -u myapp -b --no-pager -n 200
systemctl show myapp -p ExecMainStatus -p Result

不要通过连续 restart 清除现象。先查看退出码、权限、工作目录、环境变量、端口和日志。

SSH 特别规则

修改 SSH 服务前,先运行配置测试并保持现有会话:

sudo sshd -t

另开第二个终端验证新连接后,才能关闭旧窗口。