Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm writing a init.d-script for an java-app. But the java-app should be run by another user.

(The OS I'm using is Debian Squeeze.)

I already got this:

/bin/su - $USER - c "cd $PATH;echo $PASSWORD | $JAVA -Xmx256m -jar $PATH/app.jar -d > /dev/null" & PID=$!
/bin/su - $USER - c "echo $PID > $PIDFILE"

But this will of course only save the pid of the "/bin/su"-process instead of the pid of the created java-process.

share|improve this question
up vote 2 down vote accepted

Echo the PID inside the commands executed by "su":

/bin/su - $USER -c "cd $PATH;echo $PASSWORD | \
  $JAVA -Xmx256m -jar $PATH/app.jar -d > /dev/null & echo $! > $PIDFILE"
share|improve this answer
I tried that, but the $PIDFILE is just empty (except for a linebreak) when using that command. – crash3k Apr 2 '12 at 23:19
Its empty because $! is being evaluated by the shell running su, not the shell that su runs. If you change the $! to \$! or change the double-quotes to single-quotes it should work. – Patrick Apr 3 '12 at 0:27
Thank you! Works like a charm :) – crash3k Apr 3 '12 at 10:42

Your Answer


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.