Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

1 Answer 1

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
2  
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

 
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.