I have two requirements for my Java application. If he dies, restart him. If the server restarts, reboot it is simple enough. Using the answer here I have a script that will restart when the java application dies.
#! / bin / bash
until java -Xms256m -Xmx768m -jar MyApp.jar; do
echo "MyApp crashed with exit code $ ?. Respawning ..."> & 2
sleep 5
done
I can run this with "nohup restart_script.sh &" and it will work all day without any problems. Now for the launch requirement. I took the /etc/init.d/crond script and replaced the crond binary with my script, but it crashes at startup.
#! / bin / bash
#
# Init file for my application.
#
. /etc/init.d/functions
MYAPP = restart_script.sh
PID_FILE = / var / run / myapp.pid
start () {
echo -n "Starting My App"
daemon --user appuser $ MYAPP
RETVAL = $?
echo
[$ RETVAL -eq 0] && touch / var / lock / subsys / myapp
return $ RETVAL
}
stop () {
echo -n "Stopping my application"
killproc $ MYAPP
RETVAL = $?
echo
[$ RETVAL -eq 0] && rm -f / var / lock / subsys / myapp
return $ RETVAL
}
...
case "$ 1" in
start)
start
;;
stop)
stop
;;
...
esac
When I run / sbin / service myapp, the script runs, but the console hangs. I tried "daemon --user appuser nohup $ MYAPP &" and I immediately return to the prompt without specifying [OK], and when I do ps, I still see init hanging. Any ideas on how to call the script in the init script and return it correctly?
Thank,
Greg