百度
360搜索
搜狗搜索

linux启动停止服务命令行详细介绍

在 Linux 中,启动 / 停止服务的命令因 init 系统(管理服务的核心组件)不同而有所差异。以下是主流场景的命令总结:

一、
systemd 系统(主流,如 Ubuntu 16.04+、CentOS 7+)

使用 systemctl 命令,支持服务管理、状态查看、开机自启等:

操作 命令示例(以 Nginx 为例) 说明
启动服务 sudo systemctl start nginx 立即启动服务
停止服务 sudo systemctl stop nginx 立即停止服务
重启服务 sudo systemctl restart nginx 重启服务(支持平滑重启)
查看状态 sudo systemctl status nginx 检查服务运行状态(含日志)
开机自启 sudo systemctl enable nginx 启用开机自启
禁用开机自启 sudo systemctl disable nginx 关闭开机自启
查看所有服务 systemctl list-units --type=service 列出所有服务及其状态

示例
启动 Nginx 并设置开机自启:
bash

 systemctl start nginx   systemctl  nginx

二、
SysVinit 系统(旧版,如 CentOS 6、Debian 7 及更早)

使用 service 命令(兼容部分 systemd 系统):

操作 命令示例(以 Apache 为例) 说明
启动服务 sudo service apache2 start 立即启动服务
停止服务 sudo service apache2 stop 立即停止服务
重启服务 sudo service apache2 restart 重启服务
查看状态 sudo service apache2 status 检查服务状态

补充:服务脚本路径通常为 /etc/init.d/,可直接执行:
bash

 /etc/init.d/apache2 start  

三、
通用技巧

  1. 强制停止(适用于卡住的服务):bash
     systemctl  nginx  

  2. 查看服务日志:bash
     journalctl  nginx  

    /var/log/apache2/error.log

  3. 区分发行版
    • Ubuntu/Debian 新系统:优先用 systemctl
    • CentOS 6 及以下:用 service
    • 不确定时,先执行 systemctl --version,若有输出版本号则为 systemd。

四、
常见服务名称对照

服务 systemctl 命令 service 命令(旧)
Nginx nginx nginx
Apache httpd(CentOS) apache2(Debian/Ubuntu)
MySQL/MariaDB mysql(或 mariadb mysql(或 mariadb
Docker docker docker

根据实际服务名称替换命令中的关键词即可。

如果需要更具体的服务操作(如自定义服务脚本),可以进一步说明场景~

阅读更多 >>>  linux怎么测试tomcat

网站数据信息

"linux启动停止服务命令行"浏览人数已经达到23次,如你需要查询该站的相关权重信息,可以点击进入"Chinaz数据" 查询。更多网站价值评估因素如:linux启动停止服务命令行的访问速度、搜索引擎收录以及索引量、用户体验等。 要评估一个站的价值,最主要还是需要根据您自身的需求,如网站IP、PV、跳出率等!