1

I start a bash script (synchronously from java as glassfish user) which triggers another bash script:

Script 1 executed from java:

#!/bin/bash

#Start script2 as myUser on myUser desktop
echo myPassword | su -c "export DISPLAY=:0.0;xhost +localhost;script2.sh;" myUser &
echo "After subscript"
#---------------
#... other tasks
#---------------
echo "Before exit"
exit 0

Script 2 executed from script 1

#!/bin/bash

# Start java app with date added by awk at the beginning of every line
java -cp .:./lib/* com.mypackage.MyClass 2>&1 | awk '{print strftime("%D %T",systime())" "$0 }' >> logFile.log &

# Start java app without date added by awk on logs
#java -cp .:./lib/* com.mypackage.MyClass 2>&1 >> logFile.log &

My problem is that the script 1 never exits unless I kill the java process started in the script 2. This problem DOES NOT occurs if I remove the awk part in the script 2 (if I use the commented java command line).

I'm running : - GNU bash, version 4.2.53(1)-release (x86_64-redhat-linux-gnu) on Centos 6.4 - java 1.6.45 X86_64 and Glassfish 3.1.2.2

  • try removing the background operator & from end of awk command – gwillie Aug 6 '15 at 8:02
  • Ok, so I tried with this command: java -cp .:./lib/* com.mypackage.MyClass 2>&1 | awk '{print strftime("%D %T",systime())" "$0 }' >> logFile.log but it still hangs. – Quentin Morrier Aug 6 '15 at 8:55
  • @Qumo Does this code: var="$(java -cp .:./lib/* com.mypackage.MyClass 2>&1)" for script 2 exit? – user79743 Aug 6 '15 at 14:59
  • @Qumo Ok, lets clear the java part. Making this the only executable (not commented) line in script2: var="$(java -version)" makes it exit? – user79743 Aug 6 '15 at 18:52
  • @BinaryZebra var="$(java -version)" makes it exit. But I thinks it is not representative because my java program never exists. – Quentin Morrier Aug 7 '15 at 6:25
0

Finally I found the solution.

As far as I've understood it was the way to put in background both processes in my script 2. It looks that the & apply only to awk (that would explain why it works without the awk part).

With braces I can put in background both parts (java and awk):

Script 2 executed from script 1:

#!/bin/bash

# Start java app with date added by awk at the beginning of every line
{ java -cp .:./lib/* com.mypackage.MyClass 2>&1 | awk '{print strftime("%D %T",systime())" "$0 }'; } &>> logFile.log &

Thanks everyone!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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