How can I programmatically detect if the Raspberry Pi's HDMI cable is removed? I've tried tools like xrandr, but they give the same output regardless of whether the HDMI cable is plugged in or not.

1 Answer 1


Try verifying the output of the file:


The part card0-HDMI-A-1 may be a little different (verify the parent directory for the correct file) and the content should be connected or disconnected. Also works for another connections, like VGA, LVDS, etc.
After this, simply make a script like:

while [ 1 ]
    STATUS=`cat /sys/class/drm/card0-HDMI-A-1/status`
    if [ "$STATUS" == "disconnected" ]; then
        echo "turning off"
        sudo shutdown now
    #sleep for 10 seconds and check again
    sleep 10

To run it automatically on background, follow this link.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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