Launching web apps on OS X using .command files

I have a web app which does short surveys. Given the nature of the data collected, I would like the failure mode to be for the web app to exit completely (even if that trashes the sessions in progress) then re-launch itself.

I wrote a brief .command file to do this:

#!/bin/bash
##################################################
# Script to launch one or more Xojo webapps
##################################################
# 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 MyWebAppName

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


# launch MyWebAppName
cd "/Volumes/Foobar/MyWebAppName"
./MyWebAppName --logging --secureport=8000 &  # launch in background use SSL

echo '# Processes'
ps | grep MyWebAppName | grep -v grep
echo '##################################################'

echo "App(s) are running.  To terminate them, run the command 'killall MyWebAppName'"

I’ve set this .command file to be opened as a user login item so it always launches when I reboot. This works nicely. However if the app crashes, it remains offline.

Is there an easy way using this technique to have the webapp re-launched if it crashes? Perhaps instead of launching it in the background (with &) we could make a loop which launches it, waits until it crashes, then re-launches it again?

[quote=164669:@Michael Diehr]I have a web app which does short surveys. Given the nature of the data collected, I would like the failure mode to be for the web app to exit completely (even if that trashes the sessions in progress) then re-launch itself.

I wrote a brief .command file to do this:

#!/bin/bash
##################################################
# Script to launch one or more Xojo webapps
##################################################
# 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 MyWebAppName

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


# launch MyWebAppName
cd "/Volumes/Foobar/MyWebAppName"
./MyWebAppName --logging --secureport=8000 &  # launch in background use SSL

echo '# Processes'
ps | grep MyWebAppName | grep -v grep
echo '##################################################'

echo "App(s) are running.  To terminate them, run the command 'killall MyWebAppName'"

I’ve set this .command file to be opened as a user login item so it always launches when I reboot. This works nicely. However if the app crashes, it remains offline.

Is there an easy way using this technique to have the webapp re-launched if it crashes? Perhaps instead of launching it in the background (with &) we could make a loop which launches it, waits until it crashes, then re-launches it again?[/quote]

You could have a small helper monitor your app running state through ps, and if it stops running, launch it again.

Have you considered using a launchd.plist with a KeepAlive property and saving it in the /Library/LaunchDaemons folder? That would give you one instance per machine that starts with each reboot and is restarted if it crashes, quits, etc. Here’s a sample launchd.plist:

[code]<?xml version="1.0" encoding="UTF-8"?>

Label com.MyBusinessName.MyWebAppName Program /My/Web/App/Path/MyWebAppName ProgramArguments --SecurePort=8000 KeepAlive [/code]

Edited to fix a typo.

Frederick, thank you. Do you happen to know, for a launchd app - does it run as a particular user and/or with a certain set of file permissions that’s different than if run by the logged-in user?

Your web app will run as root unless you use the UserName property in the launchd.plist file. It will also run whether or not a user is logged in. If you want it to run only when users are logged in or when a particular user is logged in then you’ll need to place the launchd.plist file in a different folder. Visit Creating Launch Daemons and Agents for the details.

This can automate all, is very amazing and donationware.
LaunchControl