Skip to content

Instantly share code, notes, and snippets.

@ran-dall
Created August 26, 2022 14:24
Show Gist options
  • Save ran-dall/58cac418216cfba75e1549275176dc95 to your computer and use it in GitHub Desktop.
Save ran-dall/58cac418216cfba75e1549275176dc95 to your computer and use it in GitHub Desktop.
Disable a device from resuming system after suspending on Linux
#!/bin/bash
# Adapted from uglic's StackOverflow response
# https://askubuntu.com/questions/1328823/disable-specific-device-from-waking-up-the-system
while getopts v:p:s: flag
do
case "${flag}" in
v) vendor=${OPTARG};;
p) product=${OPTARG};;
s) state=${OPTARG};;
esac
done
if [ -z "$vendor" -o -z "$product" ]; then
echo -en "Usage: $0 -v vendorId -p productId -s state\nin any order, where vendorId and productId are both from [lsusb] and state can be enable or disable\n"
exit 1;
fi
if [ -z $state ]; then
stateTo="disabled"
fi
DEVICES=/sys/bus/usb/devices
for a in `ls $DEVICES`; do
if [ -f "$DEVICES/$a/idVendor" -a -f "$DEVICES/$a/idProduct" ]; then
idVendor=`cat "$DEVICES/$a/idVendor"`
idProduct=`cat "$DEVICES/$a/idProduct"`
if [ -f "$DEVICES/$a/product" ]; then
productName=`cat "$DEVICES/$a/product"`
fi
WAKEUPFILE="$DEVICES/$a/power/wakeup"
if [ $idVendor = $vendor -a $idProduct = $product -a -f "$WAKEUPFILE" ]; then
oldState=`cat "$WAKEUPFILE"`
echo "$state" > "$WAKEUPFILE"
newState=`cat "$WAKEUPFILE"`
echo Bus-port:$a vendor=$idVendor product=$idProduct name=$productName WakeUp: old=$oldState new=$newState
fi
fi
done
[Unit]
Description=Disable resume from sleep from Razer Basilisk mouse
Before=sleep.target
[Service]
Type=oneshot
ExecStart=/usr/bin/usb-suspend-control -v 1532 -p 0086 -s disabled
StandardOutput=journal
[Install]
WantedBy=sleep.target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment