Reputation: 10988
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}
Upvotes: 0
Views: 404
Reputation: 5291
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.
Upvotes: 1
Reputation: 1306
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.
Upvotes: 3