Restarting common services (Linux)


To totally unlock this section you need to Log-in


Login

One of the more common tasks that needs to be done on live servers is restarting services (such as Apache, mail, ftp, and DNS). A few of the reasons for services needing to be restarted can vary from the service crashing, the load on the server being too high, or a restart to make a configuration change take affect.

There are two different ways that can be used to interact with services in a Linux environment:

  • The first is to use the service command to issue commands to the service you wish to work with. For example: service httpd restart This will issue a restart command to Apache.
  • The second would be to directly interact with the init script on the server. An example of this would be, /etc/init.d/httpd restart , this will issue a restart command to Apache.

As demonstrated above, both commands will result in the same action and since both commands will yield the same result, usage comes down to personal preference.

Each service typically at the least has stop, start, restart, and status usage flags. However, some services have other usage flags that can be used to determine more information about how the service is currently performing. One example of this would be the configtest usage flag for Apache. Using this flag it is possible to test the current configuration for any errors.

Using this flag it is possible to verify any changes made to the Apache configuration will cause Apache not to start without having to restart Apache.

Below are some examples of various common services and their usage flags:

Apache

One of the primary reasons Apache may need to be restarted would be after changes have been made to the configuration file in order to optimize performance.

bash-2.05b# /etc/init.d/httpd
usage: /etc/init.d/httpd (start|stop|restart|fullstatus|status|graceful|configtest|help)

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       - this screen

To restart, start or stop Apache web server from the command line interface using either Linux or MacOS X, use the commands below, these commands should be executed as the root user otherwise prefix them with sudo. In the following examples we are using the apachectl command to send specific signal to Apache web server:

#### Start Apache

apachectl start

#### Stop Apache

apachectl stop

Alternatively, to send a signal to the parent you should issue a command, to stop Apache, such as:

kill -TERM `cat /usr/local/apache2/logs/httpd.pid`

#### Graceful Stop

apachectl graceful-stop

#### Restart Apache

apachectl restart

#### Graceful restart

apachectl graceful

The use of graceful in stopping and restarting allows a less abrupt halt to proceedings allowing any existing webserving to complete before the process is terminated. The USR1 or graceful signal causes the parent process to advise the children to exit after their current request (or to exit immediately if they're not serving anything).

The parent re-reads its configuration files and re-opens its log files. As each child dies off the parent replaces it with a child from the new generation of the configuration, which begins serving new requests immediately.

Mail

On cPanel servers the default mail service is exim. Any configuration changes to exim will require a restart to take effect.

bash-2.05b# /etc/init.d/exim
Usage: exim {start|stop|restart|status}

On Plesk servers the default mail service is qmail. Any configuration changes to qmail will require a restart to take effect.

bash-2.05b# /etc/init.d/qmail
Usage: /etc/init.d/qmail {start|stop|status|reload|condrestart|restart}

SSH

Any change to the ssh configuration file(such as changing the SSH port) require a restart to take effect.

bash-2.05b# /etc/init.d/sshd
Usage: /etc/init.d/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.

bash-2.05b# /etc/init.d/pure-ftpd
Usage: pure-config.pl {start|stop|restart|condrestart|status}

MySql

bash-2.05b# /etc/init.d/mysql
Usage: /etc/init.d/mysql start|stop|restart|reload

Firewall

On managed servers the firewall of choice could be APF. The APF firewall interacts with the iptables service. If any configuration changes are made to APF the service must be restarted in order for the changes to take effect.

bash-2.05b# /etc/init.d/iptables
Usage: /etc/init.d/iptables {start|stop|restart|condrestart|status|panic|save}

bash-2.05b# /etc/init.d/apf
usage: /etc/init.d/apf [start|stop|restart]

Cron

On Linux the cron service controls the scheduled tasks that run on the server.

bash-2.05b# /etc/init.d/crond
Usage: /etc/init.d/crond {start|stop|status|reload|restart|condrestart}

System Logging

The syslog service controls the system and kernel logging for the server.

bash-2.05b# /etc/init.d/syslog
Usage: /etc/init.d/syslog {start|stop|status|restart|condrestart}