You are on page 1of 3

Starting and Stopping Services

Services and Run Levels


Services and Run Levels
Introduction
Requirements
Starting and stopping services
Checking the status of a service
Exercise: Enabling/disabling services at bootup

Introduction
Oracle Linux 6 uses Upstart, a replacement for the System V init system to start and stop services at boot time. When the system is booted up, it
is configured to reach a certain run-level. Run levels are numbered and define what services should be started or stopped in order to reach that
run level or when transitioning to another run level. For example, run level 3 usually identifies that all services except for the graphical desktop
environment are started. The GUI is started when switching to run level 5. Also, the shutdown of a system is defined as a run level that just stops
all running services and does not start any new ones.
Each service that can be started or stopped comes with an init script, a shell script that performs the appropriate actions to start or stop the
service in question. Init scripts are stored in the directory /etc/init.d/ and accept a number standard options, e.g. start or stop. Depending
on the option, they then perform the requested action. They can either be called directly, or they can be invoked by the init process or the
service utility. Note that some init scripts are only supposed to be started once at system bootup; they don't start a service (a permanently
running process) per se, but they are being run to perform tasks like configuring the networking subsystem or adjusting certain kernel parameters.

Requirements
These lab exercises focus on services that can be started and stopped at runtime, using the Apache httpd web server as an example. The
general approach of starting/stopping and enabling services applies to most other services (e.g. CUPS print services, Samba file sharing) as well.
In order to perform the exercises outlined below, first ensure that the Apache web server (package httpd) is installed:

[oracle@oraclelinux6 ~]$ rpm -q httpd


httpd-2.2.15-26.0.1.el6.i686

If it is missing, use the yum package manager to install the package, as outlined in the exercises of Package management with RPM and yum:
sudo yum install httpd

Starting and stopping services


To simply start a service at runtime, you can either call the init script in /etc/init.d with the start or stop parameter directly, or you use the
service program, which ensures running an init script in a more predictable/secure environment (by removing most environment variables and
by changing the current working directory to the root directory).
/etc/init.d/<service> start/stop
service <service> start/stop

Example: starting the Apache httpd web server

[oracle@oraclelinux6 ~]$ sudo service httpd start


Starting httpd:

OK

You can now verify that the web server is running by pointing a web browser to its IP address (e.g. http://localhost/, if you are running these
exercises from a local desktop environment).
You should see a default web page similar to this one:
Unable to render embedded object: File (httpd.png) not found.

Checking the status of a service


Properly written init scripts can perform additional tasks to starting and stopping processes. One very common functionality is the status
operation, which checks if a given service is running. The basic command looks as follows:
service <name> status

The following example checks the status of the Apache httpd web server, stops the service and checks the status again.

[oracle@oraclelinux6 ~]$ sudo service httpd status


httpd (pid

4589) is running...

[oracle@oraclelinux6 ~]$ sudo service httpd stop


Stopping httpd:

OK

[oracle@oraclelinux6 ~]$ sudo service httpd status


httpd is stopped

You can verify this by trying to open http://localhost/ in your local browser before and after stopping the httpd service. If the web server isn't up
and running, you should get an error message.

Exercise: Enabling/disabling services at bootup


Calling the init scripts directly or using the service utility only starts or stops services in the running system, but this change is not persistent. If
you want to ensure that a particular service is automatically started at boot time and properly shut down when the system reboots, you need to
add the service's init script to the respective run level. This can either be accomplished by manually adding the respective symbolic links in the
run level's subdirectory (which is complex and error-prone, especially if a service is dependent on other services), or by using the chkconfig
utility, which automates this process.
chkconfig can be used to enable, disable services at system bootup time as well as querying the current runlevel configuration for a given
service (or all of them). The utility also honors the dependencies of services and ensures that these are started in the appropriate order (e.g. that
the network services are started before any services that require a functional network connection).
To check the status of all available services, simply run chkconfig without any options:

[oracle@oraclelinux6 ~]$ chkconfig


[...]
saslauthd
smartd
snmpd
snmptrapd
spice-vdagentd
sshd
sssd
sysstat
udev-post
vboxadd
vboxadd-service
vboxadd-x11
wpa_supplicant
ypbind

0:off
0:off
0:off
0:off
0:off
0:off
0:off
0:off
0:off
0:off
0:off
0:off
0:off
0:off

1:off 2:off 3:off 4:off 5:off 6:off


1:off 2:off 3:off 4:off 5:off 6:off
1:off 2:off 3:off 4:off 5:off 6:off
1:off 2:off 3:off 4:off 5:off 6:off
1:off 2:off 3:off 4:off 5:on 6:off
1:off 2:on 3:on 4:on 5:on 6:off
1:off 2:off 3:off 4:off 5:off 6:off
1:on 2:on 3:on 4:on 5:on 6:off
1:on 2:on 3:on 4:on 5:on 6:off
1:off 2:on 3:on 4:on 5:on 6:off
1:off 2:on 3:on 4:on 5:on 6:off
1:off 2:off 3:on 4:off 5:on 6:off
1:off 2:off 3:off 4:off 5:off 6:off
1:off 2:off 3:off 4:off 5:off 6:off

General syntax to check for an individual service and to enable/disable it:


chkconfig --list <service>
chkconfig <service> on|off

In the example below, the httpd service is currently disabled, as it is usually the default for freshly installed packages:

[oracle@oraclelinux6 ~]$ chkconfig --list httpd


httpd

0:off 1:off 2:off 3:off 4:off 5:off 6:off

Now we use the on option to enable the web server during system bootup and check the configuration again:

[oracle@oraclelinux6 ~]$ sudo chkconfig httpd on


[oracle@oraclelinux6 ~]$ chkconfig --list httpd
httpd

0:off 1:off 2:on 3:on 4:on 5:on 6:off

Verify that the Apache httpd is actually starting on bootup now by rebooting your system. After logging in again, run service httpd status or
open a web browser from the VM desktop and open http://localhost/ it should display the default start page now right away.

You might also like