1

For example if i have a pool of 10 threads and these threads go out and start different processes (perl scripts), is there a way that those threads can "check up" on those scripts to see how they're doing?

Sometimes some of the scripts freeze up and I have no way of knowing. So i've been thinking of a way to have the threads check on the scripts every once in a while so I can be notified when a script is hung up so that i can start figuring out why they are hanging up and fix the problem.

rage
  • 1,777
  • 5
  • 25
  • 36
  • 1
    Knowing if a program is "hung-up" is an undecidable (can't be solved) problem called "The stop problem". Maybe you want to monitor something a bit more specific? – cgledezma Jun 25 '13 at 13:33
  • Well my idea was to check how long the process had been running for. If it had been running longer than X hours i would take some action – rage Jun 25 '13 at 13:34
  • As my professor in compilers asked me when I proposed that: "and if you wait a bit longer?" – cgledezma Jun 25 '13 at 13:40
  • Scripts outputting to standard out or standard error and your java program does not reading the output could be the reason your scripts are hanging. [link](http://stackoverflow.com/questions/3285408/java-processbuilder-resultant-process-hangs) – Michael Krussel Jun 25 '13 at 16:32

2 Answers2

4

For example if i have a pool of 10 threads and these threads go out and start different processes (perl scripts), is there a way that those threads can "check up" on those scripts to see how they're doing?

The only way that I know of to do this is if the script itself output some sort of status message every X seconds to standard-out or error and the thread that spawned the script was actively reading, waiting for the output. It then could update status information about the process.

If you use the ProcessBuilder and call start() to get a Process, then you can attach a BufferedReader to the process.getOutputStream() to monitor the script. You certainly can also call process.exitValue() to see if the process has finished but that won't tell you if it is hung.

Alternatively would be for the script to somehow call back to the monitoring process via a socket or some other IPC but I think just monitoring the output-stream is the best approach.

Gray
  • 115,027
  • 24
  • 293
  • 354
  • I think checking `process.exitValue()` is what i'm looking for. I understand that in theory i can never know if a process is hung up or not but, in my case i can be pretty darn sure if after a couple of hours my process has not finished than something is wrong. – rage Jun 25 '13 at 14:05
1

Actually, as I posted in a comment, knowing if a program is "hung-up" is an undecidable (can't be solved) problem called The halting problem. So it is not an option to verify on this.

Some alternate solutions would be to check if the script is still running by calling the isAlive() method on the Thread object that is running the script, or, as was told in other answers, to check for some output that the script might be giving, and interpret it. But by verifying output unfortunately you cannot be sure that the program is "hung-up", you can only know it's current state.

EDIT: If you want to wait a particular time, then you can use the Thread.sleep(long millis) call on the parent, and when it wakes up, check who's alive using, again isAlive(). But this doesn't guarantee either that the program will actually finish

cgledezma
  • 612
  • 6
  • 11
  • What class supports `isAlive()`? You talking about `Thread`? – Gray Jun 25 '13 at 13:49
  • @Gray yes.. it is a method you can call on your `Thread` objects – cgledezma Jun 25 '13 at 13:50
  • 1
    The halting problem has little to do with this. The halting problem states that you cannot decide if an arbitrary algorithm runs forever or not. This problem is different. He deals with specific scripts that could in theory be decided to halt on a given input or not (or really be undecidable) but especially have anticipated runnign times. If "still runs after X" is a sufficient criteria for what he wants to do, well, than it is. – b.buchhold Jun 25 '13 at 13:59
  • @b.buchhold The OP said himself `Sometimes some of the scripts freeze up and I have no way of knowing`, which suggests me that he doesn't have a clear parameter to decide when the script is frozen.. – cgledezma Jun 25 '13 at 14:02
  • For me it is enough to know if after X hours that i need to do something. I can't just let the scripts continue to run because i don't know if they're frozen or not – rage Jun 25 '13 at 14:07
  • @rage Then you can go for the `Thread.sleep(long millis)` and `script.isAlive()` approach :) – cgledezma Jun 25 '13 at 14:08
  • Would the thread that executes the script stay alive as long as the script is still running/frozen ? – rage Jun 25 '13 at 14:10
  • @rage check the documentation on the function you're using to run the script. If the thread waits for the return of that function, then yes. Otherwise you'll have to explicitly wait on the thread for the end of the script in order to keep it alive – cgledezma Jun 25 '13 at 14:20