如何了解 *nix 上 Java 代码(或 C 或 Python)中的启动和终止进程
我需要在 Linux 上编写一个处理任务的进程控制器模块,每个任务都由多个可执行文件组成。控制器的输入是一个 XML 文件,其中包含每个可执行文件的路径以及要传递给每个可执行文件的命令行参数列表。我需要实现以下功能:
- 将每个可执行文件作为独立进程启动
- 能够独立于其他进程杀死创建的任何子进程
为了执行 (2),我认为我需要捕获 pid< /code> 当我创建进程时,发出系统 kill
命令。我尝试使用 ProcessBuilder
在 Java 中访问 pid
,但没有找到简单的方法。
我的所有其他逻辑(将有关任务的信息放入数据库等)都是用 Java 完成的,所以我想坚持这一点,但如果您可以用 C、C++ 或 Python 建议解决方案,我将不胜感激,也。
I need to write a process controller module on Linux that handles tasks, which are each made up of multiple executables. The input to the controller is an XML file that contains the path to each executable and list of command line parameters to be passed to each. I need to implement the following functionality:
- Start each executable as an independent process
- Be able to kill any of the child processes created, independent of the others
In order to do (2), I think I need to capture the pid
when I create a process, to issue a system kill
command. I tried to get access to pid
in Java using ProcessBuilder
but saw no easy way to do it.
All my other logic (putting info about the tasks in DB, etc) is done in Java so I'd like to stick with that, but if there are solutions you can suggest in C, C++, or Python I'd appreciate those, too.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于 Java 解决方案,您应该查看 apache commons exec 库。他们做了很多工作来使其独立于平台,并且他们有一个很棒的教程。
在 python 中,您可以使用包含的 subprocess 库。
For a Java solution, you should take a look at the apache commons exec library. They've done a lot of work to make it platform independant and they have a great tutorial.
In python, you can use the included subprocess library.
你真的需要在谷歌上查找“shell 脚本”。特别是如果你的雇主/导师希望你在 Linux 上工作并处理进程等。
也许从这里开始:
http://supportweb.cs。 bham.ac.uk/documentation/tutorials/docsystem/build/tutorials/unixscripting/unixscripting.html
You really really need to look up "shell scripting" on google. Specially if your employer/instructor wants you to work on linux and deal with processes etc.
Maybe start here:
http://supportweb.cs.bham.ac.uk/documentation/tutorials/docsystem/build/tutorials/unixscripting/unixscripting.html
我不确定,但如果你从 Java 启动可执行文件,你可以在单独的线程中启动它们,然后你可以根据需要映射它们 - 按名称、按行号或其他东西 - 并定期停止该封闭线程作为 java-线程,这似乎不是一个优雅的解决方案(不关闭文件等),但可以在某种程度上起作用(只要linux程序不启动进程,该进程将自身从其父进程中释放出来) 。
用于关闭每个进程的特定命令(通过标准输入发送到程序)可能是另一种选择。这里详细介绍了如何处理 stdin 和 stdout 以及其他陷阱:
http://www.javaworld.com/javaworld/ jw-12-2000/jw-1229-traps.html?
可见程序甚至可以通过java.awt.Robot(键盘、鼠标)来控制。
作为最后一个想法,我会考虑使用一个新命令“kill pidof program”,该命令基于名称工作,因此您无法区分同一程序的两个实例。
我不知道 Steen 提到的 apache-lib,但通常有非常有用的东西,我建议也看看那里 - 也许首先。
I'm not sure, but if you start executables from Java, you may start them in seperate threads, and then you can map them however you want - by name, by line number or something - and stop that enclosing thread regularly as java-thread, which doesn't seem to be an elegant solution (not closing files, etc.), but could work to some extend (as long as the linux-program doesn't start a process, which is freeing itself from its parent).
Specific commands for closing each process, send via stdin to the programs, might be another option. How to handle stdin and stdout and other pitfalls are mentioned here in some length:
http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html?
Visible programs can even be controlled by java.awt.Robot (keyboard, mouse).
As a last idea I would consider using a new command "kill pidof program" which working on a name-basis, so you can't distinguish two instances of the same progam.
I don't know the apache-lib, mentioned by Steen, but there is normally very useful stuff, I would recommend to look there too - maybe in the first place.