Java执行Shell脚本超时控制

本文介绍了一个Java程序,该程序能够执行命令行脚本并对其进行超时控制,确保在脚本异常长时间运行时Java主进程不会被挂起。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

Java的Runtime可以执行命令行脚本,某些特定场合需要对脚本的执行时间进行控制,防止脚本某些异常情况下,一直未能正确结束,导致Java主进程挂起。本文的程序对这一过程进行了控制

 

Java代码
  1. import org.slf4j.Logger;   
  2. import org.slf4j.LoggerFactory;   
  3.   
  4. import java.io.BufferedReader;   
  5. import java.io.IOException;   
  6. import java.io.InputStreamReader;   
  7.   
  8.     
  9.   
  10. public class CommandUtils {   
  11.     private static Logger logger = LoggerFactory.getLogger(CommandUtils.class);   
  12.   
  13.     //default time out, in millseconds   
  14.     public static final int DEFAULT_TIMEOUT = 10 * 1000;   
  15.     public static final int DEFAULT_INTERVAL = 1000;   
  16.   
  17.     /**  
  18.      * Executes the specified command in a separate process. The method then blocks until the process returned.  
  19.      * If an error arises during the execution or if the exeecuted process returned an non-null return code,  
  20.      * the content of the process' stderr is returned to the caller. If the execution is fine, null is returned.  
  21.      *  
  22.      * @param command String  
  23.      * @return CommandResult  
  24.      */  
  25.     public static CommandResult exec(String command) {   
  26.         long start = System.currentTimeMillis();   
  27.         long last;   
  28.   
  29.         CommandResult commandResult = new CommandResult();   
  30.   
  31.         try {   
  32.             Process process = Runtime.getRuntime().exec(command);   
  33.             process(process, commandResult);   
  34.   
  35.             if (process != null) {   
  36.                 process.destroy();   
  37.             }   
  38.   
  39.             last = (System.currentTimeMillis() - start) / 1000;   
  40.             logger.info("Execute command [" + command + "], last [" + last + "] s.");   
  41.   
  42.         } catch (Exception e) {   
  43.             last = (System.currentTimeMillis() - start) / 1000;   
  44.             String error = "Execute command [" + command + "] last [" + last + "] s, failed [" + e.getMessage() + "]";   
  45.             logger.error(error, e);   
  46.   
  47.             commandResult.setExitValue(CommandResult.EXIT_VALUE_UNEXCEPTED);   
  48.             commandResult.setErrorOutput(error);   
  49.         }   
  50.   
  51.         return commandResult;   
  52.     }   
  53.   
  54.     private static void process(Process process, CommandResult commandResult) {   
  55.         BufferedReader errorReader = null;   
  56.         BufferedReader inputReader = null;   
  57.   
  58.         try {   
  59.             errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));   
  60.             inputReader = new BufferedReader(new InputStreamReader(process.getInputStream()));   
  61.   
  62.             //timeout control   
  63.             long start = System.currentTimeMillis();   
  64.             boolean processFinished = false;   
  65.   
  66.             while (System.currentTimeMillis() - start < DEFAULT_TIMEOUT && !processFinished) {   
  67.                 processFinished = true;   
  68.                 try {   
  69.                     process.exitValue();   
  70.                 } catch (IllegalThreadStateException e) {   
  71.                     // process hasn't finished yet   
  72.                     processFinished = false;   
  73.   
  74.                     try {   
  75.                         Thread.sleep(DEFAULT_INTERVAL);   
  76.                     } catch (InterruptedException e1) {   
  77.                         logger.error("Process, failed [" + e.getMessage() + "]", e);   
  78.                     }   
  79.                 }   
  80.             }   
  81.   
  82.             if (!processFinished) {   
  83.                 commandResult.setExitValue(CommandResult.EXIT_VALUE_TIMEOUT);   
  84.                 commandResult.setErrorOutput("Command process timeout");   
  85.                 return;   
  86.             }   
  87.   
  88.             commandResult.setExitValue(process.waitFor());   
  89.   
  90.             StringBuffer sb;   
  91.             String line;   
  92.   
  93.             //parse error info   
  94.             if (errorReader.ready()) {   
  95.                 sb = new StringBuffer();   
  96.                 while ((line = errorReader.readLine()) != null) {   
  97.                     sb.append(line);   
  98.                 }   
  99.                 commandResult.setErrorOutput(sb.toString());   
  100.             }   
  101.   
  102.             //parse info   
  103.             if (inputReader.ready()) {   
  104.                 sb = new StringBuffer();   
  105.                 while ((line = inputReader.readLine()) != null) {   
  106.                     sb.append(line);   
  107.                 }   
  108.                 commandResult.setInfoOutput(sb.toString());   
  109.             }   
  110.   
  111.         } catch (Exception e) {   
  112.             String error = "Command process, failed [" + e.getMessage() + "]";   
  113.             logger.error(error, e);   
  114.   
  115.             commandResult.setExitValue(CommandResult.EXIT_VALUE_UNEXCEPTED);   
  116.             commandResult.setErrorOutput(error);   
  117.   
  118.         } finally {   
  119.             if (errorReader != null) {   
  120.                 try {   
  121.                     errorReader.close();   
  122.                 } catch (IOException e) {   
  123.                     logger.error("Close BufferedReader, failed [" + e.getMessage() + "]", e);   
  124.                 }   
  125.             }   
  126.   
  127.             if (inputReader != null) {   
  128.                 try {   
  129.                     inputReader.close();   
  130.                 } catch (IOException e) {   
  131.                     logger.error("Close BufferedReader, failed [" + e.getMessage() + "]", e);   
  132.                 }   
  133.             }   
  134.         }   
  135.     }   
  136. }  
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

 

public class CommandUtils {
    private static Logger logger = LoggerFactory.getLogger(CommandUtils.class);

    //default time out, in millseconds
    public static final int DEFAULT_TIMEOUT = 10 * 1000;
    public static final int DEFAULT_INTERVAL = 1000;

    /**
     * Executes the specified command in a separate process. The method then blocks until the process returned.
     * If an error arises during the execution or if the exeecuted process returned an non-null return code,
     * the content of the process' stderr is returned to the caller. If the execution is fine, null is returned.
     *
     * @param command String
     * @return CommandResult
     */
    public static CommandResult exec(String command) {
        long start = System.currentTimeMillis();
        long last;

        CommandResult commandResult = new CommandResult();

        try {
            Process process = Runtime.getRuntime().exec(command);
            process(process, commandResult);

            if (process != null) {
                process.destroy();
            }

            last = (System.currentTimeMillis() - start) / 1000;
            logger.info("Execute command [" + command + "], last [" + last + "] s.");

        } catch (Exception e) {
            last = (System.currentTimeMillis() - start) / 1000;
            String error = "Execute command [" + command + "] last [" + last + "] s, failed [" + e.getMessage() + "]";
            logger.error(error, e);

            commandResult.setExitValue(CommandResult.EXIT_VALUE_UNEXCEPTED);
            commandResult.setErrorOutput(error);
        }

        return commandResult;
    }

    private static void process(Process process, CommandResult commandResult) {
        BufferedReader errorReader = null;
        BufferedReader inputReader = null;

        try {
            errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
            inputReader = new BufferedReader(new InputStreamReader(process.getInputStream()));

            //timeout control
            long start = System.currentTimeMillis();
            boolean processFinished = false;

            while (System.currentTimeMillis() - start < DEFAULT_TIMEOUT && !processFinished) {
                processFinished = true;
                try {
                    process.exitValue();
                } catch (IllegalThreadStateException e) {
                    // process hasn't finished yet
                    processFinished = false;

                    try {
                        Thread.sleep(DEFAULT_INTERVAL);
                    } catch (InterruptedException e1) {
                        logger.error("Process, failed [" + e.getMessage() + "]", e);
                    }
                }
            }

            if (!processFinished) {
                commandResult.setExitValue(CommandResult.EXIT_VALUE_TIMEOUT);
                commandResult.setErrorOutput("Command process timeout");
                return;
            }

            commandResult.setExitValue(process.waitFor());

            StringBuffer sb;
            String line;

            //parse error info
            if (errorReader.ready()) {
                sb = new StringBuffer();
                while ((line = errorReader.readLine()) != null) {
                    sb.append(line);
                }
                commandResult.setErrorOutput(sb.toString());
            }

            //parse info
            if (inputReader.ready()) {
                sb = new StringBuffer();
                while ((line = inputReader.readLine()) != null) {
                    sb.append(line);
                }
                commandResult.setInfoOutput(sb.toString());
            }

        } catch (Exception e) {
            String error = "Command process, failed [" + e.getMessage() + "]";
            logger.error(error, e);

            commandResult.setExitValue(CommandResult.EXIT_VALUE_UNEXCEPTED);
            commandResult.setErrorOutput(error);

        } finally {
            if (errorReader != null) {
                try {
                    errorReader.close();
                } catch (IOException e) {
                    logger.error("Close BufferedReader, failed [" + e.getMessage() + "]", e);
                }
            }

            if (inputReader != null) {
                try {
                    inputReader.close();
                } catch (IOException e) {
                    logger.error("Close BufferedReader, failed [" + e.getMessage() + "]", e);
                }
            }
        }
    }
}

 

<think>我们被要求提供Java调用Shell脚本的示例代码。根据引用内容,我们可以总结出两种主要方法:1.使用Runtime类的exec方法2.使用ProcessBuilder引用[2]和[3]提供了使用Runtime.exec的示例,引用[4]展示了一个Shell脚本的内容。我们将提供两个示例:1.使用Runtime.exec执行Shell脚本(基本方法)2.使用ProcessBuilder(更灵活的方法)同时,我们需要注意:-处理输入输出流,避免进程阻塞-等待进程执行完成-错误处理###示例1:使用Runtime.exec执行Shell脚本```javaimportjava.io.BufferedReader;importjava.io.InputStreamReader;publicclassRunShellScript{publicstaticvoidmain(String[]args){//Shell脚本路径StringshPath="/path/to/your/script.sh";try{//执行脚本Processprocess=Runtime.getRuntime().exec(shPath);//等待脚本执行完成intexitCode=process.waitFor();System.out.println("Exitcode:"+exitCode);//读取脚本的标准输出流BufferedReaderreader=newBufferedReader(newInputStreamReader(process.getInputStream()));Stringline;while((line=reader.readLine())!=null){System.out.println(line);}//读取脚本的错误输出流BufferedReadererrorReader=newBufferedReader(newInputStreamReader(process.getErrorStream()));while((line=errorReader.readLine())!=null){System.err.println(line);}}catch(Exceptione){e.printStackTrace();}}}```###示例2:使用ProcessBuilder(推荐)ProcessBuilder提供了更灵活的方式来设置工作目录、环境变量等。```javaimportjava.io.BufferedReader;importjava.io.InputStreamReader;publicclassRunShellScriptWithProcessBuilder{publicstaticvoidmain(String[]args){try{//创建ProcessBuilder,指定命令ProcessBuilderpb=newProcessBuilder("/bin/bash","/path/to/your/script.sh");//设置工作目录(可选)//pb.directory(newFile("/path/to/work/dir"));//合并标准错误和标准输出(可选)pb.redirectErrorStream(true);//启动进程Processprocess=pb.start();//读取输出BufferedReaderreader=newBufferedReader(newInputStreamReader(process.getInputStream()));Stringline;while((line=reader.readLine())!=null){System.out.println(line);}//等待进程结束intexitCode=process.waitFor();System.out.println("Exitcode:"+exitCode);}catch(Exceptione){e.printStackTrace();}}}```###注意事项1.**脚本权限**:确保Shell脚本有可执行权限(`chmod+xscript.sh`)。2.**路径问题**:尽量使用绝对路径,或者在ProcessBuilder中设置工作目录。3.**环境变量**:如果脚本依赖环境变量,可以在ProcessBuilder中设置环境变量:```javaMap<String,String>env=pb.environment();env.put("VAR_NAME","value");```4.**输入输出处理**:一定要处理输入输出流,否则可能导致进程阻塞。5.**错误流处理**:单独处理错误流,或者使用`redirectErrorStream(true)`将错误流合并到标准输出。6.**参数传递**:可以向脚本传递参数:```javaProcessBuilderpb=newProcessBuilder("/bin/bash","/path/to/script.sh","arg1","arg2");```###相关问题1.**如何在Java执行带参数的Shell脚本?**>在ProcessBuilder的构造参数中,将参数依次添加在脚本路径后面。2.**如何获取Shell脚本执行结果(退出码)?**>使用`process.waitFor()`返回的int值就是退出码。3.**Java调用Shell脚本时如何设置超时?**>可以使用`process.waitFor(longtimeout,TimeUnitunit)`方法(Java8+)设置超时。4.**如何向Shell脚本传递输入?**>通过`process.getOutputStream()`获取输出流(即脚本的标准输入),写入数据。5.**在Windows系统上如何调用Shell脚本?**>使用cmd或PowerShell执行,例如:`ProcessBuilder("cmd.exe","/c","script.bat")`。6.**如何处理Shell脚本中的空格和特殊字符?**>使用ProcessBuilder可以避免参数解析问题,因为它接受字符串数组,每个参数独立传递。7.**Java调用Shell脚本的性能如何优化?**>避免频繁调用小脚本,考虑合并脚本;使用线程池管理并发调用。8.**如何异步执行Shell脚本?**>将调用放在新线程中执行,或者使用CompletableFuture异步处理。[^1]:[转载]JAVA调用Shell脚本。在实际项目中,JAVA有时候需要调用C写出来的东西,除了JNI以外,我认为一种比较好的方法是JAVA调用Shell。先把C写出来的make成可执行文件,然后再写一个shell脚本执行该可执行文件,最后是JAVA调用该shell脚本。[^2]:Java调用Shell脚本。代码示例展示了使用Runtime.getRuntime().exec执行脚本并读取输出。[^3]:Java调用Shell脚本。介绍了Process类的多种exec方法。[^4]:Shell脚本示例内容。</think>### Java 调用 Shell 脚本的完整指南 以下是两种常用的 Java 调用 Shell 脚本的方法,包含完整代码示例和最佳实践: #### 方法 1:使用 `Runtime.exec()`(基本方法) ```java import java.io.BufferedReader; import java.io.InputStreamReader; public class ShellExecutor { public static void main(String[] args) { try { // 1. 定义Shell脚本路径 String shPath = "/home/user/scripts/myscript.sh"; // 2. 执行脚本 Process process = Runtime.getRuntime().exec(shPath); // 3. 等待脚本执行完成 int exitCode = process.waitFor(); System.out.println("脚本执行完成,退出码: " + exitCode); // 4. 获取脚本输出 BufferedReader reader = new BufferedReader( new InputStreamReader(process.getInputStream()) ); StringBuilder output = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { output.append(line).append("\n"); } System.out.println("脚本输出:\n" + output); } catch (Exception e) { e.printStackTrace(); } } } ``` #### 方法 2:使用 `ProcessBuilder`(推荐方法,更灵活) ```java import java.io.BufferedReader; import java.io.InputStreamReader; public class AdvancedShellExecutor { public static void main(String[] args) { try { // 1. 创建命令数组 String[] command = { "/bin/bash", "/home/user/scripts/complex_script.sh", "arg1", // 脚本参数1 "arg2" // 脚本参数2 }; // 2. 配置进程环境 ProcessBuilder pb = new ProcessBuilder(command); pb.redirectErrorStream(true); // 合并错误流到标准输出 // 3. 启动进程 Process process = pb.start(); // 4. 读取输出 BufferedReader reader = new BufferedReader( new InputStreamReader(process.getInputStream()) ); StringBuilder output = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { output.append(line).append("\n"); } // 5. 等待执行完成 int exitCode = process.waitFor(); System.out.println("输出结果:\n" + output); System.out.println("退出码: " + exitCode); } catch (Exception e) { e.printStackTrace(); } } } ``` #### 示例 Shell 脚本 (`myscript.sh`) ```bash #!/bin/bash echo "Java调用的Shell脚本开始执行" echo "当前时间: $(date)" echo "参数数量: $#" echo "第一个参数: $1" echo "第二个参数: $2" echo "脚本执行完成" ``` ### 关键注意事项 1. **脚本权限问题**: ```bash chmod +x /path/to/your/script.sh # 确保脚本执行权限 ``` 2. **路径处理**: - 使用绝对路径更可靠 - 或设置工作目录: ```java pb.directory(new File("/path/to/working/dir")); ``` 3. **参数传递**: - 通过命令数组传递参数: ```java String[] cmd = {"/bin/bash", "script.sh", "param1", "param2"}; ``` 4. **超时控制** (Java 8+): ```java if (!process.waitFor(30, TimeUnit.SECONDS)) { process.destroy(); System.err.println("脚本执行超时"); } ``` 5. **错误流处理**: ```java // 单独读取错误流 BufferedReader errorReader = new BufferedReader( new InputStreamReader(process.getErrorStream()) ); ``` ### 常见问题解决方案 1. **权限拒绝错误**: - 检查脚本执行权限 - 检查Java进程用户权限 2. **脚本执行但无输出**: - 确保处理了输入流 - 添加`flush`操作到脚本: ```bash echo "Debug info" > /proc/self/fd/1 ``` 3. **特殊字符处理**: ```java // 使用数组传递含空格参数 String[] cmd = { "sh", "-c", "echo 'Hello World'; ls -l" }; ``` 4. **环境变量问题**: ```java Map<String, String> env = pb.environment(); env.put("CUSTOM_VAR", "value"); ``` ### 最佳实践建议 1. 使用`ProcessBuilder`代替`Runtime.exec()`(更安全灵活) 2. 始终处理标准输出和错误输出流 3. 设置执行超时防止进程挂起 4. 在脚本中添加明确的退出码(0=成功,非0=错误) 5. 对敏感信息使用加密或外部配置 ### 相关问题 1. **如何在Java中捕获Shell脚本的错误输出?** > 使用`process.getErrorStream()`单独读取错误流,或设置`redirectErrorStream(true)`合并流[^2]。 2. **如何向Shell脚本传递复杂参数(含空格或特殊字符)?** > 使用参数数组并正确转义,或通过环境变量传递[^3]。 3. **Java调用Shell脚本时如何设置超时限制?** > 使用`process.waitFor(long timeout, TimeUnit unit)`方法(Java 8+)[^3]。 4. **如何在Windows系统上执行类似的Shell脚本调用?** > 使用`cmd.exe /c`前缀,如:`new ProcessBuilder("cmd.exe", "/c", "script.bat")`。 5. **如何提升Java调用Shell脚本的性能?** > 避免频繁调用小脚本,改用批处理;使用线程池管理并发调用。 6. **如何安全地执行用户提供的Shell脚本?** > 使用安全沙箱环境;严格验证脚本内容;限制执行权限。 [^1]: [转载]JAVA调用Shell脚本。在实际项目中,JAVA有时候需要调用C写出来的东西,除了JNI以外,我认为一种比较好的方法是JAVA调用Shell。 [^2]: Java调用Shell脚本。展示了使用Runtime.getRuntime().exec执行脚本并读取输出的基本方法。 [^3]: Java调用Shell脚本。介绍了Process类多种exec方法的使用方式。 [^4]: 展示了Shell脚本的示例内容,包含参数处理和输出信息。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值