1

I can't define a valid upstart conf script to run a java service using upstart with the following requirements:

  • I have to specify classpath using folders because I have many jars in multiple folders
  • I have to listen to the shutdown signal fired by service myservicename stop

Based on that answer, I implemented a shutdown hook listener so I need upstart to send me the termination signal and wait for my application to terminate.

Here is my buggy upstart script:

description "masa"
author "Muhammad Gelbana <m.glba@gmail.com>"

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

The following error is shown in the /var/log/upstart/myservicename.log log: /proc/self/fd/9: 9: /proc/self/fd/9: Syntax error: word unexpected (expecting "do")

Thank you.

Community
  • 1
  • 1
Muhammad Gelbana
  • 3,890
  • 3
  • 43
  • 81
  • 2
    Show us your code. And what is your question? –  Oct 17 '12 at 09:45
  • A shutdown hook plus a longer `kill timeout` should be the right way to go. When stopping a job, Upstart first sends the TERM signal (which will cause your shutdown hook to fire), then if the process hasn't exited after the `kill timeout` (5 seconds by default) it sends the KILL signal. – Ian Roberts Oct 17 '12 at 09:50
  • @Tichodroma, very sorry, I edited it now. – Muhammad Gelbana Oct 17 '12 at 09:58
  • @Tichodroma, when I start the service, the stated error at the bottom is shown in the service's log. Thanks a lot. – Muhammad Gelbana Oct 17 '12 at 10:05
  • You can do away with the for loops by using the fact that Java treats a classpath entry of `/some/dir/*` as equivalent to `some/dir/jar1.jar:/some/dir/jar2.jar:...` for all jar files in the dir: `CLASSPATH='/home/mgelbana/services/RealServices/masa-RealService-TA.jar:/home/mgelbana/services/commons/*:/home/mgelbana/services/RealServices/lib/*'` (single quotes as the star must be protected from expansion by the shell). – Ian Roberts Oct 17 '12 at 10:49
  • Also you should say `exec java ...` rather than just `java` at the end of your script, to make sure the java process keeps the PID that upstart is tracking. If you don't then upstart will think your job keeps exiting immediately after it is started and will try to respawn it. – Ian Roberts Oct 17 '12 at 10:58
  • @IanRoberts, thanks a lot for the responses. I followed your advice but now when I start the service `service myservicename start` the cursor returns to an empty prompt where I can type on my keyboard ! and the upstart log for my service isn't created ! – Muhammad Gelbana Oct 17 '12 at 11:15
  • That's correct, if it worked successfully you shouldn't see any output. If you want a `/var/log/upstart` file to be created you need to specify `console log` in the conf file. – Ian Roberts Oct 17 '12 at 11:31
  • @IanRoberts, I added the `console log` in the conf file right beneath `kill timeout 120` but still the log file isn't created. Your help is very much appreciated, thank you. – Muhammad Gelbana Oct 17 '12 at 11:43

0 Answers0