Search code examples
pythonraspberry-pi4psutil

psutil fails to run if no monitor plugged into PI


I have a raspberry pi that on reboot runs a pyudev and psutil script to look for a removable storage device. The script runs perfectly fine if there is a monitor plugged in. But as soon as I unplug the screen and reboot the PI, it doesn't load the psutil. I can't even find what the error is as the error output is:

>@>@>@>@>@>@>@>@>@>@>@

Here is my script:

def checkstorage():
    context = pyudev.Context()
    removable = [device for device in context.list_devices(subsystem='block', DEVTYPE='disk') if device.attributes.asstring('removable') == "1"]
    for device in removable:
        partitions = [device.device_node for device in context.list_devices(subsystem='block', DEVTYPE='partition', parent=device)]
        print("All removable partitions: {}".format(", ".join(partitions)))
        print("Mounted removable partitions:")
        for p in psutil.disk_partitions():
            if p.device in partitions:
                print(" {}: {}".format(p.device, p.mountpoint))
                if p.device == "/dev/sda1":
                    path = p.mountpoint
                else:
                    path = 0
                return path, True

It fails by:

for p in psutil.disk_partitions()

If there is no HDMI screen plugged in


Solution

  • The issue was that as soon as the monitor was unplugged the PI acted like a headless PI. This intern caused psutil to fail as there was no partition associated with the usb. I fixed this by manually mounting the usb to a specific path I chose.