How to list, stop, start, manage services in Centos or Redhat Linux 6 and 7 ?
Today we will look at an interesting topic how to manage services in CentOS/RHEL 6 and 7.
We will look these step by step to understand it better.
1. List all Services in RHEL/Centos/Redhat Linux.
On RHEL 6:
You need to grep the selected service.
[root@nglinux ~]# service --status-all abrt-ccpp hook is not installed abrtd (pid 2176) is running... abrt-dump-oops is stopped acpid (pid 1666) is running... atd (pid 2214) is running... atieventsd (pid 2225) is running... auditd (pid 1444) is running... Stopped cgred is stopped cpuspeed is stopped crond (pid 2199) is running... cupsd (pid 1626) is running... dhcpd is stopped dhcpd is stopped dhcrelay is stopped dhcrelay is stopped dnsmasq is stopped /usr/sbin/fcoemon -- RUNNING, pid=1566 No interfaces created. service firstboot supports chkconfig, but is not referenced in any runlevel (run 'chkconfig --add firstboot') firstboot is not scheduled to run hald (pid 1681) is running... I don't know of any running hsqldb server. htcacheclean is stopped httpd (pid 2187) is running... Table: filter Chain INPUT (policy ACCEPT) num target prot opt source destination 1 ACCEPT all ::/0 ::/0 state RELATED,ESTABLISHED 2 ACCEPT icmpv6 ::/0 ::/0 3 ACCEPT all ::/0 ::/0 4 ACCEPT udp ::/0 fe80::/64 state NEW udp dpt:546 5 ACCEPT tcp ::/0 ::/0 state NEW tcp dpt:22 6 REJECT all ::/0 ::/0 reject-with icmp6-adm-prohibited Chain FORWARD (policy ACCEPT) num target prot opt source destination 1 REJECT all ::/0 ::/0 reject-with icmp6-adm-prohibited Chain OUTPUT (policy ACCEPT) num target prot opt source destination Table: filter Chain INPUT (policy ACCEPT) num target prot opt source destination 1 DROP all -- 192.168.0.5 0.0.0.0/0 Chain FORWARD (policy ACCEPT) num target prot opt source destination Chain OUTPUT (policy ACCEPT) num target prot opt source destination irqbalance is stopped iscsi is stopped iscsid is stopped Kdump is not operational lldpad (pid 1539) is running... lvmetad is stopped dmeventd is stopped mdmonitor is stopped messagebus (pid 1581) is running... multipathd is stopped mysqld (pid 2065) is running... netconsole module not loaded Configured devices: lo eth2 Currently active devices: lo eth1 eth2 NetworkManager (pid 1595) is running... grep: /proc/fs/nfsd/portlist: No such file or directory ntpd is stopped ifdhandler is stopped Waiting for reader attach/detach events... pcscd (pid 1801) is running... portreserve (pid 1468) is running... master dead but pid file exists pppoe-server is stopped Process accounting is disabled. quota_nld is stopped rdisc is stopped Low level hardware support loaded: none found Upper layer protocol modules: ib_ipoib User space access modules: rdma_ucm ib_ucm ib_uverbs ib_umad Connection management modules: rdma_cm ib_cm iw_cm Configured IPoIB interfaces: none Currently active IPoIB interfaces: none [root@nglinux ~]#
On RHEL7 or Cent OS 7:
The service states are: enabled, disabled, and static.
enabled and disabled are the service status and these services can be changed via systemctl command.
static shows the services run as a dependency and are always enabled.
We can’t change the status of static services.
bash-4.2# systemctl list-unit-files UNIT FILE STATE proc-sys-fs-binfmt_misc.automount static dev-hugepages.mount static dev-mqueue.mount static proc-fs-nfsd.mount static proc-sys-fs-binfmt_misc.mount static sys-fs-fuse-connections.mount static sys-kernel-config.mount static sys-kernel-debug.mount static tmp.mount disabled var-lib-nfs-rpc_pipefs.mount static brandbot.path enabled systemd-ask-password-console.path static systemd-ask-password-plymouth.path static systemd-ask-password-wall.path static machine-qemu\x2d1\x2dr285\x2distrs\x2dimaging.rtl.ngelinux.com.scope static arp-ethers.service disabled auth-rpcgss-module.service static autovt@.service enabled blk-availability.service disabled brandbot.service static console-getty.service disabled console-shell.service disabled container-getty@.service static cpupower.service disabled crond.service enabled dbus-org.freedesktop.hostname1.service static dbus-org.freedesktop.import1.service static dbus-org.freedesktop.locale1.service static dbus-org.freedesktop.login1.service static dbus-org.freedesktop.machine1.service static dbus-org.freedesktop.NetworkManager.service enabled dbus-org.freedesktop.nm-dispatcher.service enabled dbus-org.freedesktop.timedate1.service static dbus.service static debug-shell.service disabled dm-event.service static dnsmasq.service disabled dracut-cmdline.service static dracut-initqueue.service static dracut-mount.service static 329 unit files listed. bash-4.2# OR, bash-4.2# systemctl list-units --type service UNIT LOAD ACTIVE SUB DESCRIPTION crond.service loaded active running Command Scheduler dbus.service loaded active running D-Bus System Message Bus epic.service loaded active running EpicClient getty@tty1.service loaded active running Getty on tty1 gssproxy.service loaded active running GSSAPI Proxy Daemon hp-ams.service loaded active running HP Agentless Management Service daemon httpd.service loaded active running The Apache HTTP Server ip6tables.service loaded active exited IPv6 firewall with ip6tables iptables.service loaded active exited IPv4 firewall with iptables irqbalance.service loaded active running irqbalance daemon iscsi-shutdown.service loaded active exited Logout off all iSCSI sessions on shu
2. Check Individual Service status
### On RHEL6 or CentOS 6: [root@nglinux ~]# service crond status crond (pid 2199) is running... ### On RHEL 7 or CentOS 7: bash-4.2# systemctl status crond.service ● crond.service - Command Scheduler Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled) Active: active (running) since Wed 2019-02-27 11:01:17 GMT; 1 weeks 0 days ago Main PID: 9110 (crond) Tasks: 1 CGroup: /system.slice/crond.service └─9110 /usr/sbin/crond -n Feb 27 11:01:17 ngelinux.com systemd[1]: Started Command Scheduler. bash-4.2#
3. Start/Stop a Service.
### ON RHEL 6: [root@nglinux ~]# service crond status crond (pid 2199) is running... [root@nglinux ~]# service crond stop Stopping crond: [ OK ] [root@nglinux ~]# service crond status crond is stopped ### Observe the PID is changed as we have restarted the service. [root@nglinux ~]# service crond start Starting crond: [ OK ] [root@nglinux ~]# service crond status crond (pid 7324) is running... [root@nglinux ~]# ### ON RHEL 7: bash-4.2# systemctl status crond.service ● crond.service - Command Scheduler Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled) Active: active (running) since Wed 2019-02-27 11:01:17 GMT; 1 weeks 0 days ago Main PID: 9110 (crond) Tasks: 1 CGroup: /system.slice/crond.service └─9110 /usr/sbin/crond -n Feb 27 11:01:17 ngelinux.com systemd[1]: Started Command Scheduler. # systemctl stop crond.service # systemctl start crond.service
4. Enable/Disable a Service.
### On RHEL6: [root@nglinux ~]# chkconfig --list NetworkManager 0:off 1:off 2:on 3:on 4:on 5:on 6:off abrt-ccpp 0:off 1:off 2:off 3:off 4:off 5:off 6:off abrtd 0:off 1:off 2:off 3:on 4:off 5:on 6:off acpid 0:off 1:off 2:on 3:on 4:on 5:on 6:off atd 0:off 1:off 2:off 3:on 4:on 5:on 6:off atieventsd 0:off 1:off 2:on 3:on 4:on 5:on 6:off auditd 0:off 1:off 2:on 3:on 4:on 5:on 6:off blk-availability 0:off 1:on 2:on 3:on 4:on 5:on 6:off [root@nglinux ~]# chkconfig --list | grep -i crond crond 0:off 1:off 2:on 3:on 4:on 5:on 6:off [root@nglinux ~]# chkconfig --level 23 crond off [root@nglinux ~]# chkconfig --list | grep -i crond crond 0:off 1:off 2:off 3:off 4:on 5:on 6:off [root@nglinux ~]# ### On RHEL 7: bash-4.2# systemctl is-enabled crond.service enabled # systemctl enable crond.service # systemctl disable crond.service The enable disable status can be seen in the status output also: bash-4.2# systemctl status crond.service ● crond.service - Command Scheduler Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled) Active: active (running) since Wed 2019-02-27 11:01:17 GMT; 1 weeks 0 days ago Main PID: 9110 (crond) Tasks: 1 CGroup: /system.slice/crond.service └─9110 /usr/sbin/crond -n Feb 27 11:01:17 ngelinux.com systemd[1]: Started Command Scheduler. bash-4.2#
5. Check failed Services.
### RHEL 6 ### There is no command way to get failed services in RHEL 6. ### However we can get the stopped services. [root@nglinux ~]# service --status-all | grep -i stopped abrt-dump-oops is stopped Stopped cgred is stopped cpuspeed is stopped dhcpd is stopped dhcpd is stopped dhcrelay is stopped dhcrelay is stopped ### RHEL 7 bash-4.2# systemctl --failed 0 loaded units listed. Pass --all to see loaded but inactive units, too. To show all installed unit files use 'systemctl list-unit-files'. bash-4.2# systemctl --failed --all 0 loaded units listed. To show all installed unit files use 'systemctl list-unit-files'. bash-4.2#