Java程序运行、停止Shell脚本
2013-11-01 16:16
411 查看
用Java程序来控制shell脚本的运行和停止。具体来讲,这个Java程序至少要有三个功能:
运行Shell脚本;
等待Shell脚本执行结束;
停止运行中的Shell程序;
从功能需求来看,似乎是比较容易做到的。尽管没有写过类似功能的程序,Google一下,很快就有答案了。
用Runtime或者ProcessBuilder可以运行程序,而Process类的waitFor()和destroy()方法分别满足功能2和3。
waitFor()方法可以正确等待shell程序退出,但是destroy()方法并没有结束shell脚本相关的进程。
这是一个BUG。
JDK-bug-4770092:Process.destroy()不能结束孙子进程(grandchildren)。
运行Shell脚本;
等待Shell脚本执行结束;
停止运行中的Shell程序;
从功能需求来看,似乎是比较容易做到的。尽管没有写过类似功能的程序,Google一下,很快就有答案了。
用Runtime或者ProcessBuilder可以运行程序,而Process类的waitFor()和destroy()方法分别满足功能2和3。
import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.io.InputStreamReader; public class ShellRunner extends Thread { private Process proc; private String dir; private String shell; public ShellRunner(String dir, String shell) { super(); this.proc = null; this.dir = dir; this.shell = shell; } @Override public void run() { try { ProcessBuilder builder = new ProcessBuilder("sh", dir + shell); builder.directory(new File(dir)); proc = builder.start(); System.out.println("Running ..."); int exitValue = proc.waitFor(); System.out.println("Exit Value: " + exitValue); } catch (IOException e) { e.getLocalizedMessage(); } catch (InterruptedException e) { e.getLocalizedMessage(); } } public void kill() { if (this.getState() != State.TERMINATED) { proc.destroy(); } } public static void main(String args[]) { ShellRunner runner = new ShellRunner("/tmp/", "run.sh"); runner.start(); InputStreamReader inputStreamReader = new InputStreamReader(System.in); BufferedReader reader = new BufferedReader(inputStreamReader); try { String line = null; while ( (line = reader.readLine()) != null ) { if (line.equals("kill")) { runner.kill(); } else if (line.equals("break")) { break; } else { System.out.println(runner.getState()); } } reader.close(); inputStreamReader.close(); } catch (IOException e) { e.printStackTrace(); } } }
waitFor()方法可以正确等待shell程序退出,但是destroy()方法并没有结束shell脚本相关的进程。
这是一个BUG。
JDK-bug-4770092:Process.destroy()不能结束孙子进程(grandchildren)。
相关文章推荐
- Java程序运行、停止Shell脚本
- Java后台执行Shell脚本, Java方法不用等待Shell程序执行完成并保证关掉Java程序Shell脚本正常运行
- linux下运行java程序的shell脚本
- shell脚本运行java程序jar的方法
- shell脚本运行java程序
- 运行java程序shell脚本
- shell脚本运行java程序
- Java后台执行Shell脚本, Java方法不用等待Shell程序执行完成并保证关掉Java程序Shell脚本正常运行
- 用shell脚本运行Java程序
- Linux 启动停止SpringBoot jar 程序部署Shell 脚本
- Linux系统-shell脚本运行程序
- 关于java实现的mapreduce程序打包后通过脚本运行出现classnotfound异常
- Java程序定时执行shell脚本
- jprofiler 监控java程序的shell脚本
- java工具(一)----如何在java程序中调用linux命令或者shell脚本及 画图
- Linux:shell脚本反复运行同一个程序
- Java运行shell脚本
- java程序执行,调用shell命令和shell脚本
- Linux下Java程序的启动shell脚本
- java独立程序部署shell脚本