I have written this shell script as wrapper to a JAR file. The script launches the JAR without problem but completes without waiting for the JAR to finish its job.
#!/bin/bash
export WORK=/opt/conversion
export LOG=$WORK
export XMAIL=me@email.com
export JAVA_BASE=/usr/java/jdk1.6.0_06
export JAVA_HOME=/usr/java/jdk1.6.0_06/bin
export JAR=$WORK/conversion.jar
export CLASSPATH=$JAVA_BASE/lib/tools.jar
export CLASSPATH=$CLASSPATH:$WORK/lib/ojdbc14.jar
export CLASSPATH=$CLASSPATH:$JAR
$JAVA_HOME/java -Xms256M -Xmx512M -classpath 开发者_如何学Python$CLASSPATH com.myapp.cam.conversion >>$WORK/job.out 2>&1 &
echo $! > $WORK/job.pid
mail -s "Conversion" $XMAIL < $WORK/user_message
exit 0
Is there a way to have the script wait on my JAR file to complete?
Thanks for your input.
As others have said, remove the &
and bash will wait till the command finishes. If you insist on running your process in the background, here is what you could do:
pid=`pidof java`
if [ "$pid" ]; then
while kill -0 "$pid"; do
sleep 1
done
fi
The kill
command is normally used to send signals to a particular process, but by using 0
as a signal you are only checking whether a process with such a pid exists without sending a signal.
You have a &
at the end of the command:
$JAVA_HOME/java -Xms256M -Xmx512M -classpath $CLASSPATH com.myapp.cam.conversion >>$WORK/job.out 2>&1 &
which makes it run in background.
Remove the &
to wait for the java
process to complete before you proceed in the script.
If you want to run it in the background, add a wait
command at the end of the script.
Don't run the conversion Java application in the background. Or, run ps
repeatedly until you don't see the pid. This will allow you to do stuff while waiting.
精彩评论