LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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

Reply
 
Search this Thread
Old 03-17-2010, 01:58 PM   #1
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,229

Rep: Reputation: 173Reputation: 173
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.
 
Old 03-17-2010, 02:00 PM   #2
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
You could just look at the `ps` output:

shell# ps -C X

to see if X is running.

Will that work?
 
Old 03-17-2010, 02:03 PM   #3
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,229

Original Poster
Rep: Reputation: 173Reputation: 173
That was really really fast reply: 2min!
 
Old 03-17-2010, 02:08 PM   #4
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
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
 
Old 03-17-2010, 02:08 PM   #5
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,229

Original Poster
Rep: Reputation: 173Reputation: 173
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.
 
Old 03-17-2010, 02:12 PM   #6
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
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."
 
Old 03-17-2010, 02:21 PM   #7
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,229

Original Poster
Rep: Reputation: 173Reputation: 173
Quote:
Originally Posted by GrapefruiTgirl View Post
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
 
Old 03-17-2010, 02:23 PM   #8
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
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.
 
Old 03-17-2010, 02:33 PM   #9
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,229

Original Poster
Rep: Reputation: 173Reputation: 173
Quote:
Originally Posted by GrapefruiTgirl View Post
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.
 
Old 03-17-2010, 03:58 PM   #10
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
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.
Old 03-17-2010, 04:05 PM   #11
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
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.
 
Old 03-17-2010, 05:35 PM   #12
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,395
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
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.
 
Old 03-17-2010, 10:23 PM   #13
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,229

Original Poster
Rep: Reputation: 173Reputation: 173
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
 
Old 03-18-2010, 02:28 AM   #14
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,755

Rep: Reputation: 466Reputation: 466Reputation: 466Reputation: 466Reputation: 466
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.
Old 06-28-2010, 05:54 PM   #15
oshazard
LQ Newbie
 
Registered: Jun 2010
Posts: 2

Rep: Reputation: 0
@gnashley

This is better:
checkdisplay() { if [ ! -s $DISPLAY ]; then echo "X"; else echo 'CLI'; fi; }
 
0 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
passing variable from bash to perl in a bash script quadmore Programming 6 02-21-2011 04:11 AM
Running a BASH Script within PERL pm atrac Programming 4 05-10-2008 12:09 AM
To rename files in a directory should I use Bash script or a Perl Script ? jamtech Programming 7 01-22-2008 11:25 PM
why this script is running in bash but not in perl ratul_11 Linux - General 2 08-17-2007 09:31 AM
perl/bash script to monitor all processes running in my machine pudhiyavan Linux - Security 4 07-19-2005 02:09 PM


All times are GMT -5. The time now is 09:50 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration