How to check/compare lock file?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to check/compare lock file?
# 1  
Old 05-17-2014
How to check/compare lock file?

Hi all,

I have a shell script that I schedule to run from 0700 - 1900, every 15minutes. For each run it creates a lock file and remove it on exit.

While there is a bit of a flaw to it where the lock file don't get remove if someone kills the script while it is running, I am fairly satisfied with it. No one should be killing the script on purpose anyway unless the server itself rebooted and the script of course did not run its course where it is supposed to remove the lock file on exit.

Can someone advise how I can somehow check the timestamp of the lock file? For example, if the lock file is 30 or 45 minutes old, I assume the script may have been killed in which case instead of exiting because it exists, I want to remove it and continue the run of the script.

BTW, OS is Solaris 9.

Any advise much appreciated. Thanks in advance.

Last edited by newbie_01; 05-17-2014 at 07:59 AM.. Reason: Include OS information
# 2  
Old 05-17-2014
Code:
find /lock/file/location -type f -name "name.luck" -mmin -45 -exec rm -f {} \;

# 3  
Old 05-17-2014
Eons ago I ended up writing C code to wrap execution of arbitrary commands with lock management just for cases like this. Meaning, I needed a way to coordinate the use of various scripts, tools, etc... that didn't already have a means of serializing their runs.

A couple of years ago I re-wrote that program and a few others and put them up on Github for anyone to grab. But I don't think you need to bother looking for packages to compile for what you want anymore. I just checked and found a command called "flock" that's already installed on my Ubuntu system that does the same sorts of things. So check "man flock" and see if it does what you want.

The main feature I think you need is for your "lockfile" to actually use file locking calls to coordinate access. In other words, the mere existence of the lockfile isn't enough to stop execution. What should block execution of another instance of the command is if you can't establish an exclusive lock on the file. And even if the previous command died without removing the lockfile, everything will just work. In fact you don't even need to remove the lockfile at all.
# 4  
Old 05-17-2014
Instead of just removing the lock file, you might want to determine whether or not the script is still running.

A fairly common way to do this is to have the script that creates the lock file writes its PID into the lock file (echo $$ > lock). Then if the lock file is present when it isn't expected, you can cat the lock file and look for that PID in the output from ps to determine if the lock owner is still running or died before removing the lock file.

Even easier than using ps, you can use something like:
Code:
if kill -0 $(cat lock)
then    echo 'lock owner still running'
else    echo 'lock owner is gone'
fi

as long as the process that created the lock and the process running the above code are running with the same UID and permissions.

Last edited by Don Cragun; 05-17-2014 at 09:40 PM.. Reason: Add ps alternative.
This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 05-17-2014
Use the script itself as the lock file:

Code:
#!/bin/bash

for pid in `fuser $0 2> /dev/null`; do
    if [ $pid -ne $$ ]; then
        echo "Script is already running, exiting..."
        exit 1
    fi
done

...

# 6  
Old 05-18-2014
Here's a quick demonstration how "flock" can be used to coordinate serialized execution of a command. When you enter the following,

Code:
iter=0
while [ ${iter} -lt 10 ]
do
    flock -w 30 /var/tmp/test-flock.lock -c 'sleep 4; date' &
    iter="$(( ${iter} + 1 ))"
done

will start up 10 "flock" commands in parallel. But they will only run their respective payloads "sleep 4; date" when they can get an exclusive lock on the file "/var/tmp/test-flock.lock".

As a result, they will be serialized and run one at a time. You can tell that's the case because the time echo'd to your terminal will be 4 seconds apart.

Also, since each "flock" will only wait 30 second to get the exclusive lock, two of them will timeout and NOT run the "sleep 4; date" commands. And since it's using actual filesystem lock calls, you don't need to create or remove the lockfile. If it doesn't exist, it will be created for you. And there's no need to remove either.
# 7  
Old 05-18-2014
Also, handling common signals might help you to do this automatically.
However, off-course its not possible when someone kills the script with kill -9 (SIGKILL) which is used quite often when its not necessary. Unfortunately it cant be trapped.

But, for other common signals, e.g. ctrl+c, you can do something like..

Code:
trap clean_jobs SIGHUP SIGINT SIGTERM



Code:
function clean_jobs {
  #rm locks
  #do other cleanup stuffs
}


Last edited by clx; 05-18-2014 at 03:42 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Check for particular files and compare the file names

Hi, Below are the 2 files in directory /tmp: masterCSF242323.img indexCSF242323.img 1) I want to compare if both the number (242323) are same in both the files. If they are same print - Files matching, else print files do not match. 2) Also if only index file is present in that... (7 Replies)
Discussion started by: apatil65
7 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. Shell Programming and Scripting

Check and compare disk space and email it

I am very new to Linux and learning to script. This is for one of my servers at work that I have to keep track off as far as disk space and how it is used. I have tried to go line by line but little things keep chewing me up. I would appreciate any and all help or advice, and Mutt is installed on... (3 Replies)
Discussion started by: sgtjkj
3 Replies

4. 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

5. 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

6. Shell Programming and Scripting

Check File Exists and compare to previous day file script

We have data files that are ftp'd every morning to a SUN server. The file names are exactly the same except for that each has the date included in its name. I have to write script to do 2 things: STEP 1) Verify that the file arrived in morning. STEP 2) Compare the file size of the current... (3 Replies)
Discussion started by: rbknisely
3 Replies

7. UNIX for Dummies Questions & Answers

Need Script to check file exist and compare

I need a script that will check for the existence of new files that FTP'd in the morning, results go to log file. The 2nd step is to compare the new file with the previous days file. If the new file size is 30% or more smaller in size then previous day this needs to also be sent to log. This... (1 Reply)
Discussion started by: rbknisely
1 Replies

8. 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

9. 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

10. 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