how to have bash script check whether it's a x session?
Hi folks :)
This is going to be an easy one for somebody who knows... ;) how can I check in a bash script whether it's a x session or not? I'm doing a xmodmap in my ~/.profile, however, when it's a text only login, there's an error message which I'd like to avoid... a bit aesthetics here... *g* I guess a Code:
if [-a something] And, where else but here could I have found out this? Thank you already in advance! Cheers, bkeeper :) |
test $XAUTHORITY || echo "not run from X" && exit
|
Hmm, whilst I don't have anything constructive to add to this, I don't have the XAUTHORITY envvar set, and running your commands (without the exit on the end) always tell me I'm not in X, even when I am.
At a guess, since everything's a file in Linux, presumably starting X will create a screen file somewhere (localhost:0), and you could check for that? Edit: OK, I just searched for .*:0.* and got ~/.DCOPserver_$HOSTNAME_:0, but that's KDE-centric. Not so good, though it might form part of a test if it's quicker than the definitive method - assuming that the file is destroyed with X, which I haven't checked. Another approach is processes - if X is running, you can see it with ps aux. However, that brings its own problems (why couldn't it have a longer and more unique name? :)) - my output is Code:
root 5133 0.4 7.8 91424 71568 ? R Feb01 49:25 /usr/X11R6/bin/X -nolisten tcp :0 vt7 -auth /var/run/xauth/A:0-4ow2QF HTH, Andrzej |
If you are getting an error that you don't want to see, can't you just redirect that error message to /dev/null?
Code:
xmodmap whatever 2> /dev/null The problem with that, of course, is that if something goes wrong, you get no output about it. It should clear up your problem, but might make it harder to track down a problem in the future. |
Hi folks,
thanks for the replies. Indeed, $XAUTHORITY is not set on my system. I guess Andrzej way of using ps, grepping for X, is the one to go to do it properly. :) [ ...However, I'll settle with Hobbletoe's version for the moment... ;) ] Thanks again! Cheers, bkeeper :) |
checking ps of X will not help anyway, because somebody could ssh in to run the sc ript, or just use tty1 while X is running...
|
Have you tried testing for $DISPLAY?
That value should always be set for an X session and never for a session not under X -- unless you manually set it in your login scripts (I have done this when I was routinely logging in and starting X programs I wanted to see on my local display). |
Wow! :eek:
So it's as simple as Code:
if [ $DISPLAY ]; then Cheers, bkeeper :) |
Yep... that should work without a problem.
Just one question... why are you doing this in ~/.profile and not ~/.xinitrc and ~/.xsession ? Those are the two files which "traditionally" call programs which modify X. Just put a line like: Code:
xmodmap ~/.xmodmap & Of course, you're at liberty to do this however you want. I just didn't realize what you were doing at first; so, I answered the question without thinking about the actual problem you were trying to solve. :) |
Hi frob,
yes, you're right. However, for some weird reason, it's not executed if I put it in one of the ~/.x files. Actually, it seems these files are not executed on my system at all. But as it's a business machine which I don't administrate, at the end of the day, I don't really care as long as it works - which it does now, thanks again! :) Cheers, bkeeper :) |
All times are GMT -5. The time now is 08:39 AM. |