Friday, 22 February 2013

RHCSA: Controlling System Services


Here's another post that I'm doing to to help me remember the details of controlling system services on a RHEL system. Perhaps you'll find it useful too.

On Red Hat systems we can find our services in /etc/init.d/. They are really just script files that can be configured to run services at system startup.  There are a number of things we can do with services, such as starting, stopping, checking service status and reloading/rereading config files.

Check Service Status

We can do this in one of two ways:

 #/etc/init.d/ httpd status  

or

 #service httpd status  

We can use the first method on many different Linux systems. The second method is "The Red Hat Way" so you may find it doesn't work on all systems.

Starting,Stopping and Restarting a Service

Again, there are two ways:

 #/etc/init.d/ httpd start  
 #/etc/init.d/ httpd stop  

and "The Red Hat Way":

 #service httpd start  
 #service httpd stop  


We can also restart a service in the same way, just use the word "restart" at the end of the command.

If a change has been made to a config file and we want our service to reread the config, we can use:

 #/etc/init.d/httpd reload  

or

 #/service httpd reload  


Check Service Runlevels

We can use the chkconfig command to see if a service is set to run at startup. If we want to see all of the services and their runlevels, we would use
 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:on     4:off     5:on     6:off  
 abrt-oops        0:off     1:off     2:off     3:on     4:off     5:on     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  
 auditd          0:off     1:off     2:on     3:on     4:on     5:on     6:off  
 autofs          0:off     1:off     2:off     3:on     4:on     5:on     6:off  
 avahi-daemon       0:off     1:off     2:off     3:on     4:on     5:on     6:off  
 bluetooth        0:off     1:off     2:off     3:on     4:on     5:on     6:off  
 certmonger        0:off     1:off     2:off     3:on     4:on     5:on     6:off  
 cgconfig         0:off     1:off     2:on     3:on     4:on     5:on     6:off  
 cgred          0:off     1:off     2:off     3:off     4:off     5:off     6:off  
 cpuspeed         0:off     1:on     2:on     3:on     4:on     5:on     6:off  
 crond          0:off     1:off     2:on     3:on     4:on     5:on     6:off  
 cups           0:off     1:off     2:on     3:on     4:on     5:on     6:off  
 dnsmasq         0:off     1:off     2:off     3:off     4:off     5:off     6:off  
 ebtables         0:off     1:off     2:off     3:off     4:off     5:off     6:off  
 firstboot        0:off     1:off     2:off     3:off     4:off     5:off     6:off  
 haldaemon        0:off     1:off     2:off     3:on     4:on     5:on     6:off  
 httpd          0:off     1:off     2:off     3:on     4:on     5:on     6:off  
 ip6tables        0:off     1:off     2:on     3:on     4:on     5:on     6:off  
 ipsec          0:off     1:off     2:off     3:off     4:off     5:off     6:off  
 iptables         0:off     1:off     2:on     3:on     4:on     5:on     6:off  
 irqbalance        0:off     1:off     2:off     3:on     4:on     5:on     6:off  
 iscsi          0:off     1:off     2:off     3:on     4:on     5:on     6:off  
 iscsid          0:off     1:off     2:off     3:on     4:on     5:on     6:off  
 kdump          0:off     1:off     2:off     3:off     4:off     5:off     6:off  
 ksm           0:off     1:off     2:off     3:on     4:on     5:on     6:off  
 ksmtuned         0:off     1:off     2:off     3:on     4:on     5:on     6:off  
 libvirt-guests      0:off     1:off     2:on     3:on     4:on     5:on     6:off  
 libvirtd         0:off     1:off     2:off     3:on     4:on     5:on     6:off  
 lvm2-monitor       0:off     1:on     2:on     3:on     4:on     5:on     6:off  
 mdmonitor        0:off     1:off     2:on     3:on     4:on     5:on     6:off  
 messagebus        0:off     1:off     2:on     3:on     4:on     5:on     6:off  
 netconsole        0:off     1:off     2:off     3:off     4:off     5:off     6:off  
 netfs          0:off     1:off     2:off     3:on     4:on     5:on     6:off  
 network         0:off     1:off     2:on     3:on     4:on     5:

(Above output is truncated)

If we want to look at a single service, we would use the same command but also specify the service we are interested in:

 # chkconfig --list httpd  
 httpd          0:off     1:off     2:off     3:off     4:off     5:off     6:off  

We can see here that the httpd service is not currently configured to run at any runlevels. If we wanted to make a change so that it runs at renlevels 3,4 and 5:

 # chkconfig --level 345 httpd on  

And then take another look:

 # chkconfig --list httpd  
 httpd          0:off     1:off     2:off     3:on     4:on     5:on     6:off  


We can see now that the httpd service will start automatically on runlevels 3,4, and 5. If we wanted to change this would could issue the chkconfig --level  command with the runlevel we want to remove httpd from and the off command.

GUI Service Control

There is also a GUI tool that can be used to make changes to system services, if you want to use it then you will need to install system-config-services from the software repositories. Although it is there to be used if you really feel it necessary, I recommend learning and sticking to the command line tools as described above.  They are quick and simple to get to grips with and you don't know when you'll come face to face with a system which has no GUI. If you know how to use the above commands you should be able to configure services on any Red Hat system you encounter.

No comments:

Post a Comment