Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
|
03-17-2010, 01:58 PM
|
#1
|
|
Senior Member
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339
|
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.
|
|
|
|
03-17-2010, 02:00 PM
|
#2
|
|
LQ Guru
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594
|
You could just look at the `ps` output:
shell# ps -C X
to see if X is running.
Will that work?
|
|
|
|
03-17-2010, 02:03 PM
|
#3
|
|
Senior Member
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339
Original Poster
|
That was really really fast reply: 2min!
|
|
|
|
03-17-2010, 02:08 PM
|
#4
|
|
LQ Guru
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594
|
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
|
|
|
|
03-17-2010, 02:08 PM
|
#5
|
|
Senior Member
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339
Original Poster
|
Like
Code:
if [ `ps | grep X` == "" ]
then
cacaview _temp.bmp
else
display _temp.bmp
fi
OH great...it did not work
Last edited by smeezekitty; 03-17-2010 at 02:13 PM.
|
|
|
|
03-17-2010, 02:12 PM
|
#6
|
|
LQ Guru
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594
|
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."
|
|
|
|
03-17-2010, 02:21 PM
|
#7
|
|
Senior Member
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339
Original Poster
|
Quote:
Originally Posted by GrapefruiTgirl
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
|
|
|
|
03-17-2010, 02:23 PM
|
#8
|
|
LQ Guru
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594
|
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? 
Last edited by GrapefruiTgirl; 03-17-2010 at 02:31 PM.
|
|
|
|
03-17-2010, 02:33 PM
|
#9
|
|
Senior Member
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339
Original Poster
|
Quote:
Originally Posted by GrapefruiTgirl
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? 
|
ps spit out invalid argument errors.
|
|
|
|
03-17-2010, 03:58 PM
|
#10
|
|
Senior Member
Registered: May 2005
Posts: 4,481
|
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.
|
|
|
1 members found this post helpful.
|
03-17-2010, 04:05 PM
|
#11
|
|
LQ 5k Club
Registered: Sep 2009
Posts: 6,443
|
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
Last edited by MTK358; 03-17-2010 at 04:07 PM.
|
|
|
|
03-17-2010, 05:35 PM
|
#12
|
|
LQ 5k Club
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
|
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.
|
|
|
|
03-17-2010, 10:23 PM
|
#13
|
|
Senior Member
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339
Original Poster
|
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
|
|
|
|
03-18-2010, 02:28 AM
|
#14
|
|
Amigo developer
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928
|
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?
|
|
|
0 members found this post helpful.
|
06-28-2010, 05:54 PM
|
#15
|
|
LQ Newbie
Registered: Jun 2010
Posts: 2
Rep:
|
@gnashley
This is better:
checkdisplay() { if [ ! -s $DISPLAY ]; then echo "X"; else echo 'CLI'; fi; }
|
|
|
0 members found this post helpful.
|
All times are GMT -5. The time now is 06:32 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|