Linux/Unix:SysVinit Services Restart vs Reload vs Condrestart

时间:2020-01-09 10:42:25  来源:igfitidea点击:

下面的sysvinit/sbin/service命令之间的区别是什么:

service httpd restart

service httpd condrestart

service httpd reload

init是用于Linux和基于Unix的系统生成所有其他进程的程序。
它作为守护程序运行,并具有PID1。
引导加载程序启动内核,内核启动init。
可以将基于Linux或者Unix的系统启动到各种运行级别。

/etc/init.d/目录存储各种shell脚本,这些脚本可用于:

  • 加载内核驱动程序(模块)。
  • 检查并挂载文件系统。
  • 设置网络。
  • 挂载远程文件系统(例如nfs)。
  • 启动Web服务器和其他服务。

您可以使用以下语法来控制各种服务:

# /etc/init.d/NAME start|stop|restart|status|reload|condrestart

或者

# /sbin/services NAME start|stop|restart|status|reload|condrestart

其中:

  • start:启动服务(例如通过输入service httpd start来启动Apache)
  • stop:停止服务(例如通过输入service httpd stop来终止Apache)。
  • restart:重新启动(停止然后启动)服务(例如,通过执行service httpd restart来启动Apache)。
  • status:找出服务当前是否正在运行(例如,通过输入service httpd status来查找Apache)。
  • reload:在不中断挂起操作的情况下重新加载配置文件(使用服务httpd reload更改配置文件后重新加载Apache Web服务器)。
  • condrestart:如果服务已经在运行(例如,通过执行service httpd condrestart的Apache),则重新启动。

Service命令用于运行System V初始化脚本。
通常,所有系统V初始化脚本都存储在/etc/init.d目录中,并且在Linux下,可以使用service命令启动,停止和重新启动守护程序和其他服务。

SysVInit示例脚本

服务脚本可以定义任意命令。
在此示例服务中,httpd graceful为httpd服务器提供了其他选项:

#!/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 "" 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