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

A script to keep VNC running UNIX
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...]
    •    
  • Currently 2.17 / 5
  You rated: 1 / 5 (6 votes cast)
 
[12,169 views]  

A script to keep VNC running | 16 comments | Create New Account
Click here to return to the 'A script to keep VNC running' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
A script to keep VNC running
Authored by: Gonzonia on Mar 05, '03 10:14:36AM

The Redstone Software version has been as solid as a rock for me. I have it set to open at startup and it works great. The only crash I ever get is from the viewer occasionaly when I try ot copy something to the clipboard.



[ Reply to This | # ]
A script to keep VNC running
Authored by: sven on Mar 05, '03 10:33:44AM

Just using "ps -auxc" instead of "ps -auxww" will allow you to drop the "grep -v" part:

if ps -auxc | grep OSXvnc; then
...

Out of curiosity: Why did you choose these options?



[ Reply to This | # ]
A script to keep VNC running
Authored by: Accura on Mar 05, '03 10:21:28PM

I work with unix (well BSD) geeks and have had these options forced on me by others, its now just an auto thing i do with ps, the best way to work out what you need is man ps. auxww seams to do everything by every user, also ps -aSx works as well.

---
"The time has come," the walrus said. "To talk of many things..."



[ Reply to This | # ]
ww and c
Authored by: grinnelljosh on Nov 13, '03 07:13:57PM

The original code prevents two bugs.
1. the 'ww' flag tells ps to not crop the textual output, which could screw up output (this has happened to me)
2. although the 'c' flag seems to do the job, it doesn't account for multiple processor machines, where two processes (grep and ssh, for instance) could be running at the same time



[ Reply to This | # ]
VNC from Fink
Authored by: inmanjm on Mar 05, '03 12:29:21PM

Fink has had vnc available for a while. The following is from the package description, showing which version is available in the various trees:

In current-unstable: Version 3.3.6-3
In current-stable: not present
In 0.5.1-stable: not present
In 0.4.1-stable: Version 3.3.3r2-5 (10.1 only)

I'm not brave enough to try the current unstable yet, does anyone else have any idea how good it is?



[ Reply to This | # ]
VNC from Fink
Authored by: SOX on Mar 05, '03 12:36:08PM

the VNC in fink are for X11 not quartz.



[ Reply to This | # ]
VNC from Fink
Authored by: steventamm on Mar 15, '03 04:47:31PM

The "unstable" version of vnc is suitable for use (I do it daily). But then again, I'm also the maintainer, so YMMV



[ Reply to This | # ]
A script to keep VNC running
Authored by: diamondsw on Mar 05, '03 12:40:05PM

Of course, if you download the VNC server from Redstone, then it comes with scripts and such to run it as a StartupItem, and relaunch it automatically. As an aside, OSXvnc doesn't crash often that I can tell, but the gotcha is when you logout, OS X quits the WindowServer and THAT kills VNC.

A couple of starter files for getting a VNC StartupItem up and running, <b>and</b> keep everything self-contained in the StartupItem (no hard-coded paths or other ickiness). The completed startup item will contain (assuming /Library/StartupItems/OSXvnc/ is the location):

<code>
-rwxr-xr-x 1 jochs admin 8 Feb 3 09:51 .osxvncauth*
-rwxr-xr-x 1 jochs admin 742 Feb 18 15:24 OSXvnc*
-rwxr-xr-x 1 jochs admin 312 Feb 17 21:09 OSXvnc-keepalive*
-rwxr-xr-x 1 jochs admin 243888 Feb 3 09:51 OSXvnc-server*
-rw-r--r-- 1 jochs admin 553 Feb 3 11:46 StartupParameters.plist
-rw-r--r-- 1 root admin 116 Mar 5 01:28 osxvnc.log
</code>

<b>.osxvncauth</b> - encrypted password file created by "storepassword"
<b>OSXvnc</b> - shell script that is run automatically when OS X loads the startupitem
<b>OSXvnc-keepalive</b> - a shell script to always check for VNC and relaunch it if necessary
<b>OSXvnc-server</b> - the actual VNC server
<b>StartupParameters.plist</b> - Simple required file describing the StartupItem
<b>osxvnc.log</b> - any output and log messages from the VNC server

Now for the interesting files:

StartupParameters.plist:
<code>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist SYSTEM "file://localhost/System/Library/DTDs/PropertyList.dtd">
<plist version="0.9">
<dict>
<key>Description</key>
<string>VNC Server</string>
<key>Messages</key>
<dict>
<key>start</key>
<string>Starting VNC Server</string>
<key>stop</key>
<string>Stopping VNC Server</string>
</dict>
<key>OrderPreference</key>
<string>None</string>
<key>Provides</key>
<array>
<string>VNC</string>
</array>
<key>Requires</key>
<array>
<string>Resolver</string>
</array>
</dict>
</plist>
</code>

OSXvnc:
<code>
#!/bin/sh

##
# VNC Server
##

. /etc/rc.common

StartService ()
{
SERVICE_PATH=`dirname $0`

CheckForNetwork
if [ "${NETWORKUP}" = "-NO-" ]; then exit; fi
ConsoleMessage "Starting VNC Server"

$SERVICE_PATH/OSXvnc-keepalive &
exit 0
}

StopService ()
{
# Stop the wrapper process first
SERVICE_PID=`/bin/ps -auxwwww | grep OSXvnc-keepalive | grep -v grep | awk 'NF > 2 {print \$2}'`
if [ "${SERVICE_PID:=""}" ]; then kill $SERVICE_PID; fi

# Stop the VNC server itself
SERVICE_PID=`/bin/ps -auxwwww | grep OSXvnc-server | grep -v grep | awk 'NF > 2 {print \$2}'`
if [ "${SERVICE_PID:=""}" ]; then kill $SERVICE_PID; fi
}

RestartService () { StopService; StartService; }

RunService "$1"
</code>

OSXvnc-keepalive:
<code>
#!/bin/sh

VNCPATH=`dirname $0`
VNCARGS="-rfbport 5900 -rfbauth ${VNCPATH}/.osxvncauth -swapButtons -dontdisconnect -disableScreenSaver
-allowsleep -localhost"

while true
do
$VNCPATH/OSXvnc-server $VNCARGS > $VNCPATH/osxvnc.log 2>&1
RESULT=$?
sleep 5
done

echo "OSXVNC Shutdown with exit status" $RESULT
</code>



[ Reply to This | # ]
A script to keep VNC running
Authored by: heissenbuttel on Mar 24, '03 02:52:54PM

Anyone else seem to have problems with the saved password, or is it just me? Seems I have to manually open the OSXvnc.app, enter my password, and restart server before I can login using Chicken of the VNC.

Anyone have any fixes for this?



[ Reply to This | # ]
A script to keep VNC running
Authored by: smorr on Mar 05, '03 12:47:23PM

wouldn't this crontab entry run at five minutes past the hour instead of every five minutes?

I am just finding out about setting cronentries myself but thought that /5 would mean every five minutes.

so the entry should be
/5 * * * * jamesso /Users.....



[ Reply to This | # ]
No, it should be */5
Authored by: David on Mar 05, '03 01:34:43PM

It should be */5, meaning that you want it to run every 5 minutes. So */
10 would be every ten minutes, */15, */20, etc.

David

---
www.AcornWebDesigns.com
Quality websites for inexpensive prices (Is that an oxymoron?)



[ Reply to This | # ]
I do nothing of the sort
Authored by: Cerberus on Mar 05, '03 04:12:22PM

I asm sure this is off topic but I use OSXVnc .6 with the update to .7 and I have *NEVER* had trouble with it.

As a matter of fact, if i attempt to kill OSXVNc (from ps or form FORCE QUIT or just Quit) OSXVnc it will autorestart itself and without a cron job. And i have moved from 10.1 to 10.1.5 and now I have made this OS X Server 10.1.4. I have all the pieces of the .6 installer.



[ Reply to This | # ]
I do nothing of the sort
Authored by: diamondsw on Mar 05, '03 06:45:48PM

That's because OSXvnc 0.5 and 0.6 came with a script to autorestart VNC.

I'd whip up an installer package for it, but since I don't have any rights to VNC it probably wouldn't be appreciated. :)



[ Reply to This | # ]
I do nothing of the sort
Authored by: Accura on Mar 05, '03 10:25:19PM

im running this on a gs box, its only and the hardware is a bit dodge, extra non-standard ram and a few other add on's, i find it better to just test and run things (i also have a script for DC, which my brother (Windows user) insists on using, it also crashes. Does any one know i VNC like network remote that i can use instead of VNC

---
"The time has come," the walrus said. "To talk of many things..."



[ Reply to This | # ]
A script to keep VNC running
Authored by: macubergeek on Mar 06, '03 09:02:15AM

ok first running bind AND apache on the same box is a very bad idea.
Both of these are vulnerable services...running them on the same box is
asking for trouble.

Consider using tightvnc. This allows you to create a vnc connection with
the remote box piped over an encrypted ssh connection. You will need to
install tightvnc on the local and remote machines.

1. ssh to the remote box and start the vnc server. No don't keep it
running all the time. Use it when you want it and then shut it down.
start it at cli with: vncserver
note the display number it gives you something like :0 or :1

--lets say the remote machine is at ip address 63.x.x.x

2. then from your local machine at cli, do:
vncviewer -via 63.x.x.x 63.x.x.x:0

you will then connect and the entire session will go over ssh and be
encrypted.



[ Reply to This | # ]
A script to keep VNC running
Authored by: Accura on Mar 06, '03 09:51:11PM

I know about apache and bind. i know there dodge. im about to start running sendmail as well. people are welcome ta atack my box, there is nothing on there that is worth stealing, everything is backed up. this is a test box, i also dont have to type in sudo to get root access. Its a dodge box, all ways will be

---
"The time has come," the walrus said. "To talk of many things..."



[ Reply to This | # ]