title: Running a java service using Ubuntu upstart
body: <p>I can't define a valid upstart conf script to run a java service using upstart with the following requirements:</p>
  
  <ul>
  <li>I have to specify classpath using folders because I have many jars in multiple folders</li>
  <li>I have to listen to the shutdown signal fired by <code>service myservicename stop</code></li>
  </ul>
  
  <p>Based on <a href="https://stackoverflow.com/a/11810813/897041">that answer</a>, I implemented a shutdown hook listener so I need upstart to send me the termination signal and wait for my application to terminate.</p>
  
  <p>Here is my buggy upstart script:</p>
  
  <pre><code>description "masa"
  author "Muhammad Gelbana &lt;m.glba@gmail.com&gt;"
  
  start on runlevel [2345]
  stop on shutdown
  kill timeout 120
  
  script
      LOGS_DIR=/home/mgelbana/services/RealServices/logs
      IPK_DB=/home/mgelbana/services/RealServices/config/db-ipk.properties
      PRO_DB=/home/mgelbana/services/RealServices/config/db-reporting-engine.properties
      MAIN_CLASS=com.sger.masaTA
      mkdir -p $LOGS_DIR
      CLASSPATH="/home/mgelbana/services/RealServices/masa-RealService-TA.jar"
      for i in /home/mgelbana/services/commons/*.jar; do
      CLASSPATH="$CLASSPATH:$i"
      done
      for i in /home/mgelbana/services/RealServices/lib/*.jar; do
      CLASSPATH="$CLASSPATH:$i"
      done
      echo '\n\n\n====================================================='
      echo 'Service startup:\t'`date`
      echo 'Main class:\t\t'`echo $MAIN_CLASS`
      echo 'Logs directory:\t\t'`echo $LOGS_DIR`
      echo 'masa database configuration:\t'`echo $IPK_DB`
      echo 'Pro configuration file:\t'`echo $PRO_DB`
      echo 'Starting engine...'
      java -Dta.id=2 -DIPK_DB=$IPK_DB -DPRO_DB=$PRO_DB -cp $CLASSPATH $MAIN_CLASS
  end script
  </code></pre>
  
  <p>The following error is shown in the <code>/var/log/upstart/myservicename.log</code> log:
  <code>/proc/self/fd/9: 9: /proc/self/fd/9: Syntax error: word unexpected (expecting "do")</code></p>
  
  <p>Thank you.</p>