如何知道在*nix上在Java代码(或C或Python)中启动和终止进程

时间:2021-10-22 22:48:52

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:

我需要在Linux上编写一个处理任务的过程控制器模块,每个任务都由多个可执行文件组成。控制器的输入是一个XML文件,其中包含每个可执行文件的路径和要传递给每个可执行文件的命令行参数列表。我需要实现以下功能:

  1. Start each executable as an independent process
  2. 将每个可执行文件作为一个独立的过程启动
  3. Be able to kill any of the child processes created, independent of the others
  4. 能够杀死创建的任何子进程,独立于其他进程

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.

为了实现(2),我认为我需要在创建进程时捕获pid,以发出系统杀伤命令。我尝试使用ProcessBuilder访问Java中的pid,但没有找到简单的方法。

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.

我所有的其他逻辑(在DB中放置关于任务的信息等)都是用Java完成的,所以我想继续使用它,但是如果有解决方案,您可以在C、c++或Python中建议,我也很感激。

3 个解决方案

#1


3  

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.

对于Java解决方案,您应该查看apache commons exec库。他们做了很多工作使它与平*立,他们有一个很棒的教程。

In python, you can use the included subprocess library.

在python中,您可以使用包含的子进程库。

#2


0  

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.

您确实需要在谷歌上查找“shell脚本”。特别是如果你的雇主/讲师希望你在linux上工作,处理流程等。

Maybe start here: http://supportweb.cs.bham.ac.uk/documentation/tutorials/docsystem/build/tutorials/unixscripting/unixscripting.html

也许从这里开始:http://supportweb.cs.bham.ac.uk/documentation/tutorials/docsystem/build/tutorials/unixscripting/unixscripting.html

#3


0  

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

我不确定,但是,如果你开始从Java可执行文件,您可以在独立的线程开始,然后你就可以将它们映射然而你想要的名字,通过行号,并停止封闭线程经常作为Java线程,它似乎没有一个优雅的解决方案(没有关闭文件,等等),但可以努力一些扩展(只要linux-program不启动流程,摆脱其母)。

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?

关闭每个进程的特定命令,通过stdin发送到程序,可能是另一个选项。如何处理stdin和stdout以及其他陷阱,本文将详细介绍:http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html?

Visible programs can even be controlled by java.awt.Robot (keyboard, mouse).

可见程序甚至可以被java.awt控制。机器人(键盘、鼠标)。

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.

作为最后一个想法,我将考虑使用一个新的命令“kill pidof program”,该命令基于名称进行工作,因此您无法区分相同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.

我不知道Steen提到的apache-lib,但通常有非常有用的东西,我建议你也去看看——也许是在一开始。

#1


3  

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.

对于Java解决方案,您应该查看apache commons exec库。他们做了很多工作使它与平*立,他们有一个很棒的教程。

In python, you can use the included subprocess library.

在python中,您可以使用包含的子进程库。

#2


0  

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.

您确实需要在谷歌上查找“shell脚本”。特别是如果你的雇主/讲师希望你在linux上工作,处理流程等。

Maybe start here: http://supportweb.cs.bham.ac.uk/documentation/tutorials/docsystem/build/tutorials/unixscripting/unixscripting.html

也许从这里开始:http://supportweb.cs.bham.ac.uk/documentation/tutorials/docsystem/build/tutorials/unixscripting/unixscripting.html

#3


0  

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

我不确定,但是,如果你开始从Java可执行文件,您可以在独立的线程开始,然后你就可以将它们映射然而你想要的名字,通过行号,并停止封闭线程经常作为Java线程,它似乎没有一个优雅的解决方案(没有关闭文件,等等),但可以努力一些扩展(只要linux-program不启动流程,摆脱其母)。

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?

关闭每个进程的特定命令,通过stdin发送到程序,可能是另一个选项。如何处理stdin和stdout以及其他陷阱,本文将详细介绍:http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html?

Visible programs can even be controlled by java.awt.Robot (keyboard, mouse).

可见程序甚至可以被java.awt控制。机器人(键盘、鼠标)。

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.

作为最后一个想法,我将考虑使用一个新的命令“kill pidof program”,该命令基于名称进行工作,因此您无法区分相同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.

我不知道Steen提到的apache-lib,但通常有非常有用的东西,我建议你也去看看——也许是在一开始。