|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +show_help() { |
| 4 | +echo -e "\nrun command: \e[31mnohup bash /etc/cyberpanel/watchdog.sh SERVICE_NAME >/dev/null 2>&1 &\e[39m" |
| 5 | +echo -e "\nreplace \e[31mSERVICE_NAME\e[39m to the service name, acceptable word: \e[31mmariadb\e[39m or \e[31mlsws\e[39m" |
| 6 | +echo -e "\nWatchdog will check service status every 60 seconds and tries to restart if it is not running and also send an email to designated address" |
| 7 | +echo -e "\nto exit watchdog , run command \e[31mbash /etc/cyberpanel/watchdog.sh kill\e[39m" |
| 8 | +echo -e "\n\nplease also create \e[31m/etc/cyberpanel/watchdog.flag\e[39m file with following format:" |
| 9 | +echo -e "TO=address@email.com" |
| 10 | +echo -e "SENDER=sender name" |
| 11 | +echo -e "FROM=sender@email.com" |
| 12 | +echo -e "You may proceed without flag file , but that will make email sending failed." |
| 13 | +} |
| 14 | + |
| 15 | +watchdog_check() { |
| 16 | +echo -e "\nChecking LiteSpeed ..." |
| 17 | +pid=$(ps aux | grep "watchdog lsws" | grep -v grep | awk '{print $2}') |
| 18 | + if [[ "$pid" == "" ]] ; then |
| 19 | + echo -e "\nWatchDog for LSWS is gone , restarting..." |
| 20 | + nohup watchdog lsws > /dev/null 2>&1 & |
| 21 | + echo -e "\nWatchDog for LSWS has been started..." |
| 22 | + else |
| 23 | + echo -e "\nWatchDog for LSWS is running...\n" |
| 24 | + echo $(ps aux | grep "watchdog lsws" | grep -v grep) |
| 25 | + fi |
| 26 | +echo -e "\nChecking MariaDB ..." |
| 27 | +pid=$(ps aux | grep "watchdog mariadb" | grep -v grep | awk '{print $2}') |
| 28 | + if [[ "$pid" == "" ]] ; then |
| 29 | + echo -e "\nWatchDog for MariaDB is gone , restarting..." |
| 30 | + nohup watchdog mariadb > /dev/null 2>&1 & |
| 31 | + echo -e "\nWatchDog for MariaDB has been started..." |
| 32 | + else |
| 33 | + echo -e "\nWatchDog for MariaDB is running...\n" |
| 34 | + echo $(ps aux | grep "watchdog mariadb" | grep -v grep) |
| 35 | + fi |
| 36 | +} |
| 37 | + |
| 38 | +if [[ $1 == "mariadb" ]] || [[ $1 == "database" ]] || [[ $1 == "mysql" ]] ; then |
| 39 | + NAME="mariadb" |
| 40 | + echo "Watchdog on MariaDB is started up ..." |
| 41 | +elif [[ $1 == "web" ]] || [[ $1 == "lsws" ]] || [[ $1 == "litespeed" ]] || [[ $1 == "openlitespeed" ]] ; then |
| 42 | + NAME="lsws" |
| 43 | + echo "Watchdog on LiteSpeed is started up ..." |
| 44 | +elif [[ $1 == "help" ]] || [[ $1 == "-h" ]] || [[ $1 == "--help" ]] || [[ $1 == "" ]] ; then |
| 45 | + show_help |
| 46 | +exit |
| 47 | +elif [[ $1 == "check" ]] || [[ $1 == "status" ]] ; then |
| 48 | + watchdog_check |
| 49 | + exit |
| 50 | +elif [[ $1 == "kill" ]] ; then |
| 51 | +pid=$(ps aux | grep "watchdog lsws" | grep -v grep | awk '{print $2}') |
| 52 | +if [[ "$pid" != "" ]] ; then |
| 53 | +kill -15 $pid |
| 54 | +fi |
| 55 | +pid=$(ps aux | grep "watchdog mariadb" | grep -v grep | awk '{print $2}') |
| 56 | +if [[ "$pid" != "" ]] ; then |
| 57 | +kill -15 $pid |
| 58 | +fi |
| 59 | +echo "watchdo has been killed..." |
| 60 | +exit |
| 61 | +else |
| 62 | +show_help |
| 63 | + |
| 64 | +echo -e "\n\n\nunknown service name..." |
| 65 | +exit |
| 66 | +fi |
| 67 | + |
| 68 | + |
| 69 | + |
| 70 | +while [ true = true ] |
| 71 | + do |
| 72 | + systemctl status $NAME 2>&1>/dev/null |
| 73 | + if [[ $? == "0" ]] ; then |
| 74 | + if [[ $NAME == "mariadb" ]] ; then |
| 75 | + pid=$(ps aux | grep "/usr/sbin/mysqld" | grep -v grep | awk '{print $2}') |
| 76 | + echo "-1000" > /proc/$pid/oom_score_adj |
| 77 | + fi |
| 78 | + echo "$NAME service is running..." |
| 79 | + else |
| 80 | + echo "$NAME is down , try to restart it..." |
| 81 | + if [[ $NAME == "lsws" ]] ; then |
| 82 | + pkill lsphp |
| 83 | + fi |
| 84 | + if [[ $NAME == "mariadb" ]] ; then |
| 85 | + pid=$(ps aux | grep "/usr/sbin/mysqld" | grep -v grep | awk '{print $2}') |
| 86 | + echo "-1000" > /proc/$pid/oom_score_adj |
| 87 | + fi |
| 88 | + systemctl stop $NAME |
| 89 | + systemctl start $NAME |
| 90 | + if [ -f /etc/cyberpanel/watchdog.flag ] ; then |
| 91 | + flag="/etc/cyberpanel/watchdog.flag" |
| 92 | + LINE3=$(awk 'NR==3' $flag) |
| 93 | + LINE2=$(awk 'NR==2' $flag) |
| 94 | + LINE1=$(awk 'NR==1' $flag) |
| 95 | + |
| 96 | + FROM=${LINE3#*=} |
| 97 | + SENDER=${LINE2#*=} |
| 98 | + TO=${LINE1#*=} |
| 99 | + sendmail -F $SENDER -f $FROM -i $TO <<MAIL_END |
| 100 | +Subject: $NAME is down... |
| 101 | +To: $TO |
| 102 | +$NAME is down , watchdog attempted to restarting it... |
| 103 | +
|
| 104 | +MAIL_END |
| 105 | + fi |
| 106 | + fi |
| 107 | + sleep 60 |
| 108 | +done |
0 commit comments