`

管理httpd进程的shell脚本

阅读更多

每次都不记得httpd执行文件的位置,很不方便,不妨做个shell脚本来管理

#!/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: /www/wdlinux/apache/conf/httpd.conf
# config: /etc/sysconfig/httpd
# pidfile: /www/wdlinux/apache/logs/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=/www/wdlinux/apache/bin/apachectl
httpd=${HTTPD-/www/wdlinux/apache/bin/httpd}
prog=httpd
pidfile=${PIDFILE-/www/wdlinux/apache/logs/httpd.pid}
lockfile=${LOCKFILE-/var/lock/subsys/httpd}
RETVAL=0

# 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 >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 3 $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



 注意自己的目录

写好后给此文件一个可执行权限chmod 755 /etc/rc.d/init.d/httpd

service httpd start stop restart..........

添加到开机启动

chkconfig --add httpd

chkconfig --level 3 httpd on

分享到:
评论

相关推荐

    shell脚本编程100例.doc

    在本文中,我们将深入探讨几个关键的Shell脚本知识点,包括基本语法、变量、条件判断、循环、进程控制以及与系统交互。 1. **Hello World脚本**: 这是最简单的Shell脚本示例,`#!/bin/bash`是shebang,指明解释器...

    用shell脚本监控进程是否存在 不存在则启动的实例

    ### 使用Shell脚本监控进程的存在性与启动实例详解 #### 概述 本文将详细介绍如何使用Shell脚本来监控特定进程的存在性,并在该进程不存在时自动启动它。此方法适用于服务器自动化运维场景,有助于提高系统的稳定性...

    linux进程监控shell脚本代码

    在Shell脚本中,我们可以使用`wait`命令来捕获子进程的退出状态。当子进程(在这里是PHP进程)结束时,`wait`会返回子进程的退出状态码,这个信息可用于判断进程是否异常退出。 3. **重启进程的命令**: 当检测到...

    Shell脚本判断Apache进程是否存在

    `wc -l` 是`word count`的缩写,用于统计行数,这里计算的是httpd进程的数量。 3. **条件判断**:`[[ $PIDNUM -eq 0 ]]` 和 `[[ $PIDNUM != 10 ]]` 是bash shell中的条件测试表达式,分别检查进程数是否等于0和不...

    shell学习.docx

    【Shell学习】 Shell脚本是一种在Unix/Linux操作系统中用于自动化任务的文本文件,它包含了多行Shell或Linux命令。简单来说,当你需要重复执行一...通过学习和掌握Shell脚本,你可以更有效地自动化日常的系统管理任务。

    检测脚本,管理系统

    综上所述,从给定的文件信息中,我们不仅学到了Bash脚本的基础语法和结构,还深入了解了进程监控与重启以及系统管理的最佳实践。这些知识对于任何希望提升系统自动化管理水平的IT专业人士来说都是宝贵的财富。

    Linux进程管理命令实用.pdf

    一个程序在运行过程中可能会产生多个进程,例如,HTTP服务器Apache在处理多个用户请求时会创建多个httpd进程。 【进程分类】通常分为三类:交互进程、批处理进程和守护进程。交互进程与用户直接交互,如终端Shell;...

    PHP脚本修改Linux或Unix系统口令方法.pdf

    首先,我们需要一个能够修改用户口令的Shell脚本。示例中的脚本使用`expect`程序来模拟用户交互,因为`passwd`命令通常需要用户输入旧密码和新密码。脚本接受两个参数,即用户名和新密码,然后通过`spawn passwd`...

    PHP+shell脚本操作Memcached和Apache Status的实例分享

    总结起来,本文分享了如何使用Shell脚本管理Memcached服务,包括启动、停止、重启和监控进程,以及如何利用Apache Status监控服务器状态。这种自动化和监控方法对于任何使用Memcached和Apache HTTP服务器的Web应用来...

    shell面试题大全

    shell 也是一个脚本语言,可以编写脚本来自动执行一系列命令。 以下是 shell 面试题大全,涵盖了 shell 编程、sed、awk 和其他 shell 相关的知识点: 1. 用 sed 修改 test.txt 的 23 行 test 为 tset。 这个问题...

    智能监测自动重启Apache服务器的Shell脚本

    Apache服务器的高可用性对于许多在线业务至关重要,而通过编写智能监测自动重启的Shell脚本,可以在Apache服务出现问题时及时采取措施,确保服务的稳定运行。以下是对该脚本的详细解析: 首先,脚本的主要目标是...

    shell100.docx

    【Shell脚本100例】是一份涵盖了多种...这些例子展示了Shell脚本在日常运维、自动化任务和系统管理中的应用,涵盖文件操作、进程控制、条件判断、用户交互等多个方面。通过实践这些脚本,可以提升Shell脚本编程技能。

    Linux系统中守护进程的创建方法.pdf

    守护进程的启动方式有三种:在系统启动时由系统初始化脚本启动,由作业规划进程 crond 定时或定期启动,管理员或有执行权限的用户直接在 shell 上启动。守护进程的创建步骤包括脱离终端和会话期、更改当前目录、关闭...

    linux各自启动配置脚本简介

    ### Linux 启动配置脚本详解 #### 一、Linux 的启动流程 Linux 系统启动过程是一个复杂而有序的过程,涉及到多个...了解这些启动脚本的工作原理和配置方式,可以帮助我们更有效地管理 Linux 系统,确保其稳定运行。

    Linux学习笔记完整版

    25. SHELL脚本创建执行:SHELL脚本通过shebang行指定解释器,然后通过chmod命令设置执行权限,最后通过./或直接调用解释器执行。 26. SHELL变量定义:SHELL脚本中可以定义用户变量和环境变量,通过export命令将变量...

    25道shell面试题借鉴.pdf

    Shell脚本是Linux系统中非常重要的工具,它用于...这些面试题覆盖了Shell脚本的基础和高级操作,包括文件操作、流处理、网络配置、进程管理等核心知识点。熟悉和掌握这些技能对于Linux系统管理和自动化运维至关重要。

Global site tag (gtag.js) - Google Analytics