菜鸟科技网

Linux服务启动停止命令有哪些?

在Linux系统中,服务的启动、停止和重启是系统管理的基础操作,不同的Linux发行版可能使用不同的服务管理工具,常见的有Systemd、SysVinit和Upstart,Systemd是目前主流的初始化系统,被广泛应用于CentOS 7+、Ubuntu 16.04+等版本;而SysVinit则多见于 older版本的系统,如CentOS 6和Ubuntu 14.04,以下是针对不同服务管理工具的详细命令说明及使用场景。

Linux服务启动停止命令有哪些?-图1
(图片来源网络,侵删)

Systemd服务管理命令(适用于CentOS 7+、Ubuntu 16.04+等)

Systemd通过systemctl命令管理服务,支持并行启动、依赖关系管理等特性,以下是常用操作:

  1. 查看服务状态

    systemctl status 服务名.service

    systemctl status nginx.service,会显示服务的运行状态、进程ID(PID)、启动时间及最近日志。

  2. 启动服务

    Linux服务启动停止命令有哪些?-图2
    (图片来源网络,侵删)
    systemctl start 服务名.service

    systemctl start nginx,启动Nginx服务(可省略.service后缀)。

  3. 停止服务

    systemctl stop 服务名.service

    systemctl stop nginx,立即停止Nginx服务。

  4. 重启服务

    systemctl restart 服务名.service

    systemctl restart nginx,先停止再启动服务,适用于服务配置更新后重新加载。

  5. 重新加载服务(不中断连接)

    systemctl reload 服务名.service

    systemctl reload nginx,仅重新加载配置文件,不终止现有连接(需服务支持此功能)。

  6. 设置服务开机自启

    systemctl enable 服务名.service

    systemctl enable nginx,创建开机启动符号链接。

  7. 禁用服务开机自启

    systemctl disable 服务名.service

    systemctl disable nginx,删除开机启动符号链接。

  8. 查看服务是否开机自启

    systemctl is-enabled 服务名.service

    返回enabled表示已启用,disabled表示已禁用。

SysVinit服务管理命令(适用于CentOS 6、Ubuntu 14.04等)

SysVinit通过servicechkconfig命令管理服务,功能相对简单:

  1. 查看服务状态

    service 服务名 status

    service nginx status

  2. 启动/停止/重启服务

    service 服务名 start/stop/restart

    service nginx start

  3. 设置开机自启

    chkconfig 服务名 on

    chkconfig nginx on

  4. 禁用开机自启

    chkconfig 服务名 off

    chkconfig nginx off

  5. 查看所有服务的开机启动状态

    chkconfig --list

命令对比与注意事项

以下是Systemd和SysVinit核心命令的对比:

操作场景 Systemd命令 SysVinit命令
启动服务 systemctl start 服务名 service 服务名 start
停止服务 systemctl stop 服务名 service 服务名 stop
重启服务 systemctl restart 服务名 service 服务名 restart
开机自启 systemctl enable 服务名 chkconfig 服务名 on
禁用开机自启 systemctl disable 服务名 chkconfig 服务名 off

注意事项

  • 服务名通常包含.service后缀,但systemctl可省略;
  • 修改服务配置后,需重启或重新加载服务才能生效;
  • 部分服务(如firewalld)需结合特定工具管理,避免直接使用kill命令终止进程。

相关问答FAQs

Q1: 如何查看系统中所有已启动的服务?
A1: 使用以下命令:

  • Systemd系统:systemctl list-units --type=service --state=running,显示正在运行的服务;
  • SysVinit系统:service --status-all,列出所有服务及其状态(表示运行,停止)。

Q2: 服务启动失败时,如何排查问题?
A2: 可通过以下步骤排查:

  1. 查看服务状态:systemctl status 服务名,检查错误日志;
  2. 查看详细日志:journalctl -u 服务名.service -n 50(Systemd)或tail -f /var/log/服务名.log(SysVinit);
  3. 检查服务依赖:systemctl list-dependencies 服务名.service,确认依赖的服务是否正常运行;
  4. 检查端口占用:netstat -tunlp | grep 端口号,确认服务是否因端口冲突无法启动。
分享:
扫描分享到社交APP
上一篇
下一篇