99.Shell脚本自启程序并监控(实现全自动监控服务)
生活随笔
收集整理的這篇文章主要介紹了
99.Shell脚本自启程序并监控(实现全自动监控服务)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
添加重啟后執行任務
crontab -e
加入
@reboot nohup sh /root/scripts/init.sh &
編輯init.sh內容
#!/bin/bash #導入jre必須手動導入 不然啟動不了(巨坑) export JAVA_HOME=/app/jdk1.8.0_211 export JRE_HOME=${JAVA_HOME}/jre export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib export PATH=${JAVA_HOME}/bin:$PATHecho 'init...' systemctl stop firewalld.service sleep 5 nohup sh /root/scripts/startup.sh & sleep 5 nohup sh /root/scripts/startupApollo.sh>>/root/scripts/apollo.log & echo 'init success'startup.init控制mysql和主主模式配置并監控mysql服務
#author by xiaokai 2019-07-31 #啟動另外一個腳本 #count=`ps -ef | grep demo1.sh | grep -v "grep" | wc -l` #echo $count #sh demo1.sh>demo1.log &while true do sleep 5 count=`ps -ef | grep mysql | grep -v "grep" | wc -l` #echo $count if [ 0 == $count ]; then#進程不存在time2=$(date "+%Y-%m-%d %H:%M:%S")# echo "mysql服務宕機,正在重新啟動"echo 'mysql服務宕機,宕機時間為:'$time2>>error.logsleep 2service mysqld restartsleep 15sh setMysql.shsleep 5 echo 'mysql重新啟動成功!!!' #else#rest進程存在,殺掉進程# echo "mysql服務運行正常...持續監控中" fi # time=$(date "+%Y-%m-%d %H:%M:%S") # echo "本輪監測完畢,當前時間為:"$time donestartupApollo.sh 開機啟動apollo并監控apollo服務(支持自動重啟)
#!/bin/bash #author by xiaokai 2019-07-31 #自動重啟apollo服務腳本 #count=`ps -ef | grep demo1.sh | grep -v "grep" | wc -l` #echo $count #sh demo1.sh>demo1.log & export JAVA_HOME=/app/jdk1.8.0_211 export JRE_HOME=${JAVA_HOME}/jre export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib export PATH=${JAVA_HOME}/bin:$PATHwhile true do sleep 5 count=`ps -ef | grep 10010 | grep -v "grep" | wc -l` #echo $count if [ 0 == $count ]; then#進程不存在time2=$(date "+%Y-%m-%d %H:%M:%S")# echo "mysql服務宕機,正在重新啟動"echo 'apolloadmin服務宕機,宕機時間為:'$time2>>error.logsleep 5sh /app/apolloadmin/scripts/startup.sh>>apollo.log sleep 5 echo 'apolloadmin重新啟動成功!!!' fi#檢測apolloconfig進程 sleep 1 count2=`ps -ef | grep 10020 | grep -v "grep" | wc -l` #echo $count if [ 0 == $count2 ]; then#進程不存在time2=$(date "+%Y-%m-%d %H:%M:%S")echo 'apolloconfig服務宕機,宕機時間為:'$time2>>error.logsleep 5sh /app/apolloconfig/scripts/startup.sh>>apollo.logsleep 5echo 'apolloconfig重新啟動成功!!!' fi#檢測apollopor進程 sleep 1 count3=`ps -ef | grep 10030 | grep -v "grep" | wc -l` #echo $count if [ 0 == $count3 ]; then#進程不存在time2=$(date "+%Y-%m-%d %H:%M:%S")# echo "mysql服務宕機,正在重新啟動"echo 'apolloportal服務宕機,宕機時間為:'$time2>>error.logsleep 5sh /app/apolloportal/scripts/startup.sh>>apollo.logsleep 5echo 'apolloportal重新啟動成功!!!' fi#檢測haproxy進程 count3=`ps -ef | grep haproxy | grep -v "grep" | wc -l` #echo $count if [ 0 == $count3 ]; then#進程不存在time2=$(date "+%Y-%m-%d %H:%M:%S")# echo "haproxy服務宕機,正在重新啟動"echo 'haproxy服務宕機,宕機時間為:'$time2>>error.logsleep 2/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/conf/haproxy.cnfsleep 5echo 'haproxy重新啟動成功!!!' fidonesetmysql.sh 配置mysql 主主模式腳本
#!/bin/bash #author by xiaokai 2019-07-29 #-*- coding:utf-8 -*-#重啟數據庫 #echo 'restart mysql1 node...' #docker restart mysql1 #echo 'restart mysql2 node...' #docker restart mysql2echo -e "\e[1;34m connect mysql node1... \n" echo 'connect mysql node1...' #數據庫ip HOSTNAME1="172.26.2.114" #數據庫端口 PORT1="3306" #數據庫用戶名 USERNAME1="xiaokai" #數據庫密碼 PASSWORD1="123456" #數據庫名 DBNAME1="mysql" #查詢語句 select_sql="show master status" #*#執行查詢* result1=`mysql -h${HOSTNAME1} -P${PORT1} -u${USERNAME1} -p${PASSWORD1} ${DBNAME1} -Bse "${select_sql}"` node1_file_position=`echo ${result1} | awk '{print $1}'` #獲取到的第一個值 node1_Postion=`echo ${result1} | awk '{print $2}'` #獲取到的第二個值 node1_comtend=`echo ${result1} | awk '{print $n}'` #獲取到的第N個值echo 'node1_file_position:'$node1_file_position echo 'node1_Postion:'$node1_Postion echo 'node1_comtend:'$node1_comtendecho '***************************' echo 'connect mysql node2...' #數據庫ip HOSTNAME2="172.26.2.115" #數據庫端口 PORT2="3306" #數據庫用戶名 USERNAME2="xiaokai" #數據庫密碼 PASSWORD2="123456" #數據庫名 DBNAME2="mysql" #查詢語句 select_sql="show master status" #*#執行查詢* result2=`mysql -h${HOSTNAME2} -P${PORT2} -u${USERNAME2} -p${PASSWORD2} ${DBNAME2} -Bse "${select_sql}"`node2_file_position=`echo ${result2} | awk '{print $1}'` #獲取到的第一個值 node2_Postion=`echo ${result2} | awk '{print $2}'` #獲取到的第二個值 node2_comtend=`echo ${result2} | awk '{print $n}'` #獲取到的第N個值echo 'node2_file_position:'$node2_file_position echo 'node2_Postion:'$node2_Postion echo 'node2_comtend:'$node2_comtend#創建同步帳號并授權 create_acount_sql="create user 'xiaokai'@'%' identified by '123456" `mysql -h${HOSTNAME1} -P${PORT1} -u${USERNAME1} -p${PASSWORD1} ${DBNAME1} -Bse "${create_acount_sql}"` grant_acount_sql="grant all on *.* to 'xiaokai'@'%' identified by '123456'" `mysql -h${HOSTNAME1} -P${PORT1} -u${USERNAME1} -p${PASSWORD1} ${DBNAME1} -Bse "${grant_acount_sql}"``mysql -h${HOSTNAME2} -P${PORT2} -u${USERNAME2} -p${PASSWORD2} ${DBNAME2} -Bse "${create_acount_sql}"` `mysql -h${HOSTNAME2} -P${PORT2} -u${USERNAME2} -p${PASSWORD2} ${DBNAME2} -Bse "${grant_acount_sql}"`echo '同步帳號授權完畢'#配置從節點寫入 stop_slave_sql="stop slave" `mysql -h${HOSTNAME1} -P${PORT1} -u${USERNAME1} -p${PASSWORD1} ${DBNAME1} -Bse "${stop_slave_sql}"` `mysql -h${HOSTNAME2} -P${PORT2} -u${USERNAME2} -p${PASSWORD2} ${DBNAME2} -Bse "${stop_slave_sql}"` #配置node1 change_master_node1="CHANGE MASTER TO MASTER_HOST='${HOSTNAME1}',MASTER_PORT=${PORT1},MASTER_USER='${USERNAME1}',MASTER_PASSWORD='${PASSWORD1}',MASTER_LOG_FILE='${node1_file_position}',MASTER_LOG_POS=${node1_Postion}" change_master_node2="CHANGE MASTER TO MASTER_HOST='${HOSTNAME2}',MASTER_PORT=${PORT2},MASTER_USER='${USERNAME2}',MASTER_PASSWORD='${PASSWORD2}',MASTER_LOG_FILE='${node2_file_position}',MASTER_LOG_POS=${node2_Postion}"echo ${change_master_node1} echo ${change_master_node2}echo '===============================================' start_slave_sql="start slave"`mysql -h${HOSTNAME2} -P${PORT2} -u${USERNAME2} -p${PASSWORD2} ${DBNAME2} -Bse "${change_master_node1}"` sleep 1s `mysql -h${HOSTNAME2} -P${PORT2} -u${USERNAME2} -p${PASSWORD2} ${DBNAME2} -Bse "${start_slave_sql}"` sleep 1s `mysql -h${HOSTNAME1} -P${PORT1} -u${USERNAME1} -p${PASSWORD1} ${DBNAME1} -Bse "${change_master_node2}"` sleep 1s `mysql -h${HOSTNAME1} -P${PORT1} -u${USERNAME1} -p${PASSWORD1} ${DBNAME1} -Bse "${start_slave_sql}"`#回調slave狀態 show_slave_sql="show slave status" slave1_status=`mysql -h${HOSTNAME1} -P${PORT1} -u${USERNAME1} -p${PASSWORD1} ${DBNAME1} -Bse "${show_slave_sql}"` slave_node1_status=`echo ${slave1_status} | awk '{print $n}'` echo 'node1:'${slave_node1_status} echo '執行完畢'echo -e '\e[0m' #查詢語句 #select_sql2='select now()' #dateResult=`mysql -h${HOSTNAME} -P${PORT} -u${USERNAME} -p${PASSWORD} ${DBNAME} -Bse "${select_sql2}"` #time2=`echo ${dateResult}|awk '{print $n}'` #echo 'time:'$time2?
?
?
?
總結
以上是生活随笔為你收集整理的99.Shell脚本自启程序并监控(实现全自动监控服务)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 黑电平校正、FPN校正、平场校正、白平衡
- 下一篇: 中学生早恋主题班会ppt(中学生早恋吻视