Lock file creation

Hi,

Please let me know how these steps are creating a lock file using echo " ".


LOCK_FILE=${LOG_DIR}/${DBNAME}_MD.lock

# create lock file
if [ -f ${LOCK_FILE} ]
then
	echo "Another process is running already. Will terminate this one." >> ${LOG_FILE} 2>&1
	echo "If the lock file is not needed, please remove ${LOCK_FILE} in tmp directory and reschedule job in cron." >> ${LOG_FILE} 2>&1
	exit 0
else
 	echo "" > ${LOCK_FILE}
fi


Thanks
Sandy

I am not sure if I got the problem. Anyway, I try:

This is called redirection. echo produces output to Standard Out (stdout). It is redirected by the > and just creates the file if it does not exist already (which is controlled by the test above). The name "Lock"-file just describes it's purpose in the script. It a plain normal file.

1 Like

I added line numbers to your code for the purposes of this discussion. Obviously, the line numbers cannot appear in the code that you run:

1  LOCK_FILE=${LOG_DIR}/${DBNAME}_MD.lock
2
3  # create lock file
4  if [ -f ${LOCK_FILE} ]
5  then
6  	echo "Another process is running already. Will terminate this one." >> ${LOG_FILE} 2>&1
7  	echo "If the lock file is not needed, please remove ${LOCK_FILE} in tmp directory and reschedule job in cron." >> ${LOG_FILE} 2>&1
8  	exit 0
9  else
10  	echo "" > ${LOCK_FILE}
11 fi

Line 1 sets the name of the lock file that you will be using.
Line 4 tests whether or not there is a regular file with the name of your lock file. If there is a file with that name, lines 6, 7, and 8 append messages to your log file saying that another copy of your program is already running and exits. If there is not a file with that name, line 10 creates a file with that name and continues with the rest of your program.

Presumably, something else in your program will remove this lock file after it completes processing your database. The removal will probably be close to the end of your program.

1 Like

I rewrote the script some to make more sense, as best I can tell.

The script does not create the lock file, because the -f test already proved the lockfile previously existed. So there is no point using echo > $LOCKFILE command, unless there is some special meaning to clearing the lock file.

Some other code segment must create the lock file in the first place. Your code segment just tests for the existence of the lock file, and takes action if it exists. The 2>&1 has no point here. It doesn't hurt, but is just extra.

A log file usually likes having a date for each entry.

LOCK_FILE="$LOG_DIR/${DBNAME}_MD.lock"

# check for lock file

if [ -f "$LOCK_FILE" ]; then
  date >> "$LOG_FILE"
  echo "Lock file already exists: $LOCK_FILE" >> "$LOG_FILE"
  echo "Another process is running already. Will terminate this one." >> "$LOG_FILE"
  echo "If lock file not needed, please remove and reschedule job in cron." >> "$LOG_FILE"
  exit 0
fi

Sorry, but I must strenuously disagree with your analysis and your rewrite of the code. If you look at the originally posted script (and my analysis of it in the message #3 in this thread) you will see that the else side of the if statement does indeed create the lock file if it didn't already exist. (It is true that there is a race condition that could allow two or more copies of the script to run since the test for the existence of the lock file and the creation of the lock file is not an atomic operation, but the original script at least tries to create the lock file if it didn't exist.)

With this setup, there is no meaning to "clearing the lock file", it is the mere existence of the lock file that that indicates that some other process is running in this "protected region". When I use lock files like this, I prefer to set the contents of the lock file to be the process ID of the process that is setting the lock. This makes it easier to find out if the process that set a lock is still running when a later attempt to set the lock fails. (But, the OP didn't ask for a better version of the code; just an explanation of what it was doing.)

2 Likes

Oops. You're right. Totally got that wrong. :o

---------- Post updated at 03:17 PM ---------- Previous update was at 02:30 PM ----------

With the required else back in:

LOCK_FILE="$LOG_DIR/${DBNAME}_MD.lock"

# check for lock file

if [ -f "$LOCK_FILE" ]; then
  date >> "$LOG_FILE"
  echo "Lock file already exists: $LOCK_FILE" >> "$LOG_FILE"
  echo "Another process is running already. Will terminate this one." >> "$LOG_FILE"
  echo "If lock file not needed, please remove and reschedule job in cron." >> "$LOG_FILE"
  exit 0
else
  echo > "$LOCK_FILE"
fi