A script to keep VNC running

Mar 05, '03 08:58:00AM

Contributed by: Accura

I just got my old Beige G3 running as a server (bind, web and so on) and I can control most things quite happily with the CLI and with xWindows. Sometimes, though, there is something I can only do with Aqua. I have used VNC in the past to remotely control a display (handy if you work at an internet cafe and need to do something to one of the computers), but have found VNC buggy at the best of times on OSX (and most other OSes as well, is there any thing else?). I got bored of sshing into the box and running "open /Path/To/VNC/Server" if it crashed, so I wrote a shell script to check if VNC is running and if not, to start it. Here it is:

#!/bin/sh
if ps -auxww | grep \/OSXvnc.app\/ | grep -v grep ; then
  exit 0
else
  open /Applications/OSXvnc.app
  exit 0
fi
Save this some where in your path like ~/bin and make it executable with chmod +x checkvnc

All it does is do a ps (of all running commands hence the -auxww) and if it finds the string "/OSXvnc.app/" (I've included the /s so if there is another app called 'somethingOSXvnc.app,' it will still work. I've also included the "grep -v grep" command so that it removes any lines with "grep" included in them. Then it will print the ps line to the STDOUT (Standard out). If it doesn't have any output, then it will open OSXvnc.app using the command "open". Then I just added this line to /etc/crontab:
5  *  *  *  *  jameso  /Users/jameso/bin/checkvnc > /dev/null
Those spaces are tabs, do a search on cron or crontab. This line just runs the command every five minutes, and sends all output to /dev/null, which is the unix "long drop." Basically, if it goes into /dev/null, it never comes out.

If you want to edit this script for other apps, remember that you need to use \ for any special characters.

[robg adds: There is no OS X version of VNC from realvnc.com (yet; you can send feedback and ask for one!), but you can find a few on VersionTracker by searching on VNC; Redstone Software has the newest version I've seen...]

Comments (16)


Mac OS X Hints
http://hints.macworld.com/article.php?story=200303030447096