Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Use Terminal with X11, revisited UNIX
This hint evolved from two earlier hints: Stop automatic xterm in Apple X11 and Use other terminal apps with Apple's X11.

Like many others, I want to use X11 apps occasionally without needing to start X11 manually when that is needed. However, I also don't want to get the automatic xterm that Apple launches when X11 starts. Further, I don't want to modify /etc/X11/xinit/xinitrc directly (it is a system file!), but I also don't feel comfortable cloning this file into my home directory and making modifications there. If there are system updates, for instance, you won't see them otherwise. Finally, new terminals should detect when DISPLAY is not set, and have it point to the current user's X server. (Also in the presence of multiple users running X11.) In other words, it should just work.

One comment in the second hint referenced above suggested a way to start X11 from .profile, as long as X11 is not running yet. But that involves refocussing the window, which I find a bit clumsy. So I just start X11 the normal way, from System Preferences -> Accounts -> Login Items. I don't mind having it hanging around all the time -- X11 is very small when is has no windows running.

The trick is that my ~/.xinitrc surpresses the initial xterm, still runs the system's initrc, and records the display in ~/tmp/X11-display-hostname (for later lookup by starting terminals). Here it is:

.xinitrc
# surpress starting of an initial xterm
xterm()
{
    return 0
}

# remember the display name
echo "$DISPLAY" > $HOME/tmp/X11-display-`hostname -s`

# run system-defined xinitrc (doesn't return)
source /etc/X11/xinit/xinitrc
I keep temporary information in a private tmp directory. Create one with mkdir ~/tmp ; chmod 700 ~/tmp. Finally, .profile checks if it needs to connect to an X11. If DISPLAY is already set, it doesn't touch it (so X11 forwarding with ssh still works). It also tests if the X11 server is still alive. Here is the snippet in .profile:

.profile
PATH=/usr/X11R6/bin:$PATH

if [ ! $SSH_TTY ] && [ ! $DISPLAY ]
then
    DISPLAY=`cat $HOME/tmp/X11-display-`hostname -s` 2>/dev/null`
    if xwininfo -display "$DISPLAY" -root >/dev/null 2>&1
    then
        export DISPLAY
    else
        echo X11 display $DISPLAY not active
        unset DISPLAY
        rm -f $HOME/tmp/X11-display-`hostname -s`
    fi
fi
That's it! Now all my Terminals are automatically aware of the X11 server, and I have no annoying xterm window during startup.
    •    
  • Currently 1.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (1 vote cast)
 
[14,383 views]  

Use Terminal with X11, revisited | 14 comments | Create New Account
Click here to return to the 'Use Terminal with X11, revisited' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Use Terminal with X11, revisited
Authored by: wgscott on Sep 14, '05 10:52:49AM
I took a different approach, which is to get the DISPLAY from the process list. I've implemented setting everything up with a GUI assistant. It tweaks a few other things. It does however (optionally) clone ~/.xinitrc to get rid of the annoying xterm. All changes are reversible: It is a wrapper for a shell script that does the same thing. These have been tested extensively.

[ Reply to This | # ]
Use Terminal with X11, revisited
Authored by: ework on Sep 14, '05 01:07:24PM

This seems like way too much work. I prefer to simply put a couple lines in .bashrc to check for the existance of $DISPLAY and comment out the xterm line in the system xinitrc. I just start X11 manually as I only use it once in a while or it is started automatically by programs like GIMP. He does have some good points such as maintaining the original systems files, though it seems a little more error prone and uses more resources.



[ Reply to This | # ]
Waaay too much work
Authored by: pauljlucas on Sep 14, '05 02:21:29PM
To suppress automatic xterm creation, just create your own simple .xinitrc -- that's what it's for.

[ -r $HOME/.Xdefaults ] && xrdb $HOME/.Xdefaults
exec quartz-wm

---
- Paul

[ Reply to This | # ]

Waaay too much work
Authored by: silicontrip on Sep 15, '05 02:07:43AM

The original poster mentions that he did not like this method, due to the fact that if there are system updates that he might miss these chages due to the fact that he is using a copy in his home directory.

Here's a hint about /etc, it's the unix configuration directory. Software updates are not likely to touch this directory, unless a new service is installed.



[ Reply to This | # ]
Waaay too much work
Authored by: marcelk on Sep 15, '05 03:17:51AM

silicontrip, there can be more than one reason not wanting
to touch system files.
One is that unix is a multi user system, and it is not nice to
push one user's preference through the throat of all others.
Another reason is that not every user needs to have admin rights.



[ Reply to This | # ]
Waaay too much work
Authored by: ratthing on Oct 02, '05 08:07:57AM

Which is exactly why an .xinitrc file should be in your home directory.

I think the original author is being a bit disingenuous when he says he's "uncomfortable" editting an .xinitrc config file for his home dir and then posts up a more complicated shell script.

An .xinitrc file can consist of a single line that starts your window manager and nothing else.

=RT=



[ Reply to This | # ]
Use Terminal with X11, revisited
Authored by: speir on Sep 14, '05 04:40:10PM

Our lab uses a method similar to Bill's (wgscott) and we have borrowed from his script. Here is what we use in our individual tcsh environment setup files (i.e. place in your local .cshrc or .tcshrc file, NOT the system files). This has also been extensively tested. Takes care of essential X11 related startup and DISPLAY settings. Sorry for the extra bits, but wanted the snipet to have all the correct features (i.e. check for interactive session, check for remote login, etc.).

I agree with the others on getting rid of the xterm popup - just make your own xinitrc file. Should avoid modifying system files whenever possible.

--------------------------------

if ($?prompt) then

set CHECK_INTERACTIVE = "true"

### Report Unix and shell versions
set PLATFORM_UNIX_REPORT = `uname -a | awk '{print $17, $1, $3, $9"-"$10}'`
set SHELL_REPORT = `echo $version | awk '{ print $1, $2, $4}'`
set DARWIN_VERSION = `uname -a | awk '{print substr($3, 1, 1)}'`
set DARWIN_YEAR = `uname -a | awk '{print substr($13, 1, 4)}'`

### Check if Apple X11 open, launch if not
### For console login only, must skip remote sessions
### Have to check Darwin version for default location of X11.app
### sleep 0.25 = need to give system a little time to execute open command

if ( $?REMOTEHOST || $?SSH_TTY) then
goto x11_bypass
else
set X11DISP = `/bin/ps -xw -o command | grep Xauthority | grep $USER | awk '{print substr($5, 2, 1)}'`
if ( $X11DISP == "" ) then
if ( $DARWIN_VERSION >= 7 && -e /Applications/Utilities/X11.app ) then
set X11APP = "/Applications/Utilities/X11.app"
else if ( $DARWIN_VERSION < 7 && -e /Applications/X11.app ) then
set X11APP = "/Applications/X11.app"
else
echo "|== ***ERROR*** == Apple X11 not found in its default Darwin $DARWIN_VERSION location"
unset X11APP
endif
else
setenv DISPLAY :$X11DISP.0
endif
endif

if ( $?X11APP ) then
open $X11APP
sleep 0.25
set X11DISP = `/bin/ps -xw -o command | grep Xauthority | grep $USER | awk '{print substr($5, 2, 1)}'`
setenv DISPLAY :$X11DISP.0
unset X11APP
osascript -e 'tell application "Terminal"' -e 'activate' -e 'end tell'
endif

x11_bypass:

else
set CHECK_INTERACTIVE = "false"
endif



[ Reply to This | # ]
Use Terminal with X11, revisited
Authored by: Gobo on Sep 15, '05 04:23:35AM

I have this:

export DISPLAY=:0.0

in my .zshrc.

It may not be fancypancy, but works 99.99% of the time. (In fact, I wouldn't know any reason why your X11 would start at another address).

Only problem is that you might get errors if you SSH to other machines, complaining that there is no display attached if you have not started X11



[ Reply to This | # ]
Use Terminal with X11, revisited
Authored by: Anonymous on Sep 15, '05 05:52:45AM

You could copy and modify an xinitrc in your home directory and sense changes with "find /etc/X11/xinit/xinitrc -newer ~/.xinitrc". If so, "xmessage /etc/X11/xinit/xinitrc is newer than ~/.xinitrc".

Best of both worlds, none of the disadvantages.



[ Reply to This | # ]
Use Terminal with X11, revisited
Authored by: Egrefen on Sep 16, '05 06:50:10AM

More to the point... has anyone succeeded in removing X11 from the dock under OS X 10.4 ?



[ Reply to This | # ]
Use Terminal with X11, revisited
Authored by: Christoph on Sep 27, '05 11:57:21PM
You can change application properties by modifying Info.plist in the application bundle. For removing a process from the dock, try LSBackgroundOnly. See http://developer.apple.com/documentation/MacOSX/Conceptual/BPRuntimeConfig/Concepts/PListKeys.html for details.

[ Reply to This | # ]
simpler tcsh version
Authored by: jms1 on Sep 18, '05 04:33:02PM
I was already using the "xterm function" to keep the real xterm from running when X11 starts, but I had a hard-coded "setenv DISPLAY :0" in my .tcshrc. Reading the other replies here made me take another look at it- this is what I now have in my .tcshrc, it works like a charm.
if ( ! $?DISPLAY && ! $?SSH_TTY && ! $?REMOTEHOST ) then
        setenv DISPLAY `ps xw -o command | sed -n '/^\/.*X11.*\:/s/.*:/:/p'`
endif

if ( $?DISPLAY ) then
        xwininfo -display "$DISPLAY" -root >& /dev/null
        if ( $? ) then
                echo Unable to connect to X11 display \"$DISPLAY\"
                unsetenv DISPLAY
        else
                echo Using X11 display \"$DISPLAY\"
        endif 
endif


[ Reply to This | # ]
simpler tcsh version
Authored by: jms1 on Oct 01, '05 09:36:16AM
The "sed" pattern above is wrong... apparently the entry fields on this site interpret backslash "\" characters, and you have to enter them twice in order for the posted entry to contain them once. It should be...
'/^\/.*X11.*:/s/.*:/:/p'
Or... "forward slash, caret, backslash, forward slash, dot, star, X11..."

[ Reply to This | # ]
Use Terminal with X11, revisited
Authored by: Franklund on Sep 27, '05 08:04:38AM

I have made a slight modification to the systm file, /etc/X11/xinit/xinitrc, in order to automate this for all users on my laboratory computers. The relevant lines are shown below.

# =====> Commented out the xterm and replaced with a Terminal session
# xterm -ls &
/Applications/Utilities/Terminal.app/Contents/MacOS/Terminal &

Now whenever X11 launches, I get a terminal session.



[ Reply to This | # ]