Checking whether a script can open a display?-WORKAROUND

William O'Higgins william.ohiggins-H217xnMUJC0sA/PxXw9srA at public.gmane.org
Fri Mar 4 14:27:23 UTC 2005


On Tue, Mar 01, 2005 at 07:04:22PM -0500, John Vetterli wrote:
>On Tue, 1 Mar 2005, William O'Higgins wrote:
>>>On Tue, Mar 01, 2005 at 04:52:29PM -0500, William O'Higgins wrote:
>>>>>I use a cron job to update the backdrop (root window) of my window
>>>>>manager.  Sometimes I am not running X, however, and then my mail box
>>>>>fills up with cron telling me that it cannot open display :0.0.  Is
>>>>>there a way I can test whether I can open that display so I can quit
>>>>>cleanly?
>>>>>Any pointers would be appreciated.  Thanks.
>>>>if [ -z "$DISPLAY" ]; then
>>>>	echo "No display";
>>>>else
>>>>	echo "Do something";
>>>>fi
>>>Hmm...  Checking DISPLAY works if you're in Xterm shell session, say
>>>from ~/.profile.  But, I don't think Cron sees or care about DISPLAY
>>>environment variable.  In fact, Cron by design runs with minimum set of
>>>environment variables.

This is true.  Cron has an impoverished $ENVIRONMENT, and these
solutions don't work.

>>>Try 'ps -C X' or something.

This definitely works.  I was hoping for a "right" way, where the script
actually checks if it can open a $DISPLAY, but if it works, it works :-)

Here is the code snippet I ended up using:

#!/usr/bin/perl -w

# This little program sets my background image 
# randomly in blackbox (and now openbox).
# It's called when I startx.

$isxrunning = `ps -C startx | grep "startx"`;

if ($isxrunning =~ "startx") {
	changebackdrop();
} else {
	exit;
}

If anyone is interested in the code I'll post it somewhere, but it
certainly isn't tricky.

Thanks to all for the help and suggestions.
-- 

yours,

William

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
URL: <http://gtalug.org/pipermail/legacy/attachments/20050304/7fd54adc/attachment.sig>


More information about the Legacy mailing list