Autorestart With Upstart

What is upstart?
From the official description:


 * Upstart is an event-based replacement for the /sbin/init daemon which handles starting of tasks and services during boot, stopping them during shutdown and supervising them while the system is running.


 * It was originally developed for the Ubuntu distribution, but is intended to be suitable for deployment in all Linux distributions as a replacement for the venerable System-V init.

Monitoring/restarting OpenSimulator with upstart
The following assumes you're running OpenSimulator within GNU screen, so one can detach and reattach to the console. But that's not required to use upstart. It also assumes Ubuntu 12.04 LTS (or derivatives, like Linux Mint 13).

Create a file, /etc/init/opensim.conf, containing the following:

start on runlevel [!06] stop on runlevel [06] post-stop script OSPID=`ps -ef | grep opensim | grep 'mono OpenSim.exe' | awk '{ print $2 }'` kill $OSPID end script exec /bin/su - opensim -c "/usr/bin/screen -h 0 -D -m -U -S OpenSim -c /var/run/opensim/opensim.screenrc" respawn respawn limit 3 60
 * 1) give up if I respawn 3 times in 60 seconds...

If you are running MySQL, you can cause OpenSim to start after MySQL starts successfully by replacing the first two lines with:

start on started mysql stop on stopping mysql

Create a file, /var/run/opensim/opensim.screenrc, containing the following:

multiuser on acladd aclchg +rwx "#?" chdir /usr/lib/opensim/bin screen -h 500 -t opensim mono OpenSim.exe -console=basic

where is a comma-separated list of users you want to give control of the OpenSimulator console to. This is needed if you run OpenSim as a service user account you never actually log in to. Note that that account must be created with a real shell.

Then, to start or stop OpenSimulator, just use:


 * 1) start opensim

or:


 * 1) stop opensim

If OpenSimulator dies on its own, upstart will automatically restart it unless it dies three times within 60 seconds.

To connect to OpenSimulator with screen, use:


 * 1) screen -r opensim/

(or use sudo, if desired). Note that this will only work from the Unix userids you've specified in above.