Linux 启动服务与停止服务方法

2次阅读

linux服务管理以systemd为主,支持status、start、stop、enable等命令查看状态、启停服务及设置自启;旧系统兼容sysv init的service和chkconfig命令。

Linux 启动服务与停止服务方法

Linux 中管理服务主要靠 systemd(现代主流发行版如 centos 7+、ubuntu 16.04+、debian 9+ 等),少量旧系统仍用 SysV init 或 Upstart。以下以 systemd 为主,兼顾兼容性说明。

查看服务状态

确认服务是否存在、是否运行、是否开机自启:

  • systemctl status 服务名 —— 查看详细状态(如 systemctl status sshsystemctl status nginx
  • systemctl list-units –type=service –state=running —— 列出当前正在运行的服务
  • systemctl is-active 服务名 —— 返回 activeinactive(适合脚本判断)
  • systemctl is-enabled 服务名 —— 查看是否设置为开机启动(enabled/disabled

启动与停止服务

即时启停,不影响开机自启设置:

  • systemctl start 服务名 —— 启动服务(如 systemctl start httpd
  • systemctl stop 服务名 —— 停止服务
  • systemctl restart 服务名 —— 重启(常用于配置修改后生效)
  • systemctl reload 服务名 —— 重载配置(不中断连接,如 Nginx 支持,但 apache 需用 restart

设置开机自启与禁用自启

控制服务在系统启动时是否自动运行:

  • systemctl enable 服务名 —— 启用开机自启(创建软链接到 /etc/systemd/system/multi-user.target.wants/
  • systemctl disable 服务名 —— 禁用开机自启(移除对应软链接)
  • systemctl mask 服务名 —— 彻底禁止启动(创建指向 /dev/NULL 的硬链接,需 unmask 才能恢复)

兼容旧系统:SysV init(如 CentOS 6、早期 Debian)

若系统未使用 systemd,可用传统命令:

  • service 服务名 start/stop/restart/status(如 service mysqld status
  • chkconfig 服务名 on/off —— 设置开机自启(CentOS/RHEL 6)
  • update-rc.d 服务名 enable/disable —— Debian/Ubuntu 旧版方式
text=ZqhQzanResources