shell脚本启动和停止apache 检查apache的运行状态 如果apache启动了就让它停止 ,如果停止了就启动apach

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了shell脚本启动和停止apache 检查apache的运行状态 如果apache启动了就让它停止 ,如果停止了就启动apach相关的知识,希望对你有一定的参考价值。

ps -fe |grep "/usr/sbin/httpd"
if [ $? -eq 1 ]
then
service httpd restart
else
service httpd stop
fi
前面的grep里面的内容根据本机的情况进行修改
参考技术A 我理解的思路。。。。获取 apache运行状态 给变量赋值 用case 语句 参考技术B #!/bin/bash
#
# httpd Startup script for the Apache HTTP Server
#
# chkconfig: - 85 15
# description: Apache is a World Wide Web server. It is used to serve \
# html files and CGI.
# processname: httpd
# config: /etc/httpd/conf/httpd.conf
# config: /etc/sysconfig/httpd
# pidfile: /var/run/httpd.pid

# 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.pid
lockfile=$LOCKFILE-/var/lock/subsys/httpd
RETVAL=0

# check for 1.3 configuration
check13 ()
CONFFILE=/etc/httpd/conf/httpd.conf
GONE="(ServerType|BindAddress|Port|AddModule|ClearModuleList|"
GONE="$GONEAgentLog|RefererLog|RefererIgnore|FancyIndexing|"
GONE="$GONEAccessConfig|ResourceConfig)"
if LANG=C grep -Eiq "^[[:space:]]*($GONE)" $CONFFILE; then
echo
echo 1>&2 " Apache 1.3 configuration directives found"
echo 1>&2 " please read /usr/share/doc/httpd-2.2.3/migration.html"
failure "Apache 1.3 config directives test"
echo
exit 1
fi


# 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: "
check13 || exit 1
LANG=$HTTPD_LANG daemon --pidfile=$pidfile $httpd $OPTIONS
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch $lockfile
return $RETVAL


# When stopping httpd a delay of >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 10 $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=$?
echo $"not reloading due to configuration syntax error"
failure $"not reloading $httpd due to configuration syntax error"
else
killproc -p $pidfile $httpd -HUP
RETVAL=$?
fi
echo


# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status -p $pidfile $httpd
RETVAL=$?
;;
restart)
stop
start
;;
condrestart)
if [ -f $pidfile ] ; then
stop
start
fi
;;
reload)
reload
;;
graceful|help|configtest|fullstatus)
$apachectl $@
RETVAL=$?
;;
*)
echo $"Usage: $prog start|stop|restart|condrestart|reload|status|fullstatus|graceful|help|configtest"
exit 1
esac

exit $RETVAL

以上是关于shell脚本启动和停止apache 检查apache的运行状态 如果apache启动了就让它停止 ,如果停止了就启动apach的主要内容,如果未能解决你的问题,请参考以下文章

shell脚本调度 Tomcat 启动停止

linux 下 apache启动停止重启命令

shell 启动和停止脚本

linux下重启apache

分享Memcached shell启动停止脚本

安装apached源码包以及编写shell脚本使其启动更加方便,并且和启动系统服务相同