Link to home
Start Free TrialLog in
Avatar of sganta
sganta

asked on

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
ASKER CERTIFIED SOLUTION
Avatar of liddler
liddler
Flag of Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of manav_mathur
manav_mathur

see if your system supports flock.

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

Manav
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
Avatar of Tintin
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.

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
shlock is based on the lock file itself.

Manav

as I read the docs of shlock, it's useless (means not working) on NFS, not very UNIXisch :-(
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
.. and you have the same problems with links as with files on NFS ..
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
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 ...)
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... )
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