watchdog.sh 1.2KB

12345678910111213141516171819202122232425
  1. #!/bin/bash
  2. export PROC=$(ps aux | awk '{print $11}' | sort | uniq | egrep -v "^COMMAND$|^awk$|^sort$|^uniq$|^grep$|^sed$|^tee$|^echo$|^sleep$|^init$|^-bash$|^/bin/bash$|^/bin/sh$" | sed -e 's/:$//' | tee /var/proc);
  3. function poke() {
  4. [[ "$(id -u)" -ne "0" ]] && echo "Diese Funktion muss als root ausgeführt werden" >&2 && return 1;
  5. [[ "$1" == "" ]] && echo "Syntax: poke [Programmname] {(Re)Start-Script} {(Re)Start-Argument}" >&2 && return 1;
  6. [[ "$2" == "" ]] && CMD="/etc/init.d/$1" || CMD=$2;
  7. [[ "$3" == "" ]] && ARG="restart" || ARG=$3;
  8. [[ "$4" =~ ^[0-9]+$ ]] && PORT=$4 || PORT=0;
  9. if [ -x "$CMD" ]; then
  10. echo -e "$PROC" grep $1 >/dev/null 2>&1; RES1=$?;
  11. if [[ "$4" =~ "^[0-9]+$" ]] && [ "$4" -le "65535" ] && [ "$4" -ge "1" ]; then
  12. echo "PING" | nc -t 127.0.0.1 $PORT >/dev/null 2>&1; RES2=$?;
  13. else RES2=0; fi;
  14. if [ "$RES1" -ne "0" ] || [ "$RES2" -ne "0" ]; then echo "Restarte $1"; $CMD $ARG; else echo "$1 läuft bereits"; fi;
  15. else
  16. echo "$1 ($CMD) ist auf diesem System nicht installiert";
  17. fi
  18. sleep 1 && return 0;
  19. }
  20. poke mysql /etc/init.d/mysql restart 3306;
  21. poke nginx /etc/init.d/nginx restart 80;
  22. poke gogs /etc/init.d/gogs restart 3000;