Restart web app after unhandled exception

I’m running stand-alone web apps on OS X, and I have a logging scheme which logs App.Unhandled Exception, so I can tell why it died. But I’m curious if there’s an elegant way to relaunch the web app when this happens?

I currently have a bash shell script which launches my app (after killing any existing instances) which looks like this:

I suppose I could just have a command in UnhandledException which opens a Shell and executes this script again?

#!/bin/bash
##################################################
# Script to launch a Xojo webapp
##################################################
# note:
# uses command-line parameters for additions options
# see  http://www.xojo.com/blog/en/2014/01/web-standalone-ssl.php
##################################################

# kill any running apps

echo '##################################################'
echo '# Killing running apps (if any)'
echo '##################################################'
killall MyApp

echo '# Launching apps'
echo '##################################################'

# launch uploader for class(es) being taught.

cd "/path/to/my/webapp"

./MyApp --logging --secureport=8402  &  # launch in background using SSL on port 8402

If you launch your app with launchd (OS X init system) then you can specify a “keepalive” key in the config and then the system will relaunch your app automatically if it crashes/quits. Just an option to consider since it is already built-in.

Info on launchd

Launch Control - a launchd config utility

I use launchd on OS X and it’s worked great for me. I can’t remember the last time I had to touch a stand alone web app that was running from launchd. Usually I setup haproxy under launchd as well and at least two instances of my web app behind that. Works like a charm and spins like a top. :slight_smile:

Thanks! Some nice documentation / tutorial here: http://www.launchd.info

or https://developer.apple.com/library/content/documentation/MacOSX/Conceptual/BPSystemStartup/Chapters/CreatingLaunchdJobs.html

Thanks for all the help. Here’s a new script:

When run,

  • it stops any existing jobs and unloads them
  • it makes a new launchctl plist
  • it loads the plist and starts the job

[code]#!/bin/bash
##################################################

Script to launch a Xojo WebApp using LaunchCtl

and keep it alive

##################################################

note:

uses command-line parameters for webapp additions options

see http://www.xojo.com/blog/en/2014/01/web-standalone-ssl.php

For launchctl documentation see

http://www.launchd.info

##################################################

##################################################

Settings:

label = unique app id in reverse DNS format

exe = /path/to/your/exe

Note: don’t escape spaces!

arg1 = argument1 (or leave empty)

arg2 = argument2 (or leave empty)

##################################################

label=com.example.app
exe="/path/to/my/webapp"
arg1="–logging"
arg2="–secureport=443"
plist="/Users/username/Library/LaunchAgents/$label.plist"
log="/path/to/your/logfile.log"

echo “##################################################”
echo "# Stopping job (if any) "
echo “# $label”
echo “##################################################”
launchctl stop $label

echo “##################################################”
echo “# Unloading job (if any)”
echo “# $plist”
echo “##################################################”
launchctl unload $plist

echo “##################################################”
echo “# Making Launchctl plist file as”
echo “# $plist”
echo “##################################################”
cat < $plist

<?xml version="1.0" encoding="UTF-8"?> Label $label ProgramArguments $exe $arg1 $arg2 RunAtLoad KeepAlive StandardErrorPath $log StandardOutPath $log EOF

echo “##################################################”
echo “# Launching job using launchctl”
echo “##################################################”

launchctl load $plist

echo ‘# Process running?’
launchctl list | grep $label
echo ‘# Status’
launchctl list $label

echo ‘##################################################’
echo “WebApp is running.”
echo “To terminate, run the command "
echo " launchctl stop $label”
echo “To remove from launchctl, run the command "
echo " launchctl unload $plist”
echo ‘##################################################’
[/code]