2

I'm looking for a scripted way to check if the external monitor is on and connected.

My Ubuntu laptop often fails to turn on the external monitor after sleep or a blank screen. I've long since solved the problem using xset dpms force on; xset dpms force off;.

This solution is less than perfect because the monitor goes through a cycle of black screen / blue screen, and the xset operation must be executed while the monitor is in the black screen state. I often repeat the command multiple times until it works.

If I could check if the monitor is properly connected I could better automate the script that re-engages the monitor using xset dpms force on|off.

David Parks
  • 2,516

2 Answers2

4

You could filter (grep, awk, sed, etc.) the output of several commands that provide info on screens/monitors. If you only need to know if the external monitor is connected, more than one of them are possibly enough. I am listing below a few examples of what I see. I don't have an external monitor to test, but I guess you can work out the details.

  1. xrandr.

    Screen 0: minimum 320 x 200, current 1344 x 744, maximum 8192 x 8192
    LVDS connected primary 1344x744+0+0 (normal left inverted right x axis y axis) 344mm x 193mm
       1344x744_60.00  59.90*+
       ...
    HDMI-0 disconnected (normal left inverted right x axis y axis)
    VGA-0 disconnected (normal left inverted right x axis y axis)
    
  2. xdpyinfo

    ...
    default screen number:    0
    number of screens:    1
    

    screen #0: ... (a lot of useful information)

  3. inxi -aG

    As per man page, it can give info on the real monitor/s (e.g., "the real monitor size, not the Xorg full Screen diagonal size, which can be quite different"), although I am not seeing that.

  4. hwinfo | grep -i -B 3 -A 3 monitor

     ...
     37: None 00.0: 10002 LCD Monitor
       [Created at monitor.125]
       Unique ID: rdCR.0BRfQK9ozR8
       Parent ID: vSkL.mJc3+3Ia6n4
       Hardware Class: monitor
       Model: "AUO LCD Monitor"
       Vendor: AUO "AUO"
       Device: eisa 0x20ec
       Resolution: 1366x768@60Hz
     ...
    
1

I am using the Gnome extension Argos to check if my second DELL monitor is connected. And span the wallpaper if it is. Zoom the wallpaper if it isn't. (https://extensions.gnome.org/extension/1176/argos/)

The script checks every 15 seconds. (This is done by naming the file ?.1r.15s+.sh)

My Argos script file is: wallpaper.1r.15s+.sh

#!/usr/bin/env bash

connected=$(xrandr | awk '/ connected/ {count++} END {print count}') wallpaper=$(gsettings get org.gnome.desktop.background picture-options)

if [ "$connected" -gt 1 ] && [ "$wallpaper" == "'zoom'" ]; then span=$(gsettings set org.gnome.desktop.background picture-options spanned) fi

if [ "$connected" -lt 2 ] && [ "$wallpaper" == "'spanned'" ]; then span=$(gsettings set org.gnome.desktop.background picture-options zoom) fi echo " | iconName=folder-pictures-symbolic refresh=true trim=false" echo "---" echo "# screens: $connected" echo "Wallpaper: $wallpaper"