This article looks at managing services on Linux servers. We will focus on the command line, and on the systemctl
utility in particular.
Before we dive in, it is worth mentioning that you can perform some tasks via WHM. For instance, the Restart Services interface lets you restart services such as Apache and MySQL/MariaDB. Also, on cPanel servers you can manage services via various restartsrv scripts, as we will discuss after we have had a look at systemctl
.
The way services are managed on RHEL-based Linux distributions such as Centos and CloudLinux has changed significantly in recent years. Traditionally, nearly all Linux operating systems used the SysVinit daemon for system initialisation and managing services. The main contenders to replace SysVInit were Upstart and systemd. Upstart was adopted in RHEL 6 but since RHEL 7 systemd has become the standard.
Determining your system’s initialisation daemon is a little tricky. If you are not sure what init system you are using you can simply inspect PID 1 (as the init system will always have the PID 1):
# ps -p 1 PID TTY TIME CMD 1 ? 02:03:50 systemd
Here, we are using systemd. Unfortunately, SysVinit and Upstart will both identify themselves as init
. To find out if you are running Upstart you can use the strings
utility to inspect the init binary. The below command was run on a Centos 6 server running Upstart:
# strings /sbin/init | grep ^UPSTART UPSTART_JOB=%s UPSTART_INSTANCE=%s UPSTART_STOP_EVENTS UPSTART_EVENTS
As SysVinit and Upstart belong to the RHEL 5 and 6 era we won’t cover them in this article.
You can list all services managed by systemd using the command systemctl list-units
:
# systemctl list-units --type=service | head -3 atd.service loaded active running Job spooling tools auditd.service loaded active running Security Auditing Service avahi-daemon.service loaded active running Avahi mDNS/DNS-SD Stack
The list will be very long, which is why we limited the output to just the first three lines. What we want to focus on here is what the list tells us. The output shows five columms:
You can list services that have a “failed” status using the --state=failed
option:
# systemctl list-units --type=service --state=failed UNIT LOAD ACTIVE SUB DESCRIPTION ● cpanel_php_fpm.service loaded failed failed FPM service for cPanel Daemons ● cphulkd.service loaded failed failed cPanel brute force detector services
Often, a “failed” status simply indicates that a service hasn’t been enabled on the server. You can quickly check whether or not a service has been enabled using the is-enabled
command:
# systemctl is-enabled cpanel_php_fpm.service disabled
Listing service units is particularly useful if you are not sure about the name of a service. For instance, to remind yourself of the name of the FTP service you can pipe the output to grep
:
# systemctl list-units --type=service | grep ftp pure-ftpd.service loaded failed failed Pure-FTPd
Services that should be started during the boot process need to be enabled. If, say, you have installed Docker then you can enable the Docker daemon as follows:
# systemctl enable docker.service
To disable the Docker service you can use:
# systemctl disable docker.service
Starting and stopping a service is equally straight forward:
# systemctl start docker.service
# systemctl stop docker.service
If you have changed a configuration file, such as Apache’s httpd.conf file, you need to reload the file. You can do that with the reload
command:
# systemctl reload httpd.service
Not all services are able to reload their configuration file without a restart. To make sure that the changes are applied you can instead use the reload-or-restart
command. This will reload the configuration if possible, or otherwise restart the service.
# systemctl reload-or-restart httpd.service
And as you probably guessed, you can also simply restart a service:
# systemctl restart httpd.service
The status
command prints runtime information, including whether or not the service is active and what the main process ID is. The output also includes the most recent log file data.
# systemctl status exim ● exim.service - Exim is a Mail Transport Agent, which is the program that moves mail from one machine to another. Loaded: loaded (/etc/systemd/system/exim.service; enabled; vendor preset: disabled) Active: active (running) since Wed 2019-09-11 00:42:11 BST; 2 weeks 0 days ago Main PID: 13499 (exim) ...
There is a lot more to systemd to what we have covered here. In particular, we haven’t talked about unit files and targets. If you want to learn more about systemd, the official documentation is available from freedesktop.org.
Although you can use systemctl
on cPanel servers it is recommended to use cPanel’s restartsrv scripts. The scripts were introduced when RHEL made the switch from Upstart to systemd, and the scripts will work correctly regardless of the init system used.
You can see the scripts that are available by listing the contents of the /scripts directory:
# ls -1 /scripts/restartsrv* | head -3 /scripts/restartsrv /scripts/restartsrv_apache /scripts/restartsrv_apache_php_fpm
The scripts can do more than just restart services. The following options are available:
You can learn more about the scripts and the available options via the cPanel documentation.