Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/sh
- #DEBUG=; set -x # uncomment/comment to enable/disable debug mode
- # name: tomato-on-demand-services.sh
- # version: 1.0.1, 25-jun-2020, by eibgrad
- # purpose: auto start/stop service(s) on-demand
- # script type: init (autostart)
- # installation:
- # 1. enable jffs (administration->jffs)
- # 2. enable syslog (administration->logging->syslog)
- # 3. use shell (telnet/ssh) to execute one of the following commands:
- # curl -kLs bit.ly/tomato-installer|tr -d '\r'|sh -s -- N3Uc9TC0 init
- # or
- # wget -qO - bit.ly/tomato-installer|tr -d '\r'|sh -s -- N3Uc9TC0 init
- # 4. modify script w/ your preferred options:
- # vi /jffs/etc/config/tomato-on-demand-services.init
- # 5. reboot
- (
- # ------------------------------ BEGIN OPTIONS ------------------------------- #
- # device(s) to be monitored (fqdn or ip, space-separated)
- DEVICES='mikes-iphone'
- # service(s) to be managed (based on process name, space-separated)
- SERVICES='vpnserver1 vpnserver2'
- # interval between checks
- INTERVAL=60
- # ------------------------------- END OPTIONS -------------------------------- #
- # ---------------------- DO NOT CHANGE BELOW THIS LINE ----------------------- #
- _ping() { ping -qc1 -w10 "$1" >/dev/null 2>&1; }
- _echo() { echo "$* @ $(date)"; }
- [[ "$DEVICES" && "$SERVICES" ]] || { echo 'nothing to do... exiting'; exit 0; }
- # wait for internet access
- while ! _ping 8.8.8.8; do sleep 10; done
- while :; do
- ping_success=true
- for dev in $DEVICES; do
- for i in 1 2 3; do
- _ping $dev && continue 2
- done
- ping_success=false; break
- done
- # if all pings succeed, stop service(s)
- # if at least one ping fails, start service(s)
- if [[ $ping_success == true ]]; then
- for svc in $SERVICES; do
- if pidof $svc >/dev/null 2>&1; then
- service $svc stop >/dev/null 2>&1
- _echo "$svc - stopped"
- else
- [ ${DEBUG+x} ] && _echo "$svc - nothing to do"
- fi
- done
- else
- for svc in $SERVICES; do
- if ! pidof $svc >/dev/null 2>&1; then
- service $svc start >/dev/null 2>&1
- _echo "$svc - started"
- else
- [ ${DEBUG+x} ] && _echo "$svc - nothing to do"
- fi
- done
- fi
- sleep $INTERVAL
- done
- ) 2>&1 | logger $([ ${DEBUG+x} ] && echo "-p user.debug") \
- -t $(echo $(basename $0) | grep -Eo '^.{0,23}')[$$] &
Add Comment
Please, Sign In to add comment