CentOS/RHEL:检查服务是否正在运行

时间:2020-01-09 10:37:19  来源:igfitidea点击:

我如何确定Centos/RHEL/Fedora Linux服务器上是否运行了MySQL或Apache之类的服务?您需要使用服务命令。它在尽可能可预测的环境中运行System V初始化脚本,删除大多数环境变量并将当前工作目录设置为/。

语法如下:

service SERVER status

或者

/etc/init.d/SERVER status

例子

查找CentOS或RHEL上是否正在运行名为mysqld(MySQL服务器)的服务。
打开终端或使用ssh登录,输入:

# service mysqld status

输出示例:

mysqld (pid  7556) is running...

找出所有服务的状态

service --status-all命令使用status命令按字母顺序运行所有初始化脚本:

# service --status-all

输出示例:

....
...
.....
irqbalance (pid 2183) is running...
iscsi is stopped
iscsid is stopped
Kdump is operational
i5k_amb-isa-0000
Adapter: ISA adapter
Ch. 0 DIMM 0: +63.0°C  (low  = +127.5°C, high = +127.5°C)  
Ch. 0 DIMM 1: +56.5°C  (low  = +127.5°C, high = +127.5°C)  
Ch. 1 DIMM 0: +62.0°C  (low  = +127.5°C, high = +127.5°C)  
Ch. 1 DIMM 1: +49.0°C  (low  = +127.5°C, high = +127.5°C)  
Ch. 2 DIMM 0: +54.0°C  (low  = +127.5°C, high = +127.5°C)  
Ch. 3 DIMM 0: +49.0°C  (low  = +127.5°C, high = +127.5°C)  
 
lo eth0 eth1
rpc.svcgssd is stopped
rpc.mountd (pid 7199) is running...
nfsd (pid 7262 7261 7260 7259 7258 7257 7256 7255) is running...
rpc.rquotad (pid 7195) is running...
rpc.statd (pid  2215) is running...
ntpd (pid  7295) is running...
master (pid  7649) is running...
Process accounting is enabled.
ipmi_msghandler module not loaded.
ipmi_si module not loaded.
ipmi_devintf module not loaded.
/dev/ipmi0 does not exist.
quota_nld is stopped
rdisc is stopped
.....
..

ps或pgrep命令

您可以按照以下方式使用ps或pgrep命令来确定服务是否在RHEL/Centos上正在运行:

# ps aux | grep 'serviceNameHere'
# ps aux | grep 'mysqld'

或者

# pgrep serviceNameHere
## match user name ##
# pgrep -u userName serviceNameHere
# pgrep mysqld
# pgrep -u mysql mysqld

启动服务

假设服务器上没有运行名为httpd的服务,并且您希望启动该服务:

# service httpd status
## if httpd is not running start it ##
# chkconfig httpd on
# service httpd start