一个monitor脚本

#!/bin/bash
Cmd=$0;
usage()
{
    echo "usage : $Cmd crond [start|stop] | start | stop | status";
}

if [ $# -ne 1 ] && [ $# -ne 2 ];then
    usage;
    exit;
fi

INIT_FILE="/etc/rc.local";
script_name="NelmonLogMonitor.py";
script_pwd=${Cmd%%'logmonitor.sh'};
reg_path=$(echo $PWD | sed 's#/#\\/#g')


#chmod -R 555 $script_name;
sed  -i 's/^exit .*/""/g' $INIT_FILE;
if [ ! -f "$PWD/$script_name" ]; then
	cp $script_pwd/$script_name $PWD;
fi

case $1 in
	status)
		flag=`ps aux|grep 'NelmonLogMonitor.py'|grep -v 'grep'|wc -l`;
		crond_flag=`crontab -l|grep "$script_name"|wc -l`;
		if [ $flag -eq 0 ];then
			echo "$script_name			[stoped]";
		elif [ $flag -eq 1 ];then
			echo "$script_name			[running]";
		else
			echo "$script_name			[duplicate running]";
		fi
		
		if [ $crond_flag -eq 0 ];then
			echo "$script_name			[not cronded]";
		elif [ $crond_flag -gt 1 ];then
			echo "$script_name			[duplicate cronded]";
		else
			echo "$script_name			[cronded]";
		fi
		
		exit;
		;;
    start)
		if [ $# -ne 1 ];then
			usage;
			exit;
		fi
        echo "$script_name starting...";
        count=`grep "NelmonLogMonitor.py" $INIT_FILE | wc -l`;
        if [ $count -eq 0 ];then
			echo "need configure.";
            echo "python $PWD/$script_name & 1>/dev/null 2>&1" >> $INIT_FILE;
        else
            sed  -i "s/.*NelmonLogMonitor.*/python $reg_path\\/$script_name \& /g" $INIT_FILE;
        fi
        flag=`ps aux|grep 'NelmonLogMonitor.py'|grep -v 'grep'|wc -l`;
        if [ $flag -eq 0 ];then
            python $PWD/$script_name &
			
            flag=`ps aux|grep 'NelmonLogMonitor.py'|grep -v 'grep'|wc -l`;
            if [ $flag -eq 0 ];then
                echo "$script_name start fail!";
            else
                echo "$script_name start success.";
            fi
		else
			echo "$script_name is running..."
        fi
    ;;
    stop)
		if [ $# -ne 1 ];then
			usage;
			exit;
		fi
        echo "$script_name stoping ...";
		count=`grep "NelmonLogMonitor.py" $INIT_FILE | wc -l`;
		if [ $count -eq 0 ];then
			echo "the script is not configure ok.Plz start it and then stop.";
		else
			sed -i "s/.*NelmonLogMonitor.py.*/\#$reg_path\\/$script_name \&/g"  $INIT_FILE;
		fi
		
        flag=`ps aux|grep 'NelmonLogMonitor.py'|grep -v 'grep'|wc -l`;
        if [ $flag -gt 0 ]; then
            for pid in `ps aux|grep 'NelmonLogMonitor.py'|grep -v 'grep'|awk '{print $2}'`
            do
                kill -9 $pid;
				echo "$pid stoped."
            done
		else
			echo "$script_name is not running!!";
        fi
    ;;
	crond)
		if [ $# -ne 2 ];then
			echo "crond param error."
			usage;
			exit;
		fi
		
		crontab  -l > $PWD/nelmon_log_monitor;
		flag=`cat $PWD/nelmon_log_monitor|grep "$script_name"|wc -l`;
		
		case $2 in
			start)
				echo "starting crond ..."
				if [ $flag -eq 0 ];then
					echo "00 01 * * * python $PWD/$script_name  crond start" >> $PWD/nelmon_log_monitor
					crontab ./nelmon_log_monitor;
				else
					echo "$script_name has been cronded.";
				fi
				python $PWD/$script_name  crond start;
				
			;;
			stop)
				echo "stopping crond ..."
				if [ $flag -eq 0 ];then
					echo "no $script_name be cronded.";
				else
					sed -i "/.*$script_name.*/d"  $PWD/nelmon_log_monitor;
				fi
				
				crontab $PWD/nelmon_log_monitor;
			;;
			*)
				echo "crond following param error.";
			;;
			esac
		
		rm -f ${PWD}/nelmon_log_monitor;
		echo "crond finished.";
	;;
	
    *)
        echo "param error.";
        usage;
    ;;
esac


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

linuf

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值