File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Execute Java program only if it is not currently running Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Execute Java program only if it is not currently running" Watch "Execute Java program only if it is not currently running" New topic
Author

Execute Java program only if it is not currently running

Martin Lira
Ranch Hand

Joined: May 26, 2004
Posts: 97
I have a java app program that will be executed every 5 mins from the windows scheduler. It is run from a .bat file. My problem is, sometimes the app may take more than 5 mins to end. In this situation I dont want the app to start.

Is there a way i can check in my java app if already another instance is running so exit. I checked the windows task manager and it shows entry for java.exe. This does not help as it can be any app that is using the java.exe.

Thanks,
Martin
[ May 17, 2005: Message edited by: Martin Lira ]
Erik Larson
Greenhorn

Joined: Apr 25, 2005
Posts: 10
Not sure how you could do that. But why use Windows Scheduler? Why not write your own scheduler that checks the state of your app every X minutes, and if it is not running, your scheduler spawns a new thread that starts it.


SCJP<br />SCJD - someday!
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12268
    
    1
Sometimed people use a socket to act as a sort of flag that a particular program is running. If you get an error on opening the socket you know that another instance is running.
Bill
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
Why do you not change an environment variable status so that you can know when you program is running or not?

I guess you could even do that from the bat file.
Vasudhaiv Naresh
Ranch Hand

Joined: May 13, 2005
Posts: 57
Setting an environment variable is indeed a good option.
Alternatively a static flag may be used which indicates the status of the application(whether running or not).
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
No, it's not a good option. Neither is using a lockfile.
If the application crashes the environment variable or lockfile won't be cleared preventing the application from being restarted.
Setting a static variable won't help at all as it is invisible to another JVM.

You have to use some resource that goes away when the application goes away. That means a socket in case of Java (in C you might be able to use a pipe of memoryfile).


42
Martin Lira
Ranch Hand

Joined: May 26, 2004
Posts: 97
I was thinking to write to a file "RUNNING" everytime the app starts and "EXIT" after it ends.

Before doing any processing the app reads the file and if the status=RUNNING dont start the new instance. If the status=EXIT start new instance.

However the socket option also sounds good.

Any inputs?

Thanks,
Martin
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 9946
    
    6

i don't know if you read Jeroen's post, but you'll have a problem with the 'running' method if the app (or the OS) crashes... you'll never get that 'EXIT' written, so the app will never restart.


Never ascribe to malice that which can be adequately explained by stupidity.
Martin Lira
Ranch Hand

Joined: May 26, 2004
Posts: 97
Hi,
Can the same problem not occur if you go with the socket route. if the app crashes it wont release the socket. So next time it runs the socket is still alive and wont let the app to start.


~ML
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
My advice would be to drop using the windows scheduler, starting the app just once and use the Timer class for scheduling. Is probably much simpler than anything else you could do.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
M Beck
Ranch Hand

Joined: Jan 14, 2005
Posts: 323
the benefit of sockets is that only running, extant procedures can hold them - if your program dies, the OS will automatically close its open sockets for you. the backdraw of sockets is that they have to be opened on ports, and there are only so many ports available; if some other program on the computer happens to want to use the socket you're using for signalling the scheme falls apart, or at least becomes a good bit more complicated.
[ May 18, 2005: Message edited by: M Beck ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Execute Java program only if it is not currently running
 
Similar Threads
Set classpath problem (Oracle9.01)
Memory Leakage - Is "System.gc()" good to help solve this?
Adding Java App. to Windows Service
java.exe has generated errors
Regarding java.exe and javaw.exe