原文:http://bash.cyberciti.biz/web-server/restart-apache2-httpd-shell-script/
适用操作系统版本:CentOS / RHEL / Fedora / Debian / Ubuntu Linux
我们使用pgrep命令查找系统进程中是否有httpd,确定apache有没有开启。
可以把脚本加入crontab文件中,参考下面设置:
*/5 * * * * /path/to/script.sh >/dev/null 2>&1
以下是该shell脚本的源代码:
#!/bin/bash
# Apache Process Monitor
# Restart Apache Web Server When It Goes Down
# -------------------------------------------------------------------------
# Copyright (c) 2003 nixCraft project <http://cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# RHEL / CentOS / Fedora Linux restart command
RESTART="service httpd restart"
# uncomment if you are using Debian / Ubuntu Linux
#RESTART="/etc/init.d/apache2 restart"
#path to pgrep command
PGREP="/usr/bin/pgrep"
# find httpd pid
$PGREP httpd
if [ $? -eq 0 ]
then
# restart apache
$RESTART
fi |