Skip to content

Instantly share code, notes, and snippets.

@kostecky
Last active March 7, 2016 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kostecky/bddda14fe74d835feb05 to your computer and use it in GitHub Desktop.
Save kostecky/bddda14fe74d835feb05 to your computer and use it in GitHub Desktop.
Launch with `flock -n /var/run/torrent_vpn_control.pid /root/torrent_vpn_control.sh` out of crontab every minute
#!/bin/ash
last_vpn_up=-1
count=0
while :; do
ip route list | grep -q "default via .* dev tun0"
vpn_up=$?
count=$(($count+1))
if [[ $last_vpn_up -ne $vpn_up ]] || [[ $count -ge 60 ]]; then
if [[ $vpn_up -eq 0 ]]; then
echo "VPN just connected, resuming torrents..."
while :; do
paused_jobs=`/root/node_modules/syno/bin/syno.js dl listTasks | jq '[.tasks[]|select(.status=="paused").id]'`
if [[ "$paused_jobs" == "[]" ]]; then
break
fi
/root/node_modules/syno/bin/syno.js dl resumeTasks --payload "{\"id\": $paused_jobs}"
sleep 1
done
else
echo "VPN just disconnected, pausing torrents..."
while :; do
unpaused_jobs=`/root/node_modules/syno/bin/syno.js dl listTasks | jq '[.tasks[]|select(.status!="paused").id]'`
if [[ "$unpaused_jobs" == "[]" ]]; then
break
fi
/root/node_modules/syno/bin/syno.js dl pauseTasks --payload "{\"id\": $unpaused_jobs}"
sleep 1
done
fi
count=0
fi
last_vpn_up=$vpn_up
sleep 1
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment