3

I am new to linux, and this is probably a noob question, so, sorry for that.

I am using a linux PC in my workshop, and I would like to automate the workshop. That's why I have installed a power strip where all my tools are plugged in to. That way it is easy to turn of all my tools all at ones.

I also have a usb-hub's power input plugged in there. When it is turned of, the pc detects it as disconnected. Can I use that information to automatically lock or suspend the PC when it is disconnected?

TLDR; Can I make a script that suspends the PC when a usb device is disconnected?

  • Run tail -f /var/log/syslog.log and try to connect/disconnect your USB device. – FedonKadifeli Sep 22 at 14:10
  • Hi lenduck, just curious, but did you try/manage? – Jacob Vlijm Sep 25 at 11:28
4

Action on (dis) connecting usb device

The tiny script below will suspend the computer on the event of disconnecting a (any) usb volume.

#!/usr/bin/env python3
import gi
from gi.repository import GLib, Gio
import subprocess

class WatchOut:

    def __init__(self):
        someloop = GLib.MainLoop()
        self.setup_watching()
        someloop.run()

    def setup_watching(self):
        self.watchdrives = Gio.VolumeMonitor.get()
        # event to watch (see further below, "Other options")
        self.watchdrives.connect("volume_removed", self.actonchange)

    def actonchange(self, *args):
        # command to run (see further below, "Other options")
        subprocess.Popen(["systemctl", "suspend"])

WatchOut()

To use:

  • Copy the script into an empty file, save it as watchout.py
  • Run it in the background:

    python3 /path/to/watchout.py
    
  • If all works as expected, run it from startup

Other options

In this script, obviously the signal on "volume_removed" is used. Other possible events:

"volume_added", "volume_removed", "mount_added", "mount_removed"

To make it perform other commands on event, replace the command in the line:

subprocess.Popen(["systemctl", "suspend"])

(command and args are set as a list, like ["systemctl", "suspend"])

EDIT - Only act on specifically named volumes

As mentioned in a comment, you'd prefer to only run a command on specifically named volumes. If you run the example below with one or more volumenames as arguments, action will be limited to only those volumes:

#!/usr/bin/env python3
import gi
from gi.repository import GLib, Gio
import subprocess
import sys

args = sys.argv[1:]

class WatchOut:

    def __init__(self):
        someloop = GLib.MainLoop()
        self.setup_watching()
        someloop.run()

    def setup_watching(self):
        self.watchdrives = Gio.VolumeMonitor.get()
        # event to watch (see further below, "Other options")
        self.watchdrives.connect("volume_removed", self.actonchange)

    def actonchange(self, event, volume):
        if volume.get_name() in args:
            # command to run (see further below, "Other options")
            subprocess.Popen(["systemctl", "suspend"])

WatchOut()

Usage

Is pretty much the same as the first one, just add the volumenames as arguments, e.g.

python3 /path/to/watchout.py <volumename_1> <volumename_2>
  • Hi! This seems really good! Thanks for the help, but I've got one question. Can I specify the volume to detect? – LenDuck24 Sep 23 at 17:07
  • @LenDuck24 If e.g. name would be specific, absolutely! Just mention an example, I'll edit it in as an example. – Jacob Vlijm Sep 23 at 17:31
2

Simple way to detect usb events is

 udevadm monitor --kernel --property --subsystem-match=usb

Programmatical approach: https://raw.githubusercontent.com/gavv/snippets/master/udev/udev_monitor_usb.c

Other related topics:

How can I run code whenever a USB device is (un)plugged, without requiring root permissions? https://unix.stackexchange.com/questions/65891/how-to-execute-a-shellscript-when-i-plug-in-a-usb-device

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.