LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Find out if X11 is running from within perl OR bash script (https://www.linuxquestions.org/questions/programming-9/find-out-if-x11-is-running-from-within-perl-or-bash-script-796085/)

GazL 06-28-2010 06:12 PM

DELETED:
Just noticed this was a resurrected. I hate it when people do that!

oshazard 06-29-2010 10:26 AM

@GazL

You might hate that but I hate when I come to a thread via Google search results and find the resolved answer doesn't quite do it.

gnashley 06-29-2010 01:19 PM

oshahazard, your answer is wrong because the '-s' test checks for the existence and non-zero-byte status of a file. DISPLAY is an environmental variable which is set when X is started. Checking if DISPLAY is a non-null string (-n) is the proper check.

jpolcari 09-09-2011 01:18 PM

Surprised nobody got this yet.
Here's how I do it:

/usr/bin/xterm -e exit > /dev/null 2>&1
[ "$?" != "0" ] && exit

jpolcari 09-09-2011 01:21 PM

I should explain - your DISPLAY variable may set, but pointing nowhere.
If you login using putty for example, your DISPLAY gets set to something like localhost:10.0
but if you're not running a server where you came from, the DISPLAY is pointing to a non-existent server so
that's why I test it anyway.

theNbomr 09-10-2011 09:21 AM

Quote:

Originally Posted by jpolcari (Post 4467545)
Surprised nobody got this yet.
Here's how I do it:

/usr/bin/xterm -e exit > /dev/null 2>&1
[ "$?" != "0" ] && exit

Sergei Steshenko did suggest that back in article #10. He just didn't specify a particular program. Really, I don't see how running some other program with the possibility of it's failure is any different from running the program of principal interest, and having it possibly fail.

The only real test is to check whether there is a $DISPLAY variable set to a sane value, and then try to make an X connection to the indicated server. The X client application does that by default. One could probably contrive a special purpose application that does only that, and sets return values indicating success or its particular failure mode.
The problem with using the process table to check for a running X server is of course that the X server may not be running on the same host that the application will run on, and there is the possibility the any local X server is not named 'X', and that the X server may not be accepting connections from application(s) on the local host. There is also the possibility that the running X server does not support some other required functionality, such as video modes, fonts, etc. None of these conditions are easy to detect without simply trying to make the connection and use the server.

--- rod.

ta0kira 09-10-2011 05:22 PM

Quote:

Originally Posted by theNbomr (Post 4468165)
Really, I don't see how running some other program with the possibility of it's failure is any different from running the program of principal interest, and having it possibly fail.

I think that's the best possible solution, if it follows a check for the readability of the file. There are too many possible problems with trying to intuit whether or not the X program will work; it's better just to test it with a set of options that are known to be good. Elaborating on MTK358's solution:
Code:

show_image()
{
    [ -r "$1" ] || return 1
    file "$1" | grep -q 'image data' || return 1 #(or whatever format checks are needed)
    display options "$1" || cacaview other-options "$1"
}

show_image my.bmp

Kevin Barry


All times are GMT -5. The time now is 12:03 PM.