Last active
August 14, 2024 18:27
-
-
Save jcarletto27/37a544d68d0bbbf277e767d88104be08 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# My raspberry pi 5 is constantly disconnecting the USB hardrive after a few hours with nothing really showing in the logs | |
# so i created this script to check to see if it's still showing, and if not reset the hub and authorization and then remount all drives | |
#You need to use lsusb or usb-devices to find the Vendor and prod ids usually like 152d:0580 or Vendor=152d ProdID=0580 | |
#You'll also need uhubctl installed to to toggle power to the hubs | |
## use this command to download it, set it and add it to the crontab | |
## | |
<<comment | |
/bin/bash -c "sudo wget --output-document=/tmp/usb-hub-reset https://gist.githubusercontent.com/jcarletto27/37a544d68d0bbbf277e767d88104be08/raw/usb-hub-reset && sudo mv /tmp/usb-hub-reset /usr/local/bin/usb-hub-reset && sudo chmod +x /usr/local/bin/usb-hub-reset; sudo crontab -l | sudo tee /tmp/crontab; grep 'usb-hub-reset' /tmp/crontab || (echo '*/5 * * * * /usr/local/bin/usb-hub-reset > /var/log/usb-hub-reset.log' | sudo tee -a /tmp/crontab; sudo crontab /tmp/crontab);usb-hub-reset" | |
comment | |
##Can be an array of ids to check for | |
VENDOR_PRODS=("Vendor=152d ProdID=0580" "Vendor=152d ProdID=0580") | |
if [ $(dpkg-query -W -f='${Status}' uhubctl 2>/dev/null | grep -c "ok installed") -eq 0 ]; then | |
sudo apt-get install uhubctl | |
fi | |
for VENDOR_PROD in "${VENDOR_PRODS[@]}"; do | |
echo $VENDOR_PROD | |
if usb-devices | grep "${VENDOR_PROD}" >/dev/null; then | |
echo "USB Device still attached" | |
else | |
echo "Resetting USB Hub in software" | |
sudo uhubctl -l 1 -a 0 | |
sudo uhubctl -l 3 -a 0 | |
sleep 2 | |
sudo uhubctl -l 1 -a 1 | |
sudo uhubctl -l 3 -a 1 | |
sleep 2 | |
echo "Disabling authorized state for USB ports" | |
echo 0 | sudo tee /sys/bus/usb/devices/usb1/authorized >/dev/null | |
echo 0 | sudo tee /sys/bus/usb/devices/usb2/authorized >/dev/null | |
echo 0 | sudo tee /sys/bus/usb/devices/usb3/authorized >/dev/null | |
echo 0 | sudo tee /sys/bus/usb/devices/usb4/authorized >/dev/null | |
sleep 2 | |
echo "Enabling authorized state for USB ports" | |
echo 1 | sudo tee /sys/bus/usb/devices/usb1/authorized >/dev/null | |
echo 1 | sudo tee /sys/bus/usb/devices/usb2/authorized >/dev/null | |
echo 1 | sudo tee /sys/bus/usb/devices/usb3/authorized >/dev/null | |
echo 1 | sudo tee /sys/bus/usb/devices/usb4/authorized >/dev/null | |
sleep 2 | |
echo "reloading fstab with systemctl" | |
sudo systemctl daemon-reload | |
sleep 2 | |
echo "remounting all attached drives" | |
sudo mount -a -v | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment