我的代码是这样的 脚本“script.sh”需要大约7分钟才能完全执行
private void function(String par1,String par2,String par3,String par4){
String logFile = logFolder + par1 + "_label.log";
String cmd = " /bin/sh " + scriptFolder + "script.sh" + " " +
par1 + " "
+ par2 + " "
+ par3 + " "
+ par4 + " > " +
logFile + " 2>&1 ";
logger.info("label update script" +cmd);
/* vm info */
String host = ConstantsServers.LABEL_UPDATE_SERVER;
String user = "USER";
String passwd = System.getenv("USER_PW");
/*
* start the script as user on the server
*/
new RunCmdViaSSH(host, user, passwd, cmd);
logger.debug("Log file created at:" +logFile);
}
此代码执行shell脚本并将输出存储在日志文件中。 我如何在java中检查此shell脚本是否成功运行。 请建议。
我尝试使用带有类似代码的文件阅读器检查文件的最后一行
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
//checks whether the script ran successfully or not
String strLine = null,tmp;
while ((tmp = br.readLine()) != null) {
strLine=tmp;
}
String lastLine = strLine;
logger.debug(lastLine);
if (lastLine.contains("script ran successfully")) {
logger.debug("script passed");
}
else{
logger.debug("script failed");
}
但是这段代码的问题在于它会等待,直到日志文件完全更新,它只会读取前几行并打印else部分。
有没有其他方法可以做到这一点? 请建议
答案 0 :(得分:2)
处理此问题的最佳方法是使用ProcessBuilder API,它可以控制执行。 并且您可以利用其Process API等待处理直到它完成执行,并且还将返回退出代码。(同步处理)
如何使用此API的示例如下
ansible_hostname
您需要检查流程是否终止,您可以使用ProcessBuilder pb = new ProcessBuilder("script.sh", arg1, arg2);
Process p = pb.start();
int exitCode = p.waitFor();
阻止该流程,直到流程完成为止。此调用的返回值是您调用的系统命令的返回码。
如果您的exitCode值为0,则表示已成功执行。
最适合您的需求
如果需要,导致当前线程等待,直到进程 由此Process对象表示已终止。
答案 1 :(得分:0)
这样做的一种方法是在脚本末尾执行<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:tag="reqtag"
>
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:showDividers="beginning|end"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Date"
android:id="@+id/tvClaimDateReimbursementRequest"
android:layout_marginRight="50dp"
android:textSize="12dp"
android:width="100dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Type"
android:textSize="12dp"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:id="@+id/tvTypeRequestReimbursement"
android:width="80dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Amount"
android:textSize="10dp"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:width="100dp"
android:id="@+id/tvAmountReimbursementRequest" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Status"
android:textSize="12dp"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:id="@+id/tvStatusReimbursementRequest" />
</TableRow>
</TableLayout>
,在.jar
中你可以检查它是否成功完成,或者你也可以点击URL(如果有的话) )在脚本结束时通知另一个组件。取决于你的情况。
答案 2 :(得分:0)
您可以通过java进行ssh连接,而不是使用此连接执行命令。就像在自己的机器上通过Java运行命令一样,您可以在此连接上运行命令。这样你可以使用
Process process = Process.getRuntime().exec(sshCmd);
// you should get error stream and check for errors before waiting
process.getOutputStream().println(yourCmd);
process.waitFor();
// now process is finished and you can get full output from process.getInputstream()
您可以使用sshpass自动执行ssh登录,我认为还有其他一些方法可以执行此操作。在谷歌搜索“ssh with password”。