eibgrad

tomato-on-demand-services.sh

Jun 17th, 2020 (edited)
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.51 KB | None | 0 0
  1. #!/bin/sh
  2. #DEBUG=; set -x # uncomment/comment to enable/disable debug mode
  3.  
  4. #          name: tomato-on-demand-services.sh
  5. #       version: 1.0.1, 25-jun-2020, by eibgrad
  6. #       purpose: auto start/stop service(s) on-demand
  7. #   script type: init (autostart)
  8. #  installation:
  9. #    1. enable jffs (administration->jffs)
  10. #    2. enable syslog (administration->logging->syslog)
  11. #    3. use shell (telnet/ssh) to execute one of the following commands:
  12. #         curl -kLs bit.ly/tomato-installer|tr -d '\r'|sh -s -- N3Uc9TC0 init
  13. #       or
  14. #         wget -qO - bit.ly/tomato-installer|tr -d '\r'|sh -s -- N3Uc9TC0 init
  15. #    4. modify script w/ your preferred options:
  16. #         vi /jffs/etc/config/tomato-on-demand-services.init
  17. #    5. reboot
  18. (
  19. # ------------------------------ BEGIN OPTIONS ------------------------------- #
  20.  
  21. # device(s) to be monitored (fqdn or ip, space-separated)
  22. DEVICES='mikes-iphone'
  23.  
  24. # service(s) to be managed (based on process name, space-separated)
  25. SERVICES='vpnserver1 vpnserver2'
  26.  
  27. # interval between checks
  28. INTERVAL=60
  29.  
  30. # ------------------------------- END OPTIONS -------------------------------- #
  31.  
  32. # ---------------------- DO NOT CHANGE BELOW THIS LINE ----------------------- #
  33.  
  34. _ping() { ping -qc1 -w10 "$1" >/dev/null 2>&1; }
  35.  
  36. _echo() { echo "$* @ $(date)"; }
  37.  
  38. [[ "$DEVICES" && "$SERVICES" ]] || { echo 'nothing to do... exiting'; exit 0; }
  39.  
  40. # wait for internet access
  41. while ! _ping 8.8.8.8; do sleep 10; done
  42.  
  43. while :; do
  44.     ping_success=true
  45.  
  46.     for dev in $DEVICES; do
  47.         for i in 1 2 3; do
  48.             _ping $dev && continue 2
  49.         done
  50.  
  51.         ping_success=false; break
  52.     done
  53.  
  54.     # if all pings succeed, stop service(s)
  55.     # if at least one ping fails, start service(s)
  56.  
  57.     if [[ $ping_success == true ]]; then
  58.         for svc in $SERVICES; do
  59.             if pidof $svc >/dev/null 2>&1; then
  60.                 service $svc stop >/dev/null 2>&1
  61.                 _echo "$svc - stopped"
  62.             else
  63.                 [ ${DEBUG+x} ] && _echo "$svc - nothing to do"
  64.             fi
  65.         done
  66.     else
  67.         for svc in $SERVICES; do
  68.             if ! pidof $svc >/dev/null 2>&1; then
  69.                 service $svc start >/dev/null 2>&1
  70.                 _echo "$svc - started"
  71.             else
  72.                 [ ${DEBUG+x} ] && _echo "$svc - nothing to do"
  73.             fi
  74.         done
  75.     fi
  76.  
  77.     sleep $INTERVAL
  78. done
  79.  
  80. ) 2>&1 | logger $([ ${DEBUG+x} ] && echo "-p user.debug") \
  81.     -t $(echo $(basename $0) | grep -Eo '^.{0,23}')[$$] &
Add Comment
Please, Sign In to add comment