如何等待进程在java或clojure中结束 [英] how to wait for a process to end in java or clojure

查看:124
本文介绍了如何等待进程在java或clojure中结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我没有开始的过程,如何得到通知,并且是一种恢复其退出代码和输出的方法?进行监视的进程将作为root /管理员运行。

How can I be notified when a process I did not start ends and is their a way to recover its exit code and or output? the process doing the watching will be running as root/administrator.

推荐答案

您可以通过调用shell命令检查当前是否从java运行进程,该命令列出了所有当前进程,输出。在linux / unix / mac os下,命令是 ps ,在Windows下是任务列表

You can check whether a process is currently running from java by calling a shell command that lists all the current processes and parsing the output. Under linux/unix/mac os the command is ps, under windows it is tasklist.

版本,你需要做如下:

ProcessBuilder pb = new ProcessBuilder("ps", "-A");
Process p = pb.start();

BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
// Skip first (header) line: "  PID TTY          TIME CMD"
in.readLine();

// Extract process IDs from lines of output
// e.g. "  146 ?        00:03:45 pdflush"
List<String> runningProcessIds = new ArrayList<String>();
for (String line = in.readLine(); line != null; line = in.readLine()) {
    runningProcessIds.add(line.trim().split("\\s+")[0]);
}



我不知道有什么方法可以捕获退出代码输出。

I don't know of any way that you could capture the exit code or output.

这篇关于如何等待进程在java或clojure中结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆