在 Linux 系统中,重启服务的方式取决于使用的初始化系统(init system)。以下是主流初始化系统下的正确操作方式:
# 重启单个服务
sudo systemctl restart <service_name> # 例如:sudo systemctl restart nginx
# 查看服务状态
sudo systemctl status <service_name>
# 若服务无响应,可先停止再启动
sudo systemctl stop <service_name>
sudo systemctl start <service_name>
关键选项:
- --now
:同时启用/禁用服务(如 sudo systemctl disable --now nginx
)
- daemon-reload
:修改服务配置后需执行:
bash
sudo systemctl daemon-reload
sudo systemctl restart <service_name>
# 通过 service 命令
sudo service <service_name> restart # 例如:sudo service httpd restart
# 直接调用初始化脚本
sudo /etc/init.d/<service_name> restart
sudo
或 root
权限。bash
systemctl list-unit-files | grep enabled # systemd 下查看所有启用服务
service --status-all # SysVinit 下查看服务
bash
journalctl -u <service_name> -xe # systemd
tail -n 50 /var/log/<service_name>.log # 通用
ps -ef | grep <service>
找到进程 ID,再 kill -9 <PID>
。nginx -t
)。sudo systemctl reload nginx
),避免中断连接。根据系统版本选择对应方法,优先使用 systemctl
以保持操作一致性。