• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Execute Java program only if it is not currently running

 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
Martin Lira
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Martin Lira
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
please buy my thing and then I'll have more money:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic