Lock file creation


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Lock file creation
# 1  
Old 05-01-2013
Lock file creation

Hi,

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

Code:
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
# 2  
Old 05-01-2013
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.

Last edited by zaxxon; 05-01-2013 at 11:35 AM..
This User Gave Thanks to zaxxon For This Post:
# 3  
Old 05-01-2013
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:
Code:
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.
This User Gave Thanks to Don Cragun For This Post:
# 4  
Old 05-01-2013
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.
Code:
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

# 5  
Old 05-01-2013
Quote:
Originally Posted by hanson44
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.
Code:
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.)
These 2 Users Gave Thanks to Don Cragun For This Post:
# 6  
Old 05-01-2013
Oops. You're right. Totally got that wrong. Smilie

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

With the required else back in:
Code:
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

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Lock file creates with '?'

Hi, I am trying to create a lock file with the following code but for some reason after file is created it has wrong name "PASP?.lock??" Please let us know how to get rid of these '??' from file name and from where they are coming? #!/bin/ksh... (6 Replies)
Discussion started by: sandy162
6 Replies

2. UNIX for Advanced & Expert Users

Testing privileges -lock lockfile /var/lock/subsys/..- Permission denied

Hi all, I have to test some user priviliges. The goal is to be sure that an unauthorized user can't restart some modules (ssh, mysql etc...). I'm trying to automate it with a shell script but in same cases I got the syslog broadcast message. Is there any way to simply get a return code... (3 Replies)
Discussion started by: Dedalus
3 Replies

3. Red Hat

Security Question: Lock after invalid login, Session Lock and Required Minimum Password Length

Hello all, If anyone has time, I have a few questions: How do I do the following in Linux. We are using Red Hat and Oracle Enterprise Linux, which is based on Red Hat too. 1. How to lock the account after a few (like 3) invalid password attempts? 2. How do you lock a screen after 30... (1 Reply)
Discussion started by: nstarz
1 Replies

4. UNIX for Advanced & Expert Users

file lock

I have an Essbase installation on Solaris 10 and need to get the backups configured. Unfortunately several key files are locked and Essbase (OLAP application) is not releasing the locks when the Essbase or the applications within stop running. It appears I can use chmod to unlock the files but I... (0 Replies)
Discussion started by: JavaBrian
0 Replies

5. Programming

Atomic lock file creation

Hello, I need to implement a locking system in C. My problem is how to make the check if the lock file exist and locking it atomic operation. I want to make something like this: FILE* lock_fname; lock_fname = fopen ( "file.lock", "r"); /*check if file exsists*/ if (lock_fname) { fclose... (7 Replies)
Discussion started by: tsurko
7 Replies

6. Shell Programming and Scripting

Lock a file from being deleted?

Hi In my script, users have the option to delete files from a directory, however, I don't want them to be able to delete the automatically generated log file. Is there anyway to lock a file from being deleted? Note: The file can't be read only as it has to be written to quite frequently. ... (3 Replies)
Discussion started by: Darren Taylor
3 Replies

7. UNIX for Dummies Questions & Answers

Lock File

Hi, We have a lock file being created called lck8c0001 created in Unixware 2.1.2. This is locking a printer. According to some websites, 8c0001 relates to the device name. How does one link 8c0001 to those devices listed in the /dev folder? I have done a ps -lp for all printers and have... (4 Replies)
Discussion started by: canman
4 Replies

8. UNIX for Dummies Questions & Answers

how to lock keyboard without using lock command

how can I lock my keyboard while I'm away from the computer without using lock command. What other commands gives me the option to lock keyboard device? thanks (7 Replies)
Discussion started by: dianayun
7 Replies

9. UNIX for Dummies Questions & Answers

lock file!

I found a lock file like this lrwxrwxr-x 1 sskb apollo 16 Oct 22 22:00 lock -> hostname:2747 (pl. note that hostname is a number like 123.4.5.6) but this was not shown in the file manager eventhough I had selected to show the hidden files. I could not even read the... (4 Replies)
Discussion started by: sskb
4 Replies
Login or Register to Ask a Question