O'Reilly Hacks
oreilly.comO'Reilly NetworkSafari BookshelfConferences Sign In/My Account | View Cart   
Book List Learning Lab PDFs O'Reilly Gear Newsletters Press Room Jobs  

Have open-x11 open X11
The open-x11 script will tell an X11 application the correct parameters to launch, but it doesn't launch X11.app itself. Let's fix that.

Contributed by:
Jason Deraleau
[10/06/03 | Discuss (0) | Link to this hack]

Note: Mac OS X 10.3's open-x11 does not require this modification. It already launches the Apple X11 package.

The Apple X11 application opens up a world of graphical Unix tools to Mac OS X users. After downloading and installing the X11 release from Apple's site (as well as the corresponding SDK), I went to work using some of my favorite X11 apps, like xchat and pan. My only gripe was that the applications didn't quite open the way I'd expected them to.

If you attempt to run an X11 app from within a Terminal, you'll often get an error about an unknown display. This is because your Terminal session isn't aware of your X11 session. To fix this, Apple provides a script in /usr/bin called open-x11. This script sets the DISPLAY environment variable and then launches your chosen app. For example, open-x11 xeyes will open the xeyes application in your X11 session.

This is all fine and dandy, but what if I forget to launch X11 before I run the open-x11 script? I still get those same old errors. To fix this, I added a little bit to the open-x11 script. You can find my version of the script below, but first a little explanation. I use the lsof and grep commands to check if there are any files or TCP ports open by X11.app. If there aren't any, then I use the open command to launch X11.app. The rest of the script is the same as the original, so it will set the necessary variables and open your Unix app.


# Make sure X11 is running first
lsof | grep X11 > /dev/null
if [ "$?" == "1" ]; then
        /usr/bin/open "/Applications/X11.app"

# Script to run a program with the X path and display configured correctly

if [ "x$DISPLAY" = "x" ]; then
  export DISPLAY

for d in /usr/X11R6/bin /usr/bin/X11 /usr/local/bin/X11; do
  if [ -d $d ]; then
    case $PATH in
        : do nothing
export PATH

# redirect stdio?
prog=$1; shift
"$prog" "$@" &

See also: man lsof, man grep

O'Reilly Home | Privacy Policy

© 2007 O'Reilly Media, Inc.
Website: | Customer Service: | Book issues:

All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.