title: Daemonizing java application with JSVC
body: <p>I'm trying to daemonize my application using Apache Commons Daemon using the <em>Daemon</em> interface. Java application it self isn't doing anything just writes to stout.</p>
  
  <p>I compiled jsvc: <a href="http://people.apache.org/~mturk/daemon-1.0.10/" rel="noreferrer">http://people.apache.org/~mturk/daemon-1.0.10/</a></p>
  
  <p>(even tried newest version: <a href="http://people.apache.org/~mturk/daemon-1.0.12/" rel="noreferrer">http://people.apache.org/~mturk/daemon-1.0.12/</a> )</p>
  
  <p>and wrote this basic script.</p>
  
  <pre><code>do_exec()
  {
      $EXEC \
          -home "$JAVA_HOME" \
          -cp $CLASS_PATH \
          -outfile $LOG_OUT \
          -errfile $LOG_ERR \
          -pidfile $PID \
          $1 \
          $MAIN_CLASS
      echo "result: $?"
  }
  
  case "$1" in
      start)
              do_exec
              ;;
      stop)
              do_exec "-stop"
              ;;
      restart)
              do_exec "-stop"
              do_exec
              ;;
      *)
              echo "usage: daemon {start|stop|restart}" &gt;&amp;2
              exit 3
              ;;
  esac
  </code></pre>
  
  <p>Now when I try to stop the daemon when it's not running I'll get response code 255. Thas fantastic.</p>
  
  <p>But when I try start the daemon when it's already running I get response code 0. But in my errfile I find:</p>
  
  <pre><code>Still running according to PID file /tmp/deamon.pid, PID is 1799
  Service exit with a return value of 122
  </code></pre>
  
  <p>Same thing when I try throw exception in start() method and attempt to start daemon, response code 0. But errfile:</p>
  
  <pre><code> Service exit with a return value of 5
  </code></pre>
  
  <p>What I'm missing here? How can I inform user stat the deamon didn't start or that it is already running?</p>