#!/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
一个monitor脚本
最新推荐文章于 2021-05-06 15:07:46 发布