I would like to run a cron job. Scheduling it is easy and works.
My problem is that I don't know how to only have one version of itself running at a time.
Any ideas?
G-Man
|
|
|
My apologies to embobo, but I copied his script and modified it to eliminate the race condition that I noted in my comment to his answer:
Basically, we are counting on the fact that directory creation is atomic, so that only one copy of this script running concurrently can successfully create |
|||
|
|
|
You could use a lock file. Example shell code:
It is important to make sure that the lock file gets removed at any kind of exit (normal or abnormal). That is what the You could make it more sophisticated by doing more if the lock file exists. For example, get the pid from the file and see if it is actually running. |
|||||
|
|
Just have your script touch a file when it starts and delete that file when it completes. Then within the script, you can check for the existence of that file before doing anything. In pseudocode:
|
|||
|
|
|
You can do the above where you create a lock file or directory. I personally open a socket connection to some high port. This ensures that even if the application crashes the socket will be closed. With a lock file the file can continue and your script will not run till you manually remove it. In my experience this has proven to be more reliable. |
|||
|
|
There is a "lockfile" binary installed on most systems that you can use to avoid getting into the game of bash scripting oneupsmanship. On RHEL/CentOS/Fedora its part of the procmail package. So you could put something like: There's more options in the man page, you can set it to wait some amount of time, to clean up after itself, etc. |
|||
|
|