Skip to content

Instantly share code, notes, and snippets.

@huwan
Last active August 29, 2015 14:02
Show Gist options
  • Save huwan/a38b3186d3c052240e94 to your computer and use it in GitHub Desktop.
Save huwan/a38b3186d3c052240e94 to your computer and use it in GitHub Desktop.
Linux 定时使用脚本检测程序运行状态并通过飞信进行通知

Linux 定时使用脚本检测程序运行状态并通过飞信进行通知

  • 可以通过输入进程号(精确匹配)(注)或者特定的进程名称(模糊匹配)
  • 当程序完成退出后,通过中国移动[命令行飞信] 1 进行提醒
  • 需结合crontab 计划任务服务,每隔10分钟执行一次检测任务

注:因为是使用ps -ef + grep 所以,有可能一个进程号在多列出现,并不是完全意义上的精确匹配,但实际使用中一般不会出问题,可以先人工确认一下是否唯一

#!/bin/bash
##source .bash_profile
usage="Usage: $0 pid
or: $0 processname"
if [ $# -eq 0 ]; then
echo "$usage" 1>&2
exit 1
else
i=`ps -ef | grep $1 | grep -v grep |grep -v $0 | wc -l`
if [ $i -lt 1 ]; then
text="Notification: pid $1 completed successfully!"
if [ ! -f /home/ams/.$1 ]; then
echo "$text @@ `date`" > /home/ams/.$1
/usr/local/bin/fetion -d "$text"
#/usr/local/bin/fetion -t 150****3136 -d "$text"
#/usr/local/bin/fetion -f 152****4789 -p password -t 150****3136 -d "$text"
fi
else
echo "$1 running @@ `date`" >> /home/ams/.run
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment