Skip to main content

Posts

Showing posts with the label lock

Bash script lock file

Heres the lock file mechanism that I use for some of my bash scripts. I check if the lock file is older than a reasonable time for the script to execute completely just in case the script or the machine running it crashed in the middle of its execution and the lock file hangs the process forever... #!/bin/bash #Check if the lockfile exists and is older than one day (1440 minutes) minutes=1441 LOCKFILE=/tmp/extract.lock if [ -f $LOCKFILE ]; then     echo "Lockfile Exists"     filestr=`find $LOCKFILE -mmin +$minutes -print`     if [ "$filestr" = "" ]; then         echo "Lockfile is not older than $minutes minutes, exiting!"         exit 1     else         echo "Lockfile is older than $minutes minutes, ignoring it and proceeding normal execution!"         rm $LOCKFILE     fi fi touch $LOCKFILE ##Do your stuff here rm $LOCKFILE exit 0 Another approach is to stor...