screen & X11

One of the most powerful command line tools under Unix is screen. This tool lets you run a terminal emulator from within a terminal emulator. This is way more useful as it sound, it basically means you can have command-line sessions that are persistent between connections. So if you have a server, you can let the session run, even while you disconnect from the server. You can also have multiple logical terminals running in the same window, a shared information bar and of course, and abstruse configuration format. Here is the configuration I have, largely inspired by the one of a colleague. It sets control-Y to trigger screen commands, displays a bar at the bottom, with the name of the machine, the various screen sessions, the dimension of the terminal and the current time.

escape ^Yy
startup_message off
defutf8 on
info
vbell off
wrap off
hardstatus alwayslastline
hardstatus string '%{= kG}[ %{G}%H %{g}][%= %{= kw}%?%-Lw%?%{r}(%{W}%n*%f%t%?(%u)%?%{r})%{w}%?%+Lw%?%?%= %{g}][%{B} %d/%m%{W}%c %{g}]'
#!/bin/bash
set +e
for i in {10..15}
  do
  export DISPLAY="localhost:${i}.0"
  xdpyinfo -display $DISPLAY > /dev/null 2> /dev/null
  if [ $? -eq 0 ] ; then break; fi
done
message="DISPLAY: $DISPLAY"
xkbbell -display $DISPLAY "$message"

One issue with string is the definition of the X11 DISPLAY variable. As per Unix rules, this is inherited from the shell from which screen was started, so if you connect using ssh -Y it will be correct for the first ssh connection. The next time you connect, it probably will point to a non existing port. To solve this issue, I wrote a small shell script that scans for the right port, sets DISPLAY accordingly and sends a notification about it using xkbbell (as you want to inherit the DISPLAY variable, you should source this script.

#!/bin/bash
original=`basename $0`
command=${original:1}
$command "$@"
if [ $? -eq 0 ]; then
  xkbbell -v -100 -display $DISPLAY "$command $1: success"
else
  xkbbell -v 100 -display $DISPLAY "$command $1: failure"
fi

Finally I wrote a wrapper script that runs a command and then displays a status message using xkbbell once the command has finished running. The notification gets routed using xkbgrowl and displayed by Growl on my Mac OS X laptop.
The following script basically tries to run a command with the same name as itself with the first character stripped. So if you want to create a version of make that displays a notification once finished, you can write a script called xmake with the following code. For other commands, you just need to create a symbolic link (or even a hard one for the matter).

Leave a Reply