0

I'm trying to make a little java job that copies files from one folder to another and some database storage and such. The job runs from a script which is executed once each hour. I'd like to have a little check in the java job which can return some value if the job is already running.

Problem is, I have NO idea how to do this. Any suggestions?

Each hour the job is run like the following: "java myjob arg1 arg2". Is it even possible to check if the job that was started 1 hour ago is still running?

EDIT

Just thought of something.. Since each time this job is run it gets a new process id the suggestions below wont work. My job will be run from another script like this:

if ${type} = ${sys}
    then 
        java batchjob ${type} ${dir1} ${dir2}
oers
  • 18,436
  • 13
  • 66
  • 75
Linora
  • 10,418
  • 9
  • 38
  • 49

2 Answers2

3

You can consult the processlist or, even easier, you can create some file each time you launch the job and remove after the job is done, so it is possible to check the existence of the file and know that the process is running (or the process has crashed and the file has not been removed)... Just an idea.

yoprogramo
  • 1,306
  • 9
  • 14
1

Each time your job starts, create a lock file and put job's PID to it (easy on Unix, surely doable on Windows or in Java). To see if the job is still running, get PID from that file and see if it's still within the list of current processes.

That way, you'll be able to tell if the job is really still running (i.e. it didn't crash before removing the lock file).

If your startup script will also remove this file after the job finishes, you'll be also able to tell if the previous run of your job finished successfully (file not there), crashed (file with a non-existent PID still present), or is still running.

david a.
  • 5,283
  • 22
  • 24
  • This, I like very much! - my problem is that I'm developing on a windows machine, but it will be deployed on a unix machine. Do you know if there is a way to check for the processID which will work on both win and unix? – Linora Oct 19 '11 at 11:15
  • I don't know of a way that'd work on both Unix and Windows. When I had to do such a script I developed it on a Linux box. To simulate a unix shell environment on widows, I'd try cygwin, or a virtual Linux box (there are many freely downloadable vmware images). There also seem to be some (perhaps unreliable) ways to get PID in Java: http://stackoverflow.com/questions/35842/process-id-in-java . Good luck. – david a. Oct 19 '11 at 11:39