Jquery中文网 www.jquerycn.cn
Jquery中文网 >  脚本编程  >  shell  >  正文 用于进程检查的shell脚本 判断是否运行某服务(图文)

用于进程检查的shell脚本 判断是否运行某服务(图文)

发布时间:2014-11-23   编辑:www.jquerycn.cn
本文介绍一段shell脚本,它可以检测某进程或某服务是否正在运行,然后以邮件通知。有需要的朋友参考下吧。

一个简单的shell脚本,用来找出关键的服务是否正在运行,适用于Linux或Unix操作系统。
该脚本还可以使用电子邮件发送通知。

代码:

复制代码 代码示例:

#!/bin/bash
# Name : service.chk 服务检测脚本

## 根据自己的环境修改
_pgrep="/usr/bin/pgrep"
_mail="/usr/bin/mail"
 
## 环境变量
_chklist="/usr/bin/php-cgi /usr/sbin/nginx /usr/sbin/lighttpd /usr/sbin/mysqld /usr/sbin/apache2 /usr/sbin/named /usr/sbin/pgsqld"
 
## yes | no
_sendemail="no"
 
## email
_email="test@jquerycn.cn"
 
## 不要修改如下配置
_failed="false"
_service="Service:"
 
_running() {
 local p="${1##*/}"
 local s="true"
 $_pgrep "${p}" >/dev/null || { s="false"; _failed="true"; _service="${_service} $1,"; }
 [[ "$s" == "true" ]] && echo "$1 running" || { echo -n "$1 not running"; [[ ! -f "$1" ]] && echo " [ $1 not found ]" || echo ; }
}
 
## header
echo "Service status on ${HOSTNAME} @ $(date)"
echo "------------------------------------------------------"
 
## Check if your service is running or not
for s in $_chklist
do
 _running "$s"
done
 
## Send a quick email update (good for cron jobs) ##
[[ "$_failed" == "true" && "$_sendemail" == "yes" ]] && { _mess="$_service failed on $HOSTNAME @ $(date)"; 
$_mail -s 'Service not found' "$_email" < "${_mess}";
} /p>

结果:
16.png

您可能感兴趣的文章:
用于进程检查的shell脚本 判断是否运行某服务(图文)
检测linux网络服务是否开启的shell脚本(图文)
学习linux shell中 if else以及大于、小于、等于逻辑表达式
shell脚本监控php-fpm并自动重启服务
web安全之文件上传漏洞攻击与防范方法
linux下监视进程挂掉后自动重启的shell脚本
python shell是什么
Linux Shell判断程序是否运行的代码分享
ubuntu下使用chkconfig命令
expect脚本远程批量管理服务器的思路解析

[关闭]