8

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.

7

Try verifying the output of the file:

/sys/class/drm/card0-HDMI-A-1/status

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 ]
do
    STATUS=`cat /sys/class/drm/card0-HDMI-A-1/status`
    if [ "$STATUS" == "disconnected" ]; then
        echo "turning off"
        sudo shutdown now
    fi
    #sleep for 10 seconds and check again
    sleep 10
done

To run it automatically on background, follow this link.

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.