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/)

smeezekitty 03-17-2010 01:58 PM

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.

GrapefruiTgirl 03-17-2010 02:00 PM

You could just look at the `ps` output:

shell# ps -C X

to see if X is running.

Will that work?

smeezekitty 03-17-2010 02:03 PM

That was really really fast reply: 2min!

GrapefruiTgirl 03-17-2010 02:08 PM

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

smeezekitty 03-17-2010 02:08 PM

Like
Code:

if [ `ps | grep X` == "" ]
then
cacaview _temp.bmp
else
display _temp.bmp
fi

OH great...it did not work

GrapefruiTgirl 03-17-2010 02:12 PM

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."

smeezekitty 03-17-2010 02:21 PM

Quote:

Originally Posted by GrapefruiTgirl (Post 3902175)
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."

Thank you!
Here is my final code:
Code:

[ "$(ps | grep X)" ] && display _temp.bmp || cacaview _temp.bmp

GrapefruiTgirl 03-17-2010 02:23 PM

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:

smeezekitty 03-17-2010 02:33 PM

Quote:

Originally Posted by GrapefruiTgirl (Post 3902193)
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:

ps spit out invalid argument errors.

Sergei Steshenko 03-17-2010 03:58 PM

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.

MTK358 03-17-2010 04:05 PM

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>
if [ $? != "0" ]; then # If display failed for whatever reason
    cacaview <image file>
fi


theNbomr 03-17-2010 05:35 PM

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.

smeezekitty 03-17-2010 10:23 PM

This is what i have so far.
Code:

convert _temp.bmp $filename
}
function info {
identify $filename
}
function woc {
cp $filename $newname
filename=$newname
}
while [ 1 ]
do
echo -n ">"
read what
case $what in
load)
 echo Enter filename:
 read filename
 if [ -e "$filename" ]
 then
 echo File loaded
 else
 echo FILE NOT FOUND!
 fi
 ;;
view)
 view;;
resize)
 echo Enter the new size in the form of n1xn2 where n1 is the X size and n2 is the Y

size:
 read size
 resize;;
rotate)
 echo Enter the angle:
 read size
 rotate;;
colors)
 echo Enter amount of colors:
 read size
 colors;;
brightness)
 read size
 brightness;;
info)
 info;;
copy)
 echo Enter new name:
 read newname
 woc;;
esac
done
rm _temp.bmp
end


gnashley 03-18-2010 02:28 AM

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?

oshazard 06-28-2010 05:54 PM

@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.