• Post Reply Bookmark Topic Watch Topic
  • New Topic

How can I check if the proccess is in memory (both Linux and M$)  RSS feed

 
Ivan Jouikov
Ranch Hand
Posts: 269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
My goal is to make an app that checks every 5 minutes if a given program is running... If its not, the app would run it...
The last part seems to be pretty easy: Runtime.GetRuntime().exec("start my program")
but how would I go for checking whether the proccess is loaded or not?
Thank you very much!
Ivan
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's no way to ask any operating system "Is this Java program running or not?" What you need to do instead is to use "breadcrumbs:" a running copy of the program has to give some signal that it's alive. There are quite a few ways to do this: you can open a server socket at a known port. Then the "watcher" can try to bind another server socket to that port; if it fails, it can assume the program is running -- or send a message and check the reply to confirm it.
Another one: the running program can create a file at a known location; when the program exits, it should remove the file. The watcher can then just check for the presence of the file.
Either of these can be made fairly foolproof if you're careful.
 
Ivan Jouikov
Ranch Hand
Posts: 269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well.. you see, the thing is that the program that's running isn't a java program.. just a normal program (C++ I believe)...
And I do not have access to the source code or anything...
So when that thing crashes, it's quite unpleasant to all the clients that r using it... so how would I do it in that case...
Now that you mentioned sockets.. the program is in fact a server... so maybe what I could do is try to establish a connection, and if it fails, then I can assume the program is down?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes indeed, that sounds like an implementation of the first idea above. You probably want to do your connection attempts in a separate thread that you can interrupt, so the main watcher program doesn't get "stuck".
If the only instances of the program will be launched by the watcher, then you could use Process.waitFor() to wait until the program exits before launching a new one. You do need to be careful; if the program crashes instantly upon launching, then you don't want to get into an infinite loop of launching and crashing and launching. You also need to be careful because the port the server was listening on may not become available for reuse immediately; depending on what socket options the C program uses, there may be a delay while the OS waits for incoming packets even after the process has exited.
 
VIJAY Yadlapati
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using Java you can't do any thing related with Operating System Intenals. Wrire code for that in C/C++ and use that code in Java using JNI.
 
muthu muruges
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We can do it in JAVA. Use setReuseAddress (SO_REUSEADDR) to set such options for the server socket.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!