title: Waiting for process started by bash to end and get its PID
body: <p>I have a bash script that starts a java application using an until block.  It gets the exit code from the java application and if it gets a known shutdown exit code the bash script exits, otherwise, it restarts the java application</p>
  
  <pre><code>$CMD=java -jar program.jar
  until $CMD; do
    EXIT=$?
    if [ $EXIT == 2 ]
    then
         exit
    fi
    sleep 10
  done
  </code></pre>
  
  <p>This works fine, but I've got another application that needs to be aware of the pid of the process, up to this point, i've been using pgrep, but the powers that be don't like this idea.  They want me to write the PID to a pid file.  How can I modify my script so that it writes the pid to a pid file and removes it when it ends?</p>
  
  <p>Thank you.</p>