2

I created a (Debian) init Script for an executable Jar file:

[...]
NAME=NameOfMyShellScript
DAEMON="/usr/share/myApp/bin/$NAME"
USER="myUser"
PIDFILE="/usr/share/myApp/run/$NAME.pid"
EXTRA_ARGS=" -c $USER --background --make-pidfile"
[...]
start-stop-daemon --start --quiet $EXTRA_ARGS --pidfile $PIDFILE --startas $DAEMON --
[...] 

My init script basically just invokes start-stop-daemon which executes a shell script:

#!/bin/sh
XNAME="hellofellow_1"
SCALATRA_ENV="development"
TMP_DIR="tmp/$XNAME/"
JAVA_OPTIONS="-Xmx2024M -XX:MaxPermSize=512M"
LOGFILE="logs/$XNAME.log"
PORT="8080"
JAVA_OPTIONS="$JAVA_OPTIONS -Djava.io.tmpdir=$TMP_DIR \
 -Dorg.scalatra.environment=$SCALATRA_ENV"
cd $(dirname 0ドル)/../
rm -Rf $TMP_DIR*
exec java $JAVA_OPTIONS -jar current/myJarFile.jar $PORT > $LOGFILE

This works so far, my applications runs and any output is written to $LOGFILE.

To enhance logging with rotation I then tried to to something like:

exec java $JAVA_OPTIONS -jar current/myJarFile.jar $PORT | multilog t s131072 n100 '!/bin/gzip' /my/log/dir

Logging works perfect, but now start-stop-daemon writes the PID of multilog to $PIDFILE and not the PID of my java process (so stopping does not work anymore).

Is there any "pipeline magic" that can be used to prevent that from happening? :)

asked Oct 24, 2012 at 15:41

1 Answer 1

1

Run your script with bash and use the process substitution construct to perform the logging. That way your "main" process will still be executed with the same process id as the shell was.

While you're at it, use an array for JAVA_OPTIONS, since it's a list of words. This way your script won't break even if $TMP_DIR or $SCALATRA_ENV contain shell special characters.

#!/bin/bash
set -e
XNAME="hellofellow_1"
SCALATRA_ENV="development"
TMP_DIR="tmp/$XNAME/"
JAVA_OPTIONS=(-Xmx2024M -XX:MaxPermSize=512M)
LOGFILE="logs/$XNAME.log"
PORT="8080"
JAVA_OPTIONS=("${JAVA_OPTIONS[@]}" "-Djava.io.tmpdir=$TMP_DIR")
JAVA_OPTIONS=("${JAVA_OPTIONS[@]}" "-Dorg.scalatra.environment=$SCALATRA_ENV")
cd "$(dirname "0ドル")/../"
rm -rf "$TMP_DIR"/*
exec java "${JAVA_OPTIONS[@]}" -jar current/myJarFile.jar "$PORT" \
 > >(multilog t s131072 n100 '!/bin/gzip' /my/log/dir)
answered Oct 25, 2012 at 0:24

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.