5

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.

crash3k
  • 53
  • 1
  • 3

1 Answers1

3

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"
S19N
  • 1,803
  • 1
  • 19
  • 28
  • I tried that, but the $PIDFILE is just empty (except for a linebreak) when using that command. – crash3k Apr 02 '12 at 23:19
  • 4
    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. – phemmer Apr 03 '12 at 00:27