title: Start java process at boot and auto-restart on death
body: <p>I have two requirements for my Java app.  If it dies, restart it.  If the server reboots, restart it - simple enough.  Using the answer <a href="https://stackoverflow.com/questions/696839/how-do-i-write-a-bash-script-to-restart-a-process-if-it-dies">here</a> I have a script that will restart when the java application dies.</p>
  
  <pre>
  #!/bin/bash
  
  until java -Xms256m -Xmx768m -jar MyApp.jar; do
      echo "MyApp crashed with exit code $?.  Respawning... " >&2
      sleep 5
  done
  </pre>
  
  <p>I can run this with "nohup restart_script.sh &amp;" and it will run all day long without issue.  Now for the startup requirement.  I took the /etc/init.d/crond script and replaced the crond binary with my script but it hangs on startup.</p>
  
  <pre>
  #!/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
  </pre>
  
  <p>When I run /sbin/service myapp start the script starts but hangs the console.  I have tried "daemon --user appuser nohup $MYAPP &amp;" and I am immediately returned to the prompt without any [OK] indication and when I do a ps, I still see that the init is hung.  Any ideas how to call a script within the init script and get it to return properly?</p>
  
  <p>Thanks,</p>
  
  <p>Greg</p>