Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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? :)

share|improve this question

1 Answer 1

up vote 1 down vote accepted

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)
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.