Managed USB Hub Bash Scripts

USB Watchdog: How the Script Works

A 20-line Bash script that notices when a USB device drops off the bus and power-cycles its port on a Coolgear Managed USB Hub so the device comes back without anyone unplugging a cable.

Without a managed hub

  • ✕Unplug and reconnect the cable by hand
  • ✕Reboot the whole computer
  • ✕Send a technician to a remote site

With USB_Watchdog.sh

  • ✓Failure is detected automatically
  • ✓Only the affected port is reset
  • ✓Every other USB device keeps running

What It’s Used For

Any USB peripheral that sometimes drops off the bus or hangs and recovers after a power cycle:

Industrial automationPLC interfaces and USB I/O on the factory floor
Machine visionCameras that hang after long capture sessions
LTE & 5G modemsRestore connectivity at unattended sites
Scanners & RFIDBarcode and RFID readers at kiosks and POS stations
Storage & backupExternal drives like the Western Digital drive used here
Remote & embeddedEdge boxes where nobody is around to replug cables

Requires a Coolgear Managed USB Hub with software-controlled ports, a Linux host with lsusb, and the Coolgear command-line utility (cusbi).

The Recovery Cycle

1

Check

lsusb lists connected devices; the script looks for “Western Digital”.

2

Found?

If the device is listed, print “Device detected.” and exit.

3

Port OFF

If it’s missing, the hub cuts power to port 2.

4

Wait 5 s

The device fully powers down and resets its state.

5

Port ON

Power returns, and Linux re-enumerates the device.

Walk Through the Script

Choose a scenario to step through the lines that run and see what the terminal prints. Hover over (or tap) the underlined commands to see what they do.

#!/bin/bash
TOOL=“./cusbi”
CONTROL_PORT=“ttyUSB0”
PORT=“2”
DEVICE=“Western Digital”

if lsusb | grep -qi “$DEVICE”; then
echo “Device detected.”
else
echo “Device not found.”
echo “Resetting USB Port…”
$TOOL /S:$CONTROL_PORT 0:$PORT
echo “Waiting 5 seconds…”
sleep 5
$TOOL /S:$CONTROL_PORT 1:$PORT
fi

Simulate a run


$ ./USB_Watchdog.sh
Coolgear Managed USB Hub
control port /dev/ttyUSB0
Port 1ON
Port 2ON
Port 3ON
Port 4ON


Western Digital
The highlighted port is PORT="2", where the watched drive is plugged in. Only that port is power-cycled.

The Settings

Variable Value What it does
TOOL ./cusbi Path to the Coolgear command-line utility that switches hub ports.
CONTROL_PORT ttyUSB0 The hub’s control port, /dev/ttyUSB0, which tells the utility which managed hub to talk to. Run ls /dev/ttyUSB* to find yours.
PORT 2 The hub port the watched device is plugged into.
DEVICE Western Digital Any text from the device’s lsusb line. The match is case-insensitive, so “western digital” also works.

The utility’s command format is cusbi /S:<control port> <state>:<port>, where state 0 turns the port off and 1 turns it on.

Run It Automatically

Every minute with cron

* * * * * /opt/coolgear/USB_Watchdog.sh

Or in a continuous loop

while true; do
    ./USB_Watchdog.sh
    sleep 30
done

Customize it

  • Watch a different device: change DEVICE="Camera" to any text from its lsusb line.
  • Reset a different port: change PORT="3".
  • Give slow devices more time: raise sleep 5 to sleep 10.
  • Need auto-detection? Advanced_Watchdog_with_port_detection.sh finds the device’s port from lsusb -v, remembers it, and checks that recovery worked.

Before deploying on Linux: keep #!/bin/bash as the first line so cron runs the script with Bash, and make it executable with chmod +x USB_Watchdog.sh (the cusbi utility needs execute permission too). Your user also needs access to /dev/ttyUSB0, usually through the dialout group.


Explore More. Automate More.

Discover more powerful automation scripts or explore Coolgear’s lineup of compatible Managed USB Hubs to expand your USB control capabilities.

View Compatible Managed USB Hubs Explore More Scripts

Looking for Assistance? Reach out to our tech team for help getting set up.