I have a cell phone whose charging port doesn't connect well, and I want to be notified if it comes unplugged. I want this to work even if the phone is off, and not mounted as a file system, so I need to detect power drain. I will use notify-send to send the alert from a script.

It looks like powertop can show power usage by device, but it isn't parseable or easy to script with. I see that it is possible in to detect USB power usage in other operating systems.

I found some information on detecting USB devices, but the ones I tried only worked for mounted filesystems. I also found directions to turn USB devices on and off.

  • 1
    Check if lsusb -v can get you the info. Oct 24, 2017 at 18:06

1 Answer 1


Here's what I did to figure this out. First, I wrote a script to compare the output from some system info commands:

  mkdir "$1"
  ls /dev/disk/by-uuid/ > "$1"/uuids
  lsusb -v > "$1"/lsusb-v
  lsusb -t > "$1"/lsusb-t
  ls -alh /sys/bus/usb/devices/ | sed -E 's/.+ -> //' | tail -n+4 > "$1"/sysbus
echo "Insert plug and press ENTER."
read VAR; write_logs a
echo "Remove plug and press ENTER."
read VAR; write_logs b
diff -r a b

I didn't catch that the output was any different at first, because it takes my system a couple seconds to recognize a new USB device is attached. If you run this script, wait a few seconds before pressing ENTER. When I did this, I realized that the output for these commands really did change.

For my phone, I can detect if it is plugged in with this:

lsusb -v 2>/dev/null | grep 'Motorola PCS' >/dev/null \
&& echo phone is plugged in

I can detect if anything is plugged in to a particular port with this:

ls -alh /sys/bus/usb/devices/ | grep pci0000:00/0000:00:1d.7/usb2/2-6 >/dev/null \
&& echo Something is plugged into the left USB port.

I found these grep patterns just by comparing output from the above script; you may have to modify them for your own system or your own phone.

I'll still have to play around with how to activate my detection function and run it periodically to see if the device has been removed (probably using chron). Also, I haven't figured out how to detect how much power is being used; this merely detects if anything is connected or not.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.