i've got a cron job set to call this java app every minute. this app looks in a directory for some files, if it finds the files it's looking for then it does some processing on them. if it doesn't find the files, it quits.
i need a way to prevent other java processes from interferring with this, uh, process. so the initial java app looks to see if a lock file exists in the search directory. if it doesn't then it creates one and stores its PID in it.
why? because if another java process comes in and sees the lock file, it will get the stored PID and look to see if it exists in /proc if it does, it knows to get out, if it doesn't find the PID in /proc, then it knows that it can ignore the lock file, that is, the lockfile is bad (leftover from a process that is no longer running.)
does this give you a reasonable idea what i'm trying to accomplish?
You may open a socket on an used port, and if it fails, (because the port is in use), you don't start.
As far as I know, a terminated programm will always release the lock.
$$ will expand to the PID, so your Programm get's started
for example, and may store the '1642' in a lockfile. Now - if the program terminates abnormal, without removing the lock-file, the next starting will show: there is a lock-file. Your newly started program shall now find out, whether this lockfile is valid: a) is there a process running with this pid? b) is this really your 'myProg'?
a) isn't a common java-task, but you could look at /proc/PID to see if there is a file with this pid. b) could be done in a similar way.
I wouldn't solve it from within java (and of course not with Runtime - but that wasn't the suggestion of James), but from the script. In crontab call the script: .... filemod.sh
in the filemod.sh:
[ July 26, 2004: Message edited by: Stefan Wagner ]
Ah - I made a mistake. If the program tests itself for running, it is of course running, so 2 would be the testing program (plus headline), 3 would mean another instance running, 4 and above: serious problem.
Or you start another script inside the script, which starts the java-program, and query for that in this testscript.
Joined: Jul 26, 2004
thanks for your help. i'll give these suggestions a try.