发布时间:2014-09-05 17:40:52作者:知识屋
#!/bin/bash
#author:haiy
#email:haiyangfu512@gmail.com
# Fri Apr 25 13:04:48 CST 2014
#GNU licence.#monitor and keep the program runningDEBUG=falsefunction restart_program(){#向screen发送要执行的命令 if [ $# -lt 3 ];then echo $# parameters get! echo usage: bash restart_program session_name window_index_number command exit fi session=$1 window=$2 command=$3 #echo "current session is $session.$window_num" screen -x -S $session -p $window -X stuff "$command"}function single_watch(){#检测某个程序是否在运行 if [ $# -lt 2 ];then echo $# parameters get! echo usage: bash single_watch program_name running_instance_number exit fi program_name=$1 expected_number=$2 real_number=$( ps aux | grep "$program_name"$ | grep -v grep | grep -v $0 | grep -v SCREEN | grep -v vim | wc -l ) if [ ! $real_number = $expected_number ];then echo false else echo true fi}function keep_running(){#从文件读入要检测运行的程序的列表以及其所在的screen 的session name和window number。 #$1 is the service_list file with service name , #screen session name and window index #$2 is the sleep duration service_list=$1 sleep_time=$2 while true; do if [ $# -lt 2 ];then echo $# parameters get! echo usage: bash check_running_service list_file sleep_time exit fi #read the checked service list from the file while read line; do IFS=';' read -a content <<< "$line" session=$(echo "${content[0]}") window=$(echo "${content[1]}") program=$(echo "${content[2]}") if $DEBUG; then echo session,$session, echo window,$window, echo program,$program fi stat=$(single_watch "$program" 1) if [ $stat == "false" ];then echo "`date` Gonna restart $program..." restart_program $session $window "$program" #echo "$program ." > mail_content #python gmail_sender.py
#发送邮件。。。。 else echo "`date` ==> $line ==>Running normally." fi done < $service_list if $DEBUG;then exit fi echo "I'm gonna sleep a while,$2.." sleep $sleep_time done}if $DEBUG;then #Here the command should be quoted with double quotes restart_program 16902.watch_dog 3 "ls" #single_watch keep_running $1 $2 #session_name=16902.watch_dog #window_num=3 #screen -x -S $session -p $window_num -X stuff $commandfiif [ $# -lt 2 ];then echo usage: bash keep_running list_file sleep_time exitfikeep_running $1 $2=======分隔=====================list_file example: 16902.watch_dog;2;bash test_loop.sh
linux一键安装web环境全攻略 在linux系统中怎么一键安装web环境方法
Linux网络基本网络配置方法介绍 如何配置Linux系统的网络方法
Linux下DNS服务器搭建详解 Linux下搭建DNS服务器和配置文件
对Linux进行详细的性能监控的方法 Linux 系统性能监控命令详解
linux系统root密码忘了怎么办 linux忘记root密码后找回密码的方法
Linux基本命令有哪些 Linux系统常用操作命令有哪些
Linux必学的网络操作命令 linux网络操作相关命令汇总
linux系统从入侵到提权的详细过程 linux入侵提权服务器方法技巧
linux系统怎么用命令切换用户登录 Linux切换用户的命令是什么
在linux中添加普通新用户登录 如何在Linux中添加一个新的用户
2012-07-10
CentOS 6.3安装(详细图解教程)
Linux怎么查看网卡驱动?Linux下查看网卡的驱动程序
centos修改主机名命令
Ubuntu或UbuntuKyKin14.04Unity桌面风格与Gnome桌面风格的切换
FEDORA 17中设置TIGERVNC远程访问
StartOS 5.0相关介绍,新型的Linux系统!
解决vSphere Client登录linux版vCenter失败
LINUX最新提权 Exploits Linux Kernel <= 2.6.37
nginx在网站中的7层转发功能