简体   繁体   English

如何检查shell脚本是否在java中成功执行

[英]how to check whether a shell script executed successfully in java

i have my code something like this the script "script.sh" takes some 7 minutes to execute completely 我有这样的代码,脚本“ 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);

    }

this code executes the shell script and stores the output in a log file. 此代码执行shell脚本并将输出存储在日志文件中。 how do i check in java whether this shell script ran successfully or not. 我如何在Java中检查此Shell脚本是否成功运行。 please suggest. 请提出建议。

i tried checking the last line of the file using a file reader with a code something like this 我尝试使用文件读取器检查文件的最后一行,代码如下所示

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");
                             }

but the problem with this code is that it will nit wait untill the log file is completely updated,it will just read the first few lines and print the else part. 但是此代码的问题在于,它将等到日志文件完全更新后,才读取前几行并打印else部分。

is there any other way to do this? 还有其他方法吗? please suggest 请建议

Best way to deal with this is to use the ProcessBuilder API which will give you control over the execution. 解决此问题的最佳方法是使用ProcessBuilder API,该API可让您控制执行。 And you can leverage its Process API to wait your processing till it completes execution and will also return the exit code.(Synchronous Processing) 您可以利用其Process API来等待您的处理,直到完成执行,并且还将返回退出代码。(同步处理)

Sample of how to use this API is below 下面是如何使用此API的示例

ProcessBuilder pb = new ProcessBuilder("script.sh", arg1, arg2);
Process p = pb.start();
int exitCode = p.waitFor();

You need to check if the process terminated which you can do with p.waitFor() which blocks until the process completed. 您需要检查该进程是否终止,可以使用p.waitFor()阻止该进程,直到该进程完成为止。 The return value of this call is the return code of the system command you invoked. 该调用的返回值是您调用的系统命令的返回代码。

If your exitCode value is 0, it means successfully executed. 如果exitCode值为0,则表示已成功执行。

Best fit for your need as it 最适合您的需求

Causes the current thread to wait, if necessary, until the process represented by this Process object has terminated. 如有必要,使当前线程等待,直到此Process对象表示的进程终止。

One way to do that is to execute a .jar at the end of script and in that .jar you can check either it completed successful or not, or you can also hit URL (if have any) to notify the other component at the end of script. 一种方法是在脚本末尾执行一个.jar ,在该.jar中,您可以检查它是否成功完成,或者您也可以点击URL(如果有)以在末尾通知其他组件脚本。 depends on your scenario. 取决于您的情况。

You can make your ssh connection via java and than execute your commands using this connection. 您可以通过Java建立ssh连接,然后使用该连接执行命令。 And like running commands via Java on your own machine, you can run your commands on this connection. 就像在自己的计算机上通过Java运行命令一样,您也可以在此连接上运行命令。 This way you can use 这样你可以使用

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()

Have a look at bobah's answer . 看一下鲍勃的答案

You can automate ssh login with sshpass and I think there are some other ways for doing it as well. 您可以使用sshpass自动化ssh登录,我认为还有其他一些方法可以做到这一点。 Search google for "ssh with password". 在Google中搜索“带密码的SSH”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 shell 脚本中检查 JAVA 程序是否成功执行? - How to check if the JAVA Program successfully executed in shell scripting? java - 通过Java DefaultExecutor和CommandLine执行shell脚本时如何检索可能的shell脚本退出代码? - How to retrieve possible shell script exit codes when Executed shell script by Java DefaultExecutor & CommandLine? 如何确定Java函数是否成功执行 - How to determine if java function executed successfully 如何使用java-selenium webdriver检查zip文件是否成功下载? - How to check whether a zip file is successfully downloaded or not using java-selenium webdriver? Shell脚本,如果语句从Java执行? - shell script if statement executed from java? 如何从servlet检查客户端是否启用了Java Script? - How to check whether Java Script is enabled or not in client side from servlet? 如何在GWT中检查代码是在服务器端还是客户端执行? - How to check whether code is executed on server or client side in GWT? 如何检查是否可以在Java 8中成功创建文件 - How to check if a file can be successfully created in Java 8 Java-如何识别方法是完全执行还是返回 - Java - How to identify whether a method executed completely or Returned 如何知道storedproc是否成功执行 - how to know if storedproc executed successfully
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM