Linux / Unix: SysVinit Services Restart vs Reload vs Condrestart

by on January 5, 2012 · 0 comments· LAST UPDATED January 5, 2012

in ,

Can you tell me differences between the following sysvinit /sbin/service command:

service httpd restart

service httpd condrestart

service httpd reload

init is a program for Linux an Unix-based systems to spawns all other processes. It runs as a daemon and has PID 1. The boot loader starts the kernel and the kernel starts init. A Linux or Unix based system can be started up into various runlevels. The /etc/init.d/ directory stores various shell scripts which can be used for:

  1. Load kernel drivers ( modules ).
  2. Check and mount file systems.
  3. Setup network.
  4. Mount remote file systems (such as nfs).
  5. Start web server and other services.

You can use the following syntax to control various services:
# /etc/init.d/NAME start|stop|restart|status|reload|condrestart
OR
# /sbin/services NAME start|stop|restart|status|reload|condrestart
Where,

  1. start : Start a service (such as Apache by typing 'service httpd start')
  2. stop : Stop a service (such as Apache by typing 'service httpd stop').
  3. restart : Restart (stop and then start) a service (such as Apache by typing 'service httpd restart') .
  4. status : Find out whether a service is currently running or not (such as Apache by typing 'service httpd status') .
  5. reload : Reloads the config file without interrupting pending operations(reload Apache web server after the config file changes using 'service httpd reload') .
  6. condrestart : Restarts if the service is already running (such as Apache by typing 'service httpd condrestart).

Service command is used to run a System V init script. Usually all system V init scripts are stored in /etc/init.d directory and service command can be used to start, stop, and restart the daemons and other services under Linux.

Sample Session With service Command

Open a terminal and type the following commands:

sysvinit Command Examples

sysvinit Command Examples (click to enlarge)

Where,

  1. Start a httpd server.
  2. Restart a httpd server.
  3. Stop a httpd server.
  4. Find a status of httpd server.
  5. Start a httpd server.
  6. Find a status of httpd server.
  7. Edit /etc/httpd/conf/httpd.conf file and reload the server.
  8. Restarts if the httpd service is already running.
  9. See Apache error log file.

Sample SysVInit Script

The service script can define arbitrary commands. In this example 'service httpd graceful' provides additional options to httpd server:

 
#!/bin/bash
#
# httpd        Startup script for the Apache HTTP Server
#
# chkconfig: - 85 15
# description: The Apache HTTP Server is an efficient and extensible  \
#	       server implementing the current HTTP standards.
# processname: httpd
# config: /etc/httpd/conf/httpd.conf
# config: /etc/sysconfig/httpd
# pidfile: /var/run/httpd/httpd.pid
#
### BEGIN INIT INFO
# Provides: httpd
# Required-Start: $local_fs $remote_fs $network $named
# Required-Stop: $local_fs $remote_fs $network
# Should-Start: distcache
# Short-Description: start and stop Apache HTTP Server
# Description: The Apache HTTP Server is an extensible server 
#  implementing the current HTTP standards.
### END INIT INFO
 
# Source function library.
. /etc/rc.d/init.d/functions
 
if [ -f /etc/sysconfig/httpd ]; then
        . /etc/sysconfig/httpd
fi
 
# Start httpd in the C locale by default.
HTTPD_LANG=${HTTPD_LANG-"C"}
 
# This will prevent initlog from swallowing up a pass-phrase prompt if
# mod_ssl needs a pass-phrase from the user.
INITLOG_ARGS=""
 
# Set HTTPD=/usr/sbin/httpd.worker in /etc/sysconfig/httpd to use a server
# with the thread-based "worker" MPM; BE WARNED that some modules may not
# work correctly with a thread-based MPM; notably PHP will refuse to start.
 
# Path to the apachectl script, server binary, and short-form for messages.
apachectl=/usr/sbin/apachectl
httpd=${HTTPD-/usr/sbin/httpd}
prog=httpd
pidfile=${PIDFILE-/var/run/httpd/httpd.pid}
lockfile=${LOCKFILE-/var/lock/subsys/httpd}
RETVAL=0
STOP_TIMEOUT=${STOP_TIMEOUT-10}
 
# The semantics of these two functions differ from the way apachectl does
# things -- attempting to start while running is a failure, and shutdown
# when not running is also a failure.  So we just do it the way init scripts
# are expected to behave here.
start() {
        echo -n $"Starting $prog: "
        LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS
        RETVAL=$?
        echo
        [ $RETVAL = 0 ] && touch ${lockfile}
        return $RETVAL
}
 
# When stopping httpd, a delay (of default 10 second) is required
# before SIGKILLing the httpd parent; this gives enough time for the
# httpd parent to SIGKILL any errant children.
stop() {
	echo -n $"Stopping $prog: "
	killproc -p ${pidfile} -d ${STOP_TIMEOUT} $httpd
	RETVAL=$?
	echo
	[ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
}
reload() {
    echo -n $"Reloading $prog: "
    if ! LANG=$HTTPD_LANG $httpd $OPTIONS -t >&/dev/null; then
        RETVAL=6
        echo $"not reloading due to configuration syntax error"
        failure $"not reloading $httpd due to configuration syntax error"
    else
        # Force LSB behaviour from killproc
        LSB=1 killproc -p ${pidfile} $httpd -HUP
        RETVAL=$?
        if [ $RETVAL -eq 7 ]; then
            failure $"httpd shutdown"
        fi
    fi
    echo
}
 
# See how we were called.
case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  status)
        status -p ${pidfile} $httpd
	RETVAL=$?
	;;
  restart)
	stop
	start
	;;
  condrestart|try-restart)
	if status -p ${pidfile} $httpd >&/dev/null; then
		stop
		start
	fi
	;;
  force-reload|reload)
        reload
	;;
  graceful|help|configtest|fullstatus)
	$apachectl $@
	RETVAL=$?
	;;
  *)
	echo $"Usage: $prog {start|stop|restart|condrestart|try-restart|force-reload|reload|status|fullstatus|graceful|help|configtest}"
	RETVAL=2
esac
 
exit $RETVAL
 
TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 0 comments… add one now }

Leave a Comment

Tagged as: , , , , , , , , , , , , , ,

Previous Faq:

Next Faq: