Topic: [Solved]Detect if monitors are connected?

Hi!
I want to use a different conky setup depending on if I am using dual external monitors, or if i'm just using my laptop's screen.
Is there a way to check if a certain display is connected? my two external are 'DFP1' and 'CRT1', my laptop's display is 'LVDS'.
I'm thinking maybe something randr can handle?

EDIT: I figured it out. I used the command xrandr, which outputs a list of connected displays to the terminal, piped that into a text file, then read the text file with grep looking for CRT1, if it was found, i started a conky_dual setup, and if it wasn't, i started a conky_single setup.
Hooray!

Last edited by FiniteStateMachine (2010-08-21 17:47:18)

just call me...
~FSM~

Re: [Solved]Detect if monitors are connected?

why would you pipe it to a text file?
why not xrandr | grep .... straight away?

a.

Re: [Solved]Detect if monitors are connected?

I was using it to debug actually.

just call me...
~FSM~

Re: [Solved]Detect if monitors are connected?

hi,

alon_h wrote:

why would you pipe it to a text file?
why not xrandr | grep .... straight away?

could you give the whole line of the 'xrandr | grep...' command? that would be great! i looked for a way to autodetect whether my external monitor was connected to my laptop to automtically use different display settings. but i'm not really good at everything beyond very basic scripting. sad but I hopefully should be able to adjust your example to my system. smile

thanx!

Re: [Solved]Detect if monitors are connected?

hi janek

first open a terminal and just run xrandr. you should be able to find the indentifier of your external monitor. My bet is it is CRT1
heres a small script, assuming its CRT1. you can fill in what you want it to do, or ask for some guidance if you need it:

#!/bin/sh

#check if CRT1 is connected
if (xrandr | grep "CRT1 connected" > /dev/null)
then
 exec echo "CRT1 is connected!" &
else
 exec echo "CRT1 isn't connected!"  &
 exit
fi
just call me...
~FSM~

Re: [Solved]Detect if monitors are connected?

hi FiniteStateMachine,

thanks for the help, works now like a charm. i love linux for being able to do this in such an easy way (and for you people helping smile )

#!/bin/bash
# if connected switches on external screen and switches off laptop screen

if (xrandr | grep "VGA1 connected" > /dev/null)
then
    exec xrandr --output LVDS1 --off --output VGA1 --mode 1920x1080 --rate 60.0 &
    exec sleep 1s &
    exec openbox --restart &
    exec conkywonky &
    exit
else
    exit
fi

Re: [Solved]Detect if monitors are connected?

nice janek!

just call me...
~FSM~