Find out if X11 is running from within perl OR bash script
I am writing a script based image manipulator but i need to know if X is running so i can tell if i use CACAVIEW to imagemagick DISPLAY command.
|
You could just look at the `ps` output:
shell# ps -C X to see if X is running. Will that work? |
That was really really fast reply: 2min!
|
Heh, woulda been MUCH faster had I not been in the midst of debugging a weird window-manager bug which initially prevented me from getting to this thread!
Cheers, Sasha |
Like
Code:
if [ `ps | grep X` == "" ] |
I would use this as an example of how to do what you want:
Code:
[ "$(ps --no-headers -C X)" ] && echo "Yes, we have X" || echo "Boo!! No X." |
Quote:
Here is my final code: Code:
[ "$(ps | grep X)" ] && display _temp.bmp || cacaview _temp.bmp |
Your final code may fail, if there's any other single instance of the character "X" in the output of `ps`.
That's why I use the -C option. EDIT: And how did `grep` get in there? :scratch: |
Quote:
|
Generally speaking, it's not good to rely on certain process name. I would rather try to run a program which requires X and check whether it fails or not because of lack of X.
... Typically under X DISPLAY environment variable is set. It is set even when, say, X tunneling 'ssh' connection is used. |
How about this:
Note that it will use cacaview when you are in another VT, even when X is running, which is what you want! Code:
display <image file> |
You need X to be running on the host pointed to by $DISPLAY. If I telnet to some arbitrary host, check using ps whether it has an X server running, and then try to launch imagemagick, I will invariably simply fail to accomplish what I need (assuming I want to actually see the X rendering, not always the case).
Really, there is no sure-fire way to accomplish what you want, and in the absence of X, most X applications will simply write a brief message to stderr, and set their exit code to non-zero (imagemagick does on my system right now). Since you must have planned to handle the possibility of not having an X server anyway, why not just let the application fail to launch and go on with whatever corrective action you were planning? --- rod. |
This is what i have so far.
Code:
convert _temp.bmp $filename |
Something like this wil work better:
"if [ -n $DISPLAY ]" Simply looking to see if X is running may not work -what if the user running the script is ot the one running X? |
@gnashley
This is better: checkdisplay() { if [ ! -s $DISPLAY ]; then echo "X"; else echo 'CLI'; fi; } |
All times are GMT -5. The time now is 11:56 PM. |