Managing services on Linux servers

In this article, we will talk about managing Linux services. A Linux service is a process or a set of processes running in the background. These processes are used most often in executing essential system tasks or running certain kinds of server applications like databases, schedule applications, or even sound system applications.

The services that run on your server occasionally need to be told to start, restart, reload their configuration files or stop the service. You also may want to enable or disable them at during a system boot, which is not related to starting or stopping them at the moment. How this is managed differs from one operating system to another. We have compiled a short list of to manage services using the major Linux operating systems.

Managing services in CentOS 6

CentOS 6, like the most Linux distributions, have adopted SystemV (SysV)-like init scripts to manage their services. The Init process, which has been used since early Unix days,  is used to begin an entire set of processes for a Linux operating system.

The functions around SysV start with the inittab file. The inittab file is located in /etc/ directory and has the information about the default run level and information on the scripts that run at each run level. Those scripts are contained in /etc/init.d/ directory.

For example, /etc/init.d/named directory has scripts responsible to starting and stopping DNS services. If we want to stop any services, we can use the following syntax:

/etc/init.d/(service name here) stop

To stop the Apache web service, use the command:

/etc/init.d/httpd stop

Similar to the stop command, the command to start the Apache web service on your server is as follows:

/etc/init.d/httpd start

The same action can be done with the “service” command:

service service_name start | stop | reload | restart

If you are in need to determine the current status of a service, type the following in your shell window:

service service_name status

A good example of this command would be to determine the status of the Apache web server (httpd)service. To review the current status of your web server, type in the following command:

service httpd status

You may need to review at some points the status of all of your available services at one time. Your services can be reviewed using the command:

service --status-all

Now, let’s go to /etc/ folder and find all files that have rc. in front of them:

etc#  ls -l | grep rc.

Reviewing your shell window, you can see that  every runlevel has a directory of scripts. When we look at them, you will notice some of them contain “S” at the beginning, while others have a “K”.

  • S – “start”: This script will run with the “start” parameter when that specific runlevel is entered.

  • K – “kill’ — this script will run with the “stop” parameter when that specific runlevel is reached.

Init will start processes in order and will wait until the previous script is finished before it moves to the next one. Init does not run scripts simultaneously. As an example, , take a look at a typical /etc/init.d/rc5.d

/etc/rc3.d# ls -ltr

As you can see, Init provides you symlinks to the actual startup scripts allowing you to call directly to them if you need to under different t parameters, like start, stop, restart, etc.

You can change runlevel by using the init command. The telinit command is commonly used for changing runlevel, which passes control to the init command with some parameters. Please be aware that there are some old distributives that do not  support it. For the old distributives, you will need to use the  init command.

You can check the current runlevel of your system using the command:

runlevel

Runlevels in CentOS 6

Runlevel

Description

0

Used to halt the system. This runlevel is reserved and cannot be changed.

1

Used to run in a single-user mode. This runlevel is reserved and cannot be changed.

2

Not used by default. You are free to define it yourself.

3

Used to run in a full multi-user mode with a command-line user interface.

4

Not used by default. You are free to define it yourself.

5

Used to run in a full multi-user mode with a graphical user interface.

6

Used to reboot the system. This runlevel is reserved and cannot be changed.

If you need to change the runlevel, say to “1”, you would use the following command:

init 1

To return runlevel to the default level use the following command:

telinit 3

You have the ability to change the default runlevel by modifying the /etc/inittab file. Near the end of the file, you will find a line similar to the following:

Id:5:initdefault:

Next, let’s check the ntsysv utility. The ntsysv utility is a command-line application with a simple text user interface. It allows you to configure which services are to be started in selected runlevels. To start the ntsysv utility, type “ntsysv” at a shell prompt as root. The utility displays the list of available services (the services from the /etc/rc.d/init.d/ directory) along with their current status and a description obtainable by pressing “F1.”

To enable a service, navigate through the list using the Up and Down arrows keys, and select the service with the Spacebar. Once a service is selected, an asterisk (*) appears in the brackets.

To disable a service, navigate through the list using the Up and Down arrows keys, and toggle the server status with the Spacebar. The asterisk (*) in the brackets will disappear.

After you have completed your changes, use the Tab key to navigate to the Ok button, and confirm the changes by pressing Enter. Please remember that ntsysv does not start or stop the modified service. If you need to start or stop the modified service immediately, use the service command as described above.

By default, the ntsysv utility only affects the current runlevel. To enable or disable services for other runlevels, as root, run the command with the additional –level option followed by numbers from 0 to 6 representing each runlevel you want to configure:

ntsysv --level runlevels

For example, to configure runlevels 3 and 5, type:

ntsysv --level 35

The chkconfig utility is a command-line tool that allows you to specify in which runlevel is used to to start a selected service. It also provides you a list of all available services along with their current setting. Please note that with the exception of command listing, you must have superuser privileges to use this command on your server.

To display a list of all system services (services from the /etc/rc.d/init.d/ directory, as well as the services controlled by xinetd), either type chkconfig –list, or use chkconfig with no additional arguments. You will be presented with an output similar to the following:

To display the current settings for a selected service only, use chkconfig –list followed by the name of the service:

chkconfig --list service_name

For example, to display the current settings for the httpd (Apache web server) service, type:

chkconfig --list httpd

To enable a service in runlevels 2, 3, 4, and 5, type the following at a shell prompt as root:

chkconfig service_name on

For example, to enable the httpd service in these four runlevels, type:

chkconfig httpd on

To enable a service in certain runlevels only, add the –level option followed by numbers from 0 to 6 representing each runlevel in which you want the service to run:

chkconfig service_name on --level runlevels

As an example, you can enable the abrtd (automated bug reporting tool) service in runlevels 3 and 5, type:

chkconfig abrtd on --level 35

To disable a service in runlevels 2, 3, 4, and 5, type the following at a shell prompt as root:

chkconfig service_name off

To disable the httpd service in these four runlevels, type:

chkconfig httpd off

If you need disable a service in certain runlevels only, add the –level option followed by numbers from 0 to 6 representing each runlevel in which you do not want the service to run:

chkconfig service_name off --level runlevels

To disable the abrtd in runlevels 2 and 4, type:

chkconfig abrtd off --level 24

The service will be stopped the next time you enter one of these runlevels. If you need to stop the service immediately, use the service command as described above.

Managing services in Debian 7

On Debian 7, services can be started, stopped and reloaded with the scripts stored in /etc/init.d/  directory. While starting a service or when changing runlevel, those scripts are searched in /etc/rcX.d/ directories where X is the runlevel number.

To manage services, the following syntax is used:

/etc/init.d/(service_name) start | stop | reload | restart

To start an Apache service, use the following command:

/etc/init.d/apache2 start

Starting and stopping daemons in this manner is temporary, and will revert back to its default status after a reboot.To disable the autostart for this service, you can either remove the symbolic links in /etc/rcX.d/SYYapache2 or by using the update-rc.d command:

update-rc.d <daemon> enable|disable

If you are using Apache, use the following command:

update-rc.d apache2 disable

If you want to re-add the service to be started on boot up, you can use the command:

update-rc.d apache2 defaults

As a parameter, you can also pass a priority for boot. For example, to set apache2 to be started with priorities 61 for both Start and Kill,use the following command:

update-rc.d apache2 defaults 61

To set different priorities for when the service is started and/or killed, you can use the command:

update-rc.d apache2 defaults 20 80

This will Start with priority of 20 and kill with a priority 80. Remember that the start priority is always first and kill follows behind it.

As an alternative, you can use the chkconfig utility. For more information on the chkconfig utility, review its description in the CentOS 6 section..

Managing services in Ubuntu 14.04

Ubuntu 14.04 uses Upstart, developed for Ubuntu, as an intended to be a replacement for SystemV init designed to overcome some of the limitations in SystemV, and other dependency-based init systems, Upstart has proven to work well on Ubuntu. There are several large advantages in using upstart over init:

  • better support for hotplug devices

  • cleaner and faster service management

  • works asynchronously, which means it can run multiple scripts simultaneously and can get your system running much faster.

Let’s look at how all this could work. To stop and start services temporarily you can use the service command. Keep in mind this will start or stop your services, but they will be back to their default state on the next boot.

Syntax:
sudo service <servicename> <control>

For Upstart jobs, the service command can send several ‘controls’ like start, stop, restart, status, etc. In fact, this is deprecated for start, stop, status & restart.

Please beware that you do not need to use service command with Upstart. You can instead simply use the following ‘stop’ command:

stop <servicename>

If you wanted to stop mysql you would use the following command:

stop mysql

If you need to check the status of a specific service, like mysql, you would use the command:

status mysql

To start a service you would use the command:

start mysql

So this simplified this process a little bit but we also don’t need all of the symbolic links in the run control directory (/etc/init.d). You can manage the startup of the services through the configuration files in /etc/init directory. As an example, you can go to /etc/init/ directory and review the mysql.conf file for the following:

We have start on runlevel [2345] or whenever we start in an interface that is not a local interface. And the service will be stopped on runlevels [016]. So, this is how we can change runlevels on which MySQL service runs on by default.. The rest of it then really describes how the system should  go about reading MySQL configuration file.

If need to check what  deamons are currently running on your server, use the following command:

initctl list

If you need to disable autostart for a specific service, you have to add the word ‘manual’ to the .conf file. Below is an example adding the word to your file:

echo manual >> /etc/init/squid.conf

After your server has been rebooted, the service can be started using the start command as shown previously.

Apart from this, any service with the .override ending will take precedence over the original service file. You can only start the service manually afterwards. If you do not want this, you simply have to delete the .override file:echo manual > /etc/init/apache2.override

Managing services in new versions of Debian/Ubuntu which are using systemd component.

Any server that is running the newer versions of Debian and Ubuntu run the service systemd. Systemd is a modern init system that is more efficient than systemV.  Some distributions are still in the process of migrating to systemd, but the following section you will learn how to manage services in systemd. And we will do this with MySQL.

Services are just one of the different types of units that can be managed by systemd. If you need to to list all the units that are managed by systemd, review the  /usr/lib/systemd/ directory or use the  command – systemctl:

# systemctl list-units

This command will display all manageable units the system sees. Our team recommends the command “systemctl list-units –type service” so you can view systems currently on the system rather than all the units.

systemctl list-units --type service

To check the status of a service, use the command:

# systemctl  status mysql

This command provides you the service name – The MySQL database server daemon

Now you have the path to the file and its current status. In our example, the service is currently disabled. To start the command, you use the following command:

# systemctl start mysql

Now the service will start running. Now if you look the status, you will see the list of current processes, and so you will notice that the service is still disabled.The service is running but is not enabled during the startup.

To enables a service during startup use the command::

# systemctl enable mysql

You should see that it creates a symbolic link to a multi-user.target, Targets are a replacement to runlevels which no longer exist in systemd. Now, if we check on its status, we will see the service is enabled so it is going to start automatically and it is currently running.

If you need to stop a service, you will use the following command:

# systemctl stop mysql

Since you stopped the service, it will not be displayed as running but will still be shown as enabled. It is still currently enabled since it will start again when your server is rebooted. You will also see that is displays messages inside the loaded file. To view these messages using the command to list all messages:

# systemctl -l status mysql

Use the following command to restart a service:

# systemctl restart mysql

Managing these services are a little different than managing services using CentOS 6 with SystemV.  Here are some other commands that may come handly:

systemctl is-enabled SERVICE

This command allows you to check if a service is currently configured to start or not on the next reboot.

systemctl is-active SERVICE

The command displays all currently active services.

systemctl show SERVICE

A command that provides you all the information about the service.

Was this article helpful?

Yes (14)
No (1)

We're sorry you didn't find this article very helpful. Please help us improve it by leaving your feedback below.

Error