1

I am trying to get these scripts to work in an environment where the java application needs to run as user foobar no matter if it is started as root or foobar.

So these are my modifications to the init script, which is symlinked from init.d:

RUN_USER=foobar
USER_NAME=$(id --user --name)
START_SCRIPT=/opt/app/scripts/start

ARGS=""

start() {
    if [ "$USER_NAME" != "$RUN_USER" ]; then
        PID=$(su $RUN_USER -c $START_SCRIPT $ARGS > /dev/null 2>&1 & echo $!)
    else
        PID=`$START_SCRIPT $ARGS > /dev/null 2>&1 & echo $!`
    fi
}

And my /opt/app/scripts/start script looks like this:

exec java -jar /opt/app/app.jar > /dev/null 2>&1 & echo $!

To make the init script work the PID variable needs to be set properly, but it is being set to the PID of the su command. There are multiple questions about making that work, but none of the solutions work for me. I understand that the > /dev/null 2>&1 & echo $! part of my su command doesn't work, but it doesn't work when I change it to > /tmp/foobar either (no file gets written) and I don't know how to make it work. The su command on its own (su foobar -c /opt/app/scripts/start) will output the PID properly.

1 Answers1

1

You echoed java pid inside /opt/app/scripts/start script which is true but again echoed pid of su command inside the init script. I think you should change init script like this:

start() {
if [ "$USER_NAME" != "$RUN_USER" ]; then
    PID=$(su $RUN_USER -c "$START_SCRIPT $ARGS")
else
    PID=`$START_SCRIPT $ARGS`
fi
}
Kadir
  • 126
  • 1
  • 8