redis部署,linux下秒级定时监控服务脚本

redis.sh

#! /bin/bash
tcl_root="/usr/local"
tcl_home="/usr/local/tcl8.6.9"
redis_home="/usr/local/redis-5.0.5"
ipaddr=`ip a | grep ens | grep inet | grep -v 127.0.0.1 | grep -v inet6 | awk '{print $2}' | cut -d "/" -f1`
state=$1
echo $ipaddr

function Choose(){
if [ $state = "install" ];then
Install $1 $2
fi
if [ $state = "start" ];then
Start
fi
if [ $state = "stop" ];then
Stop
fi
if [ $state = "log" ];then
Log $1
fi
if [ $state = "restart" ];then
Restart
fi
}
function Install(){
if [ ! -n "$1" ];
then
echo "please input tcl package!"
return
fi
if [ ! -n "$2" ];
then
echo "please input redis package"
return
fi
tar -zxvf $1 -C $tcl_root
if [ $? -eq 0 ];
then
echo "tar $1 sucess!"
else
echo "tar $1 error!"
fi
cd $tcl_home/unix
if [ $? -eq 0 ];
then
echo $PWD
sudo ./configure
if [ $? -eq 0 ];
then
echo "configure runtime sucess!"
else
echo "configure runtime error,please find error cause?"
return
fi
sudo make
if [ $? -eq 0 ];
then
echo "make tcl success!"
else
echo "make tcl error! please find compile tcl failed cause?"
return
fi
sudo make install
if [ $? -eq 0 ];
then
echo "make install tcl success!"
else
echo "make install tcl error! please find compile tcl failed cause?"
eturn
fi
else
echo "cd $tcl_home/unix error!"
return
fi
tar -zxvf $2 -C $tcl_root
echo $2
if [ $? -eq 0 ];
then
echo "tar $2 success!"
else
echo "tar $2 error!"
return
fi
cd $redis_home
if [ $? -eq 0 ];
then
echo $PWD
make MALLOC=libc
if [ $? -eq 0 ];
then
echo "make redis success!"
else
echo "make redis error,please find compile redis failed cause?"
fi
fi
cd $PWD/src && make install
if [ $? -eq 0 ];
then
echo "make install redis success!"
else
echo "make install redis error! please find compile redis failed cause?"
return
fi
sed -i 's/bind 127.0.0.1/bind '$ipaddr'/g' $redis_home/redis.conf
if [ $? -eq 0 ];
then
echo "replace redis.conf bind 127.0.0.1 to bind $ipaddr success!"
else
echo "replace redis.conf bind 127.0.0.1 to bind $ipaddr error!"
return
fi
sed -i 's/port 6379/port 6060/g' $redis_home/redis.conf
if [ $? -eq 0 ];
then
echo "replace redis.conf port 6379 to port 6060 success!"
else
echo "replace redis.conf port 6379 to port 6060 error!"
return
fi
sed -i 's/daemonize no/daemonize yes/g' $redis_home/redis.conf
if [ $? -eq 0 ];
then
echo "replace redis.conf daemonize no to daemonize yes success!"
else
echo "replace redis.conf daemonize no to daemonize yes error!"
return
fi
echo "redis install and edit redis.conf success!"
}
function Start(){
cd $redis_home
if [ $? -eq 0 ];
then
echo $PWD
else
echo "cd $redis_home error!"
return
fi
./src/redis-server redis.conf
if [ $? -eq 0 ];
then
echo "redis server start success!"
else
echo "redis server start error,please find related reasons!"
fi
}
function Stop(){
pkill -f redis-server
if [ $? -eq 0 ];
then
echo "redis server stop success!"
return 0
else
echo "redis server stop error,please find related reasons!"
return 2
fi
return 0
}
function Restart(){
Stop
echo $?
result=$?
if [ $result -eq 0 ];
then
echo "redis server stop success!"
else
echo "redis server stop error!"
return
fi
sleep 1s
Start
}
function Log(){
echo "find log unrealized!"
}
if [ $# -lt 1 ]; then
echo "Usage: $0 [ install | start | stop | restart]"
exit 1
fi
Choose $2 $3

 

monitor.sh

#!/bin/bash

medicalsvcname="medicalrecordservice"
redissvcname="redisservice"
publish_path='/work/go/bin/publish.sh'

cur_path=$(cd "$(dirname "$0")"; pwd)

# 要定时执行的任务
task_cmd="/bin/sh /work/go/bin/monitorsvc.sh monitor > /tmp/monitor.log 2>&1&"
# 要添加的crontab任务
crontab_task="* * * * * ${task_cmd}"
# 备份原始crontab记录文件
crontab_bak_file="${cur_path}/crontab_bak"

function create_crontab()
{
echo 'Create crontab task...'
crontab -l > ${crontab_bak_file} 2>/dev/null
echo "${crontab_task}" >> ${crontab_bak_file}
crontab ${crontab_bak_file}

echo 'Complete'
}

function clear_crontab(){
echo 'Delete crontab task...'
crontab -l > ${crontab_bak_file} 2>/dev/null
sed -i "/.*${SCRIPT_NAME}/d" ${crontab_bak_file}
crontab ${crontab_bak_file}

echo 'Complete'
}
function monitor() {
echo "monitor medicalrecordservice process"
sleeptime=5
for (( i = 0; i < 60; i=$(( $i+$sleeptime )) ));
do
echo $i
medical=`/bin/ps -ef | grep $medicalsvcname | grep -v grep | awk '{print $2}'`
echo $medical
if [ ! -n "$medical" ];
then
echo "start medicalrecordservice and redisservice"
echo $PWD
cd /work/go/bin/
echo $PWD
/bin/sh ./publish.sh start
else
echo "medicalrecordservice is start............."
fi
sleep $sleeptime
done

}

if [ $# -lt 1 ]; then
echo "Usage: $0 [start | stop]"
exit 1
fi

case $1 in
'start' )
create_crontab
;;
'stop' )
clear_crontab
;;
'monitor' )
monitor
;;
esac

posted @ 2019-09-23 16:50  一直行走的小兵  阅读(750)  评论(0编辑  收藏  举报