I have a simple bash script that disables a USB port if there is no internet connection and after five seconds enables it again. But SOMETIMES it fails, the script disables the port but is not enabled again. Note that I only have a one USB port. This is my script:
#!/bin/bash while true do sleep 10 if ping -c 2 google.com >> /dev/null 2>&1;then echo $(date +"%r") >> /root/log.txt echo "Conectado a SopelaBari" >> /root/log.txt else echo $(date +"%r") >> /root/log.txt echo "Sin conexion" >> /root/log.txt echo '2-1' |tee /sys/bus/usb/drivers/usb/unbind sleep 5 echo '2-1' |tee /sys/bus/usb/drivers/usb/bind sleep 5 fi sleep 30 done
I am running Ubuntu 14.04.05 and the script is run as root with crontab. As I am quite unfamiliar with bash: So I know that the code is improvable and probably fails due to some mistake I made.