views:

229

answers:

3

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:

  1. Start each executable as an independent process
  2. 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.

A: 

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

@user314967: not downvoting you because you're new but your answer doesn't answer the question at all. If you think shell scripting should be used instead of Java (which isn't clear from your answer btw), then make it a comment. Answers on SO are supposed to, well, answer the OP's question. Oh, and answers of the type "Google Is Your Friend" are **really not** welcome on SO. SO is a place to find answers, not link to answers.
Webinator
+3  A: 

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.

Steen
The first sentence on their site says it all: "Executing external processes from Java is a well-known problem area." Indeed! Looks very promising, thanks!
recipriversexclusion
A: 

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.

user unknown