Linux はコマンドラインからモニターが接続されているかどうかを判断できますか?

Linux はコマンドラインからモニターが接続されているかどうかを判断できますか?

コマンドラインから、モニターがコンピューターに接続されているかどうかを確認できるようにしたいです (スクリプトから X を実行するかどうかを決定できます)。どうすればできますか?

Xの存在に頼ることはできないので、有効なXディスプレイに依存するもの、例えば、は除外されますxrandrddccontrolですが、その出力は複数行に及び、解析するのは簡単ではありません。(理論的には、必要な方法で情報が利用できることはわかります)。モニターがシステムに接続されているかどうかを教えてくれる、よりスクリプトに適したプログラムはありますか?

これを実行したいシステムのほとんどは、Fedora 20 を実行しています。

答え1

私はこれを Fedora 20 で試していません (Ubuntu 13.04 でのみ) が、私の頭の中の理論は正しいです。

read-edid ( ) を使ってみることもできます。http://www.polypux.org/projects/read-edid/

これにより、XF86 互換のモードラインが生成され、解析できるようになります。

となることによってかもしれない動作します。おそらく、終了コードを確認するだけで済むほうがさらに簡単でしょう。

$ sudo get-edid > edid
get-edid: get-edid version 2.0.0

    Performing real mode VBE call
    Interrupt 0x10 ax=0x4f00 bx=0x0 cx=0x0
    Function supported
    Call successful

    VBE version 300
    VBE string at 0x11100 "NVIDIA"

VBE/DDC service about to be called
    Report DDC capabilities

    Performing real mode VBE call
    Interrupt 0x10 ax=0x4f15 bx=0x0 cx=0x0
    Function supported
    Call successful

    Monitor and video card combination does not support DDC1 transfers
    Monitor and video card combination supports DDC2 transfers
    0 seconds per 128 byte EDID block transfer
    Screen is not blanked during DDC transfer

Reading next EDID block

VBE/DDC service about to be called
    Read EDID

    Performing real mode VBE call
    Interrupt 0x10 ax=0x4f15 bx=0x1 cx=0x0
    Function supported
    Call failed

The EDID data should not be trusted as the VBE call failed
EDID claims 255 more blocks left
EDID blocks left is wrong.
Your EDID is probably invalid.
tom@tom:~$ parse-edid < edid 
parse-edid: parse-edid version 2.0.0
parse-edid: EDID checksum failed - data is corrupt. Continuing anyway.
parse-edid: first bytes don't match EDID version 1 header
parse-edid: do not trust output (if any).

    # EDID version 255 revision 255
Section "Monitor"
    Identifier "___:ffff"
    VendorName "___"
    ModelName "___:ffff"
    # DPMS capabilities: Active off:yes  Suspend:yes  Standby:yes

    Mode    "4095x4095" # vfreq 9.770Hz, hfreq 80.018kHz
        DotClock    655.350000
        HTimings    4095 4350 4605 8190
        VTimings    4095 4158 4221 8190
        Flags   "Interlace" "+HSync" "+VSync"
    EndMode
    Mode    "4095x4095" # vfreq 9.770Hz, hfreq 80.018kHz
        DotClock    655.350000
        HTimings    4095 4350 4605 8190
        VTimings    4095 4158 4221 8190
        Flags   "Interlace" "+HSync" "+VSync"
    EndMode
    Mode    "4095x4095" # vfreq 9.770Hz, hfreq 80.018kHz
        DotClock    655.350000
        HTimings    4095 4350 4605 8190
        VTimings    4095 4158 4221 8190
        Flags   "Interlace" "+HSync" "+VSync"
    EndMode
    Mode    "4095x4095" # vfreq 9.770Hz, hfreq 80.018kHz
        DotClock    655.350000
        HTimings    4095 4350 4605 8190
        VTimings    4095 4158 4221 8190
        Flags   "Interlace" "+HSync" "+VSync"
    EndMode
EndSection

答え2

次を使用して、現在接続されているディスプレイや「既知の」ディスプレイなどの情報を一覧表示できますxrandr

# list all known monitors
xrandr --listmonitors --verbose

# list active monitors
xrandr --listactivemonitors

少し grep などを使えば、これは機能します。manページを見て、自分のユースケースにさらに優れた機能があるかどうかを確認する価値はあります。

これはスクリプトにとって最も役立つと私が見つけたものです

xrandr | grep " connected " # =>
  # eDP-1
  # HDMI-1 # <-- the monitor I want to detect

# AND -- to check if `HDMI-1` is connected
xrandr | grep " connected " | awk '{ print$1 }' | grep HDMI-1

# store connection status in a var
$hdmi_connection=$(xrandr | grep " connected " | awk '{ print$1 }' | grep HDMI-1)

関連情報