bug ❌
脚本名称不要包含 grep 抓取的关键字 ✅
端口 80 检测
service httpd start ❓RedHat 专用命令
/etc/rc.d/init.d/httpd start ✅ 通用方式,访问文件的绝对路径
&>/dev/null
输出所有 log 信息到`空设备`文件,即丢弃所有的 log 信息
-n
没有装apache服务,有tomcat服务,改用tomcat服务测试
#!/bin/bash
#截取tomcat进程,并把结果赋予变量test
test=$( ps aux | grep tomcat | grep -v grep | grep "/usr/share/tomcat" | cut -d "=" -f 2 )
#判断test是否为空,为空则执行then中的命令
if [ -n "$test" ];then
#把tomcat服务运行状态重定向指定的log日志
echo "$(date) this tomcat is online." >> /tmp/tomcat_is_ok.log
else
#tomcat服务没起来,所以要启动tomcate服务,重启后的命令传到黑洞
/usr/share/tomcat/bin/startup.sh &> /dev/null
#把tomcat重启后的状态重定向指定的log日志
echo "$(date)this tomcat is restart!" >> /tmp/tomcat_is_restart.log
fi
判断apache是否启动
#! /bin/bash
test=$(ps aux | grep httpd | grep -v grep)
#截取httpd进程,并把结果赋予变量test
if [ -n "$test" ]
#如果test的值不为空,则执行then中命令
then
echo " $(date) httpd is ok!" >> /tmp/autostart-acc.log
else
/etc/rc.d/init.d/httpd start &> /dev/null
echo "$(date) restart httpd !!" >> /tmp/autostart-err.log
fi
看看linux 的crontab,例如
* */1 * * * /usr/local/apache/bin/apachectl restart
vi apache.shujuku #注意文件名不要取httpd
#!/bin/bash
test=$(ps aux | grep httpd | grep -v grep)
#截取httpd进程,把结果赋给变量test
if [ -n "$test" ] #-n非空,执行then中命令
then
echo "httpd is open!"
else
echo "httpd is closed!!"
fi
#!/bin/bash
test=$(ps aux | grep "httpd" | grep -v "grep")
if [ -n "$test" ]
then
echo "httpd is on"
else
echo "httpd is off"
/etc/rc.d/init.d/httpd start
fi
例子:判断apache是否启动
ps aux 累死任务管理器,查看当前正在运行的程序
注意事项: 通过“httpd”进行判断apache是否启动,需要注意,相关名字的文件是否也在进程中。
#!/bin/bash
test=$(ps aux | grep "httpd" | grep -v "grep")
if [ -n "$test" ]
then
echo "httpd is on"
else
echo "httpd is off"
/etc/rc.d/init.d/httpd start
fi
判断apache是否启动
判断apache是否启动脚本,注意脚本名字不能包含httpd
判断apache是否启动
例子2.判断apache是否启动
判断apache是否启动