Check if java process is running via DFilter parameter

Posted on 2009-04-17
Last Modified: 2012-06-27
Im running multiple java instances in windows.
Each java process should not be executed/started twice, otherwise it will cause problem on the application.
Is there a way to avoid this?
Im thinking of putting it in to windows services.
Im thinking of using srvinstw.exe but I will need to compile my batch file using Quick Batch File compiler(But I don't have a license though).

By the way, I installed cygwin on my server to run a script that will check if desired java application is already running(ps -efW) however, the result will on display java.exe but the DFilter(name of java) is not reflected, so it seems to be useless.

Any batch script would help.
Question by:banyokhing
    LVL 3

    Expert Comment

    If you have written the program yourself, you might probably integrate it into your Java program.

    Otherwise, just some batch file and create a 'pid' file, and do not run the program when the 'pid' file exists. And delete the 'pid' file after the process return....

    Author Comment

    Actually, that is the type of batch scrip I was looking for.
    If only I can redirect the PID of my batch file in to a file.
    Then from that file, using batch script also, I will check if process is still alive.
    If so, I will not start the script, else, start it.
    LVL 3

    Accepted Solution

    With the limited shell toolkit, the best I can do might be a wrapper batch script.

    Everytime, you run the script, if the 'pid' file (which does not hold any PID, in fact) exists, it skip running.

    Two assumptions made:
    1. Your program will not return to the shell before it finishes running
    2. Nobody kills the batch script during running, for this I means
       a. if someone kill the cmd.exe running the script, it will terminate your program and the batch script
       b. if someone shutdown the computer
       c. if above 2 occurs, the pid file might not be cleaned up even if the process is no longer running

    Possible workaround on 2:
    run another batch file in task scheduler every time system boots to clean up the pid files... before your program run

    Attached code snippet to be put into a simple batch file:

    Alternatively, you can write your own wrapper program with C/C++/Java/other language to really do pid recording and checking....... but this will involve more effort
    if exist (
    	echo "Exists"
    	goto fin
    ) else (
    	echo "hold" >
    	java -version

    Open in new window


    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    Suggested Solutions

    A quick step-by-step overview of installing and configuring Carbonite Server Backup.
    Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
    Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
    This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now