Blog

03/06/2019

CentOS / RHEL: Check If A Service Is Running Or Not



How do I find out if a service such as MySQL or Apache running on my Centos/RHEL/Fedora Linux server?

You need to use service command. It runs a System V init script in as predictable environment as possible, removing most environment variables and with current working directory set to /. The syntax is as follows:

service SERVER status

OR

/etc/init.d/SERVER status

Examples

Find, out if a service called mysqld (MySQL server) is running on CentOS OR RHEL. Open a terminal or login using ssh, enter:
# service mysqld status

Sample outputs:

mysqld (pid  7556) is running...

Find out status of all services

The service --status-all command runs all init scripts, in alphabetical order, with the status command:
# service --status-all
Sample outputs:

....
...
.....
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)  
 
coretemp-isa-0000
Adapter: ISA adapter
Core 0:      +37.0°C  (high = +78.0°C, crit = +100.0°C)  
Core 1:      +38.0°C  (high = +78.0°C, crit = +100.0°C)  
Core 2:      +34.0°C  (high = +78.0°C, crit = +100.0°C)  
Core 3:      +38.0°C  (high = +78.0°C, crit = +100.0°C)  
 
coretemp-isa-0001
Adapter: ISA adapter
Core 0:      +40.0°C  (high = +78.0°C, crit = +100.0°C)  
Core 1:      +40.0°C  (high = +78.0°C, crit = +100.0°C)  
Core 2:      +39.0°C  (high = +78.0°C, crit = +100.0°C)  
Core 3:      +39.0°C  (high = +78.0°C, crit = +100.0°C)  
 
w83627hf-isa-0290
Adapter: ISA adapter
in0:         +4.08 V  (min =  +0.00 V, max =  +4.08 V)   
in1:         +4.08 V  (min =  +0.00 V, max =  +4.08 V)   
in2:         +4.08 V  (min =  +2.82 V, max =  +3.79 V)   ALARM
in3:         +3.07 V  (min =  +4.08 V, max =  +4.05 V)   ALARM
in4:         +3.12 V  (min =  +4.08 V, max =  +4.08 V)   ALARM
in5:         +3.15 V  (min =  +4.08 V, max =  +4.06 V)   ALARM
in6:         +3.20 V  (min =  +4.08 V, max =  +4.06 V)   ALARM
in7:         +3.28 V  (min =  +3.82 V, max =  +4.06 V)   ALARM
in8:         +3.28 V  (min =  +4.06 V, max =  +4.06 V)   ALARM
fan1:          0 RPM  (min =    0 RPM, div = 2)
fan2:          0 RPM  (min =    0 RPM, div = 2)
fan3:          0 RPM  (min =    0 RPM, div = 2)
temp1:       -48.0°C  (high = +60.0°C, hyst = +55.0°C)  sensor = thermistor
temp2:       -48.0°C  (high = +80.0°C, hyst = +75.0°C)  sensor = thermistor
temp3:       -48.0°C  (high = +80.0°C, hyst = +75.0°C)  sensor = thermistor
cpu0_vid:   +1.419 V
beep_enable:enabled
 
lvmetad is stopped
mdmonitor is stopped
memcached (pid  45560) is running...
messagebus (pid  7066) is running...
mysqld (pid  7556) is running...
netconsole module not loaded
Configured devices:
lo eth0 eth1
Currently active devices:
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 or pgrep command

You can use ps or pgrep command as follows to find out if service is running or not on RHEL/Centos:
# ps aux | grep 'serviceNameHere'
# ps aux | grep 'mysqld'

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

Starting service

Say if a service called httpd is not running on server and you wish to start the same:
# service httpd status
## if httpd is not running start it ##
# chkconfig httpd on
# service httpd start

References

For more information see man pages – ps(1).

Posted by: SXI ADMIN

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

14/08/2019

How to KVM, QEMU start or stop virtual machine from command line (CLI)

KVM or Kernel Based Virtual Machine is a popular virtualization technology. It allows you to run virtual guest machines over a host machine. To start...
14/08/2019

How to Docker backup Saving and restoring your volumes

Running a Docker volume backup First, we spin up a temporary container, and we mount the backup folder and the target Docker volume to this container....
12/08/2019

How to Start and Enable Firewalld on CentOS 7

In this article, we discuss how to start and enable firewalld. It is highly recommended that you have a firewall protecting your server.Pre-Flight CheckThese...