Shell script command to lock the file

Hi ,

My requirement is to access the specific file at one at a time.

I would like to lock the file using shell script command.

Please advice me if there is any command ..

Thanks in Advance.
Regards
Sarat
LVL 4
sgantaAsked:
Who is Participating?
 
liddlerConnect With a Mentor Commented:
The usual way to do this is:
Check for a flag,
if flag exists do nothing
if flag does not exist
create flag
access file
then remove flag

i.e.
#!/bin/ksh
if [ -f /tmp/flag ]
then
exit
fi
touch /tmp/flag
.
.
run commands you want on file
.
.
rm /tmp/flag
0
 
manav_mathurCommented:
see if your system supports flock.

Manav
0
 
manav_mathurCommented:
or fcntl, if your files are spread over NFS.

Manav
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
manav_mathurCommented:
and what liddler has suggested is very close to a concept being used widely in OS today - the mutex semaphore. Which stands for mutual-exclusion semaphores.

But liddler, in this case, you have not elaborated much on contention-problems and equivalence. We still dont know how many processes might be trying to test-and-set on the file in quesiton at a particular point of time.

Manav
0
 
TintinCommented:
You could consider using shlock.

http://flinflon.brandonu.ca/Dueck/1997/62306/UNIX/shlock.html

*However*, every locking scheme is dependent on everyone coming to the party, ie: whatever locking mechanism you use must be the same one used by all processes accessing the file.

0
 
ahoffmannConnect With a Mentor Commented:
AFAIK there is no reliable "locking" from within any shell
If you need the lock for your shell itself only, then use your own ".lock" file as semaphore.
0
 
manav_mathurCommented:
shlock is based on the lock file itself.

Manav

0
 
ahoffmannCommented:
as I read the docs of shlock, it's useless (means not working) on NFS, not very UNIXisch :-(
0
 
cjjcliffordConnect With a Mentor Commented:
take care with lock files, the approach outlined above using "if [ ! -f ]" and touch is not atomic.
However, a very simple way is to use symbolic links - the creation of a symbolic link is atomic, so try and create a symbolic link, if creation is successful you've got the lock - just remember to delete the link when you are finished (setting a trap to delete this is probably the safest...)

Also, using the PID of the process in the symlink is a good way of debugging if there are problems with locks being left after scripts complete...

e.g.

LOCKFILE=/tmp/mylock

# $$ is the PID of the script
if ln -s $$ $LOCKFILE; then
    echo locked
    # do some work
    rm $LOCKFILE
fi
0
 
ahoffmannCommented:
.. and you have the same problems with links as with files on NFS ..
0
 
cjjcliffordCommented:
ah yes, NFS is a pain alright... if you have to lock over NFS you might be better using a language that has a file locking interface, since file locks are managed only for a single process (e.g. python can use the standard fcntl() call to lock a file).

Cheers,
Conor
0
 
ahoffmannCommented:
hmm, even the language does not help anyway ion NFS :-(
IMHO the only reliable way is to use a private (sphisticated) .lock file as semaphore (I'm repeating ...)
0
 
cjjcliffordCommented:
fcntl() can do locking over NFS, but it does rely on how the NFS is setup (different systems have different "quirks"... e.g., AFAIK, to allow full locking with Solaris (8) as the NFS client, the NFS server software has to be running on the Solaris client box also... )
0
 
zdesCommented:
mkdir should be an atomic operation even over NFS. I used that as a filesystem semaphor without any problems. Cannot claim completeness of my tests, but see a similar discussion on CVS lists: http://lists.gnu.org/archive/html/info-cvs/2003-10/msg00064.html
0
All Courses

From novice to tech pro — start learning today.