该脚本可以等待外部进程完成吗?

发布于 2024-10-08 13:20:17 字数 799 浏览 4 评论 0原文

我已将此 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

轻许诺言 2024-10-15 13:20:17

正如其他人所说,删除 & ,bash 将等待命令完成。如果您坚持在后台运行进程,可以执行以下操作:

pid=`pidof java`

if [ "$pid" ]; then
    while kill -0 "$pid"; do
        sleep 1
    done
fi

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:

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.

暮倦 2024-10-15 13:20:17

命令末尾有一个 &

$JAVA_HOME/java -Xms256M -Xmx512M -classpath $CLASSPATH com.myapp.cam.conversion >>$WORK/job.out 2>&1 &

这使得它在后台运行。

删除 & 以等待 java 进程完成,然后再继续执行脚本。

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.

你对谁都笑 2024-10-15 13:20:17

如果您想在后台运行它,请在脚本末尾添加 wait 命令。

If you want to run it in the background, add a wait command at the end of the script.

匿名的好友 2024-10-15 13:20:17

不要在后台运行转换 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文