如何在不让 shell 脚本等待程序完成执行的情况下从 shell 脚本调用 Java 程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24272162/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I call a Java program from a shell script without having the shell script to wait for the program to finish execution?
提问by mtrivedi
#!/bin/bash
JAVA_HOME="jdkpath"
CLASSPATH="JAR file path" .
$JAVA_HOME/bin/java -cp $CLASSPATH JavaClass
exit 0
I am using the above shell script to call my Java program but I want to make sure that once the shell script calls the Java program, it does not sit idle and wait for the program to complete. I don't know how to do that. I read in an online forum that I can put an "&" symbol at the end of the command where I supply the JAR file path but I am not sure since I am new to shell scripting. All the help is greatly appreciated.
我正在使用上面的 shell 脚本来调用我的 Java 程序,但我想确保一旦 shell 脚本调用 Java 程序,它不会闲置并等待程序完成。我不知道该怎么做。我在一个在线论坛上读到,我可以在提供 JAR 文件路径的命令末尾放置一个“&”符号,但我不确定,因为我是 shell 脚本的新手。非常感谢所有的帮助。
回答by iil
At the end of the shell command, you can put the &. It makes the program run in a subshell.
在shell命令的末尾,你可以放&。它使程序在子shell中运行。
java -cp /.../.../....jar ClassWithMain &
回答by kkarayannis
Adding & to the end will send Java to the background. That means that Java will run in the background and the shell script will continue running. But once the shell script reaches the end, it will wait for Java to finish before it exits. You have to disown the java program, and then the script will exit and java will continue to run. Just add the command "disown" directly below where you run the jar file.
添加 & 到最后会将 Java 发送到后台。这意味着 Java 将在后台运行,而 shell 脚本将继续运行。但是一旦shell脚本结束,它会等待Java完成才退出。您必须否认java程序,然后脚本将退出并且java将继续运行。只需在运行 jar 文件的位置正下方添加命令“disown”。