I have a bash script I want to run every 5 minutes from cron… but there’s a chance the previous run of the script isn’t done yet… in this case, i want the new run to just exit. I don’t want to rely on just a lock file in /tmp.. I want to make sure sure the process is actually running before i honor the lock file (or whatever)…
Here is what I have stolen from the internet so far… how do i smarten it up a bit? or is there a completely different way that’s better?
if [ -f /tmp/mylockFile ] ; then
echo 'Script is still running'
else
echo 1 > /tmp/mylockFile
/* Do some stuff */
rm -f /tmp/mylockFile
fi
There is at least one race condition in this script. Don’t use it for a life support system, lol. But it should work fine for your example, because your environment doesn’t start two scripts simultaneously. There are lots of ways to use more atomic locks, but they generally depend on having a particular thing optionally installed, or work differently on NFS, etc…