Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

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

share|improve this question
    
try removing the background operator & from end of awk command –  gwillie Aug 6 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. –  Qumo Aug 6 at 8:55
    
@Qumo Does this code: var="$(java -cp .:./lib/* com.mypackage.MyClass 2>&1)" for script 2 exit? –  BinaryZebra Aug 6 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? –  BinaryZebra Aug 6 at 18:52
    
@BinaryZebra var="$(java -version)" makes it exit. But I thinks it is not representative because my java program never exists. –  Qumo Aug 7 at 6:25

1 Answer 1

up vote 0 down vote accepted

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!

share|improve this answer

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.