Restarting Services From the Command Line

Posted on by Andrej Walilko | Updated:
Reading Time: 5 minutes

One of the more common management tasks performed on web servers is restarting services (such as your web server daemon, mail daemon, FTP server, or DNS service).

You might need to restart the service(s) if:

  • The service may be crashing or stalled
  • The load the process is causing on the server might be too high
  • A restart might be needed to produce a configuration change to go into effect.

This article is geared towards operating systems using the systemd service.

  • CentOS 6
  • CentOS 7
  • most newer Ubuntu or Debian servers.

Restarting A Service

Note
For this article, general program names are bolded and italicised, binary commands or operands are highlighted in blue.

There are many ways to do anything on a computer, particularly in Linux, and performing service restarts is no exception. Here are three ways to achieve the same action, restarting Apache for typical VPS servers:

  • With the first method, we use the service command, also known as systemd, to issue commands to the desired service. For Apache, this looks like service httpd restart will issue a restart command to Apache. You can check to see if you are able to restart services this way by running which service. CentOS 7 uses this method by default, CentOS 6 has it available, and it is also available on other major operating systems.
  • An older method is to call an init script on the server, known as the upstart method. An upstart command looks like /etc/init.d/httpd restart, which will issue a restart command to Apache also. You can check to see if you can use the init scripts by listing the scripts inside the init.d directory with ls /etc/init.d/. CentOS 5 and 6 used this type of service control by default, as do other aging OSes. Since this is an older method, we won’t discuss it in this article.
  • Finally, on cPanel machines, there are service restart scripts provided by cPanel that begin with /scripts/restartsrv_. For instance, the module to restart Apache is /scripts/restartsrv_httpd. This script performs a clean restart of Apache. If your web server has cPanel installed, these scripts are available for use and will always use the proper restart method for your operating system. This method is particularly helpful when the service name can vary based on the engine in use, such as proftpd or pure-ftpd for the FTP client; both use /scripts/restartsrv_ftpd and cPanel selects the appropriate service automatically. You can list the available scripts by running ls /scripts/restartsrv_*.

Choosing a Script

As demonstrated above, all commands will result in the same action. Therefore, the script you use comes down to a mix of personal preference and OS support. This article will focus on the first method, systemd, and the third method, for cPanel servers.

The typical formulation for using systemd with the service binary looks like this:

Service Command

The command followed by the name of the service you want to affect, and then an option called the ‘usage flag’, which tells service what to do.

Usage Flags

Every service typically at the least can stop, start, restart, as well as status usage flags. However, some services have other usage flags that can be used to give more information about how the service performs.

The configtest usage flag for Apache is an excellent example of this. You can test the written configuration for errors without having to load it into Apache first. You can then verify whether the changes made to the Apache configuration might cause Apache not to start, and correct them, without affecting your sites.

Note
cPanel has a background daemon called chkservd, which a daemon that checks to see if specific services are running. If you stop a service that is monitored, like Apache, and chkservd finds out, it will assume the service died and will attempt to restart it. Keep this in mind if you stop any services from the command line, and if needed, turn off chkservd from inside of WHM before you start. Make sure to turn it back on when done.

Below are some examples of typical services that you might need to restart on your server, as well as all of their usage flags for systemd.

Typical Services

Apache

If you are making configuration changes to Apache from the command line, such as creating new include files or optimizations, restarting the service is necessary for the new configuration to be operational. Additionally, if Apache (or httpd) is causing undue load on the server, restarting it usually kills its child processes and starts over with new ones, alleviating memory usage.

# service httpd
usage: httpd {start|stop|restart|fullstatus|status|graceful|configtest|help}

The usage flags may not always make sense to you when printed on the command line. Here is a basic outline of what they mean for Apache. These same general descriptions can apply to other services that use the same usage flags (though some may be Apache-specific).

start start httpd
startssl start httpd with SSL enabled
stop

stop httpd

restart

restart httpd if running by sending a SIGHUP or start if not running

fullstatus dump a full status screen; requires lynx and mod_status enabled
status dump a short status screen; requires lynx and mod_status enabled
graceful do a graceful restart by sending a SIGUSR1 or start if not running
configtest do a configuration syntax test
help  list the commands available

You would use any of the optional usage flags as an extra argument after the service name as described above, like so:

# service httpd status
httpd (pid  74669) is running…

The cPanel restart script is essentially the same as service httpd restart:
# /scripts/restartsrv_httpd
Waiting for “httpd” to restart gracefully …

Mail

On cPanel servers the default mail service is exim.  Configuration or include changes to exim will require a restart to take effect.

# service exim
Usage: exim {start|stop|restart|status}

On Plesk servers, the default mail service is qmail instead.

# service qmail
Usage: qmail {start|stop|status|reload|condrestart|restart}

SSH

Any change to the SSH configuration file (such as changing the SSH port) requires a restart to the server daemon to take effect. Restarting sshd does not normally break SSH connections, but if you do change the SSH port, make sure you have opened the right ports in your firewall and are ready to make a new connection if needed!

# service sshd
Usage: sshd {start|stop|restart|reload|condrestart|status}

FTP

On cPanel servers the default FTP program is pure-ftp.  Any configuration changes to pure-ftp will require a restart to take effect. The cPanel restart script is convenient in this instance, as we covered earlier, even if the FTP program changes through cPanel, the restartsrv_ftpd script will restart the right service.

# service pure-ftpd
Usage: pure-config.pl {start|stop|restart|condrestart|status}

# /scripts/restartsrv_ftpd

Mysql

Mysql is mostly often restarted for configuration changes. Some newer machines will use mariadb instead of mysql or mysqld, though MariaDB normally responds to service requests made through systemd for MySQL as well. Again, the cPanel restart script is handy for cPanel servers, because usage either mariadb or mysql, /scripts/restartsrv_mysql will restart the database engine.

# service mysql
Usage: mysql (start|stop|restart|reload)

# /scripts/restartsrv_mysql

Firewall

On managed Liquidweb servers, the firewall of choice is CSF. The CSF firewall interacts with the iptables service.  Any configuration changes made to CSF the service must need a restart for the changes to take effect. However, you should not use the systemd or init scripts for this. CSF has a special restart procedure:

# csf -ra

If you try to use the service command, CSF will warn you not to:

# service csf restart
This script should ONLY be used by the init process. To restart csf use the CLI command 'csf -r'

Cron

Within Linux, the Cron service controls the scheduled tasks that run on the server. Stopping the Cron service will result in schedule task to be skipped. Unlike other services, the cron service does not need a restart if there is an additional cron added; it is generally only restarted when there is a change to the system clock or timezone, or when crons are not running due to various reasons.

# service crond
Usage: crond {start|stop|status|reload|restart|condrestart}

DNS

The named service is the default nameserver daemon for cPanel servers. If you run a different daemon, like MyDNS or NSD, use these names instead of ‘bind’ below.

# service bind
Usage: named {start|stop|status|restart|try-restart|reload|force-reload}

# /scripts/restartsrv_bind

Avatar for Andrej Walilko

About the Author: Andrej Walilko

Andrej Walilko (RHCE6) is a seasoned Linux Administrator, and he is a Migration Project Manager at Liquid Web, developing specialized processes for complex migration types. He enjoys doing woodworking, home improvement, and playing piano in his free time.

Latest Articles

How to use kill commands in Linux

Read Article

Change cPanel password from WebHost Manager (WHM)

Read Article

Change cPanel password from WebHost Manager (WHM)

Read Article

Change cPanel password from WebHost Manager (WHM)

Read Article

Change the root password in WebHost Manager (WHM)

Read Article