该脚本可以等待外部进程完成吗?
我已将此 shell 脚本作为 JAR 文件的包装器编写。该脚本可以毫无问题地启动 JAR,但无需等待 JAR 完成其工作即可完成。
#!/bin/bash
export WORK=/opt/conversion
export LOG=$WORK
export [email protected]
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 $CLASSPATH com.myapp.cam.conversion >>$WORK/job.out 2>&1 &
echo $! > $WORK/job.pid
mail -s "Conversion" $XMAIL < $WORK/user_message
exit 0
有没有办法让脚本等待我的 JAR 文件完成?
感谢您的意见。
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 [email protected]
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 $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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
正如其他人所说,删除
&
,bash 将等待命令完成。如果您坚持在后台运行进程,可以执行以下操作:kill
命令通常用于向特定进程发送信号,但可以使用0
作为信号,您仅检查具有此类 pid 的进程是否存在,而不发送信号。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:The
kill
command is normally used to send signals to a particular process, but by using0
as a signal you are only checking whether a process with such a pid exists without sending a signal.命令末尾有一个
&
:这使得它在后台运行。
删除
&
以等待java
进程完成,然后再继续执行脚本。You have a
&
at the end of the command:which makes it run in background.
Remove the
&
to wait for thejava
process to complete before you proceed in the script.如果您想在后台运行它,请在脚本末尾添加
wait
命令。If you want to run it in the background, add a
wait
command at the end of the script.不要在后台运行转换 Java 应用程序。或者,重复运行
ps
直到看不到 pid。这将允许您在等待时做一些事情。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.