Applying lock on a file in Unix Ksh


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Applying lock on a file in Unix Ksh
# 1  
Old 06-24-2009
Applying lock on a file in Unix Ksh

Hi,

How can we apply lock on a text file through Unix Ksh script. I did found a command flock (file descriptor) but am not very acquainted with the usage.

Can anybody tell me if I need to use Flock command for applying locks to a file while writing on it. If the person can explain the usage with an example, that will be really great.

If this is not the right command, can anyone let me know what command shall i use.
# 2  
Old 06-24-2009
Code:
lockfile:-
This utility is part of the procmail package (www.procmail.org). It creates a lock file, a semaphorefile that controls access to a file, device, or resource. The lock file serves as a flag that this
particular file, device, or resource is in use by a process (it is "busy"), and this permits only restricted
access (or no access) to other processes.
lockfile /home/bozo/lockfiles/$0.lock
# Creates a write-protected lockfile prefixed with the name of the script.
Lock files are used in such applications as protecting system mail folders from simultaneously being
changed by multiple users, indicating that a modem port is being accessed, and showing that an
instance of Netscape is using its cache. Scripts may check for the existence of a lock file created by a
certain process to check if that process is running. Note that if a script attempts to create a lock file
that already exists, the script will likely hang.
Normally, applications create and check for lock files in the /var/lock directory.
A script can test for the presence of a lock file by something like the following.
appname=xyzip
# Application "xyzip" created lock file "/var/lock/xyzip.lock".
if [ -e "/var/lock/$appname.lock" ]
then #+ Prevent other programs & scripts
# from accessing files/resources used by xyzip.
...


flock:-

Much less useful than the lockfile command is flock. It sets an "advisory" lock on a file and then
executes a command while the lock is on. This is to prevent any other process from setting a lock on
that file until completion of the specified command.
flock $0 cat $0 > lockfile__$0
# Set a lock on the script the above line appears in,
#+ while listing the script to stdout.
Unlike lockfile, flock does not automatically create a lock file.

# 3  
Old 07-06-2009
Thanks Ahmad!!!!!

I will test it and will inform you if it worked.
# 4  
Old 07-06-2009
Here is a pointer to a good discusion on the issue: create/remove a lock file
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Ksh: How to write the log file simultaneously when .sql file executes by UNIX process

Hello Team- we would like to implement an approach which has to write the log file simultaneously when .sql file is executing by Unix process. At present,it is writing the log file once the process is completed. I've tested the current process with the below approaches and none of them... (1 Reply)
Discussion started by: Hima_B
1 Replies

2. Shell Programming and Scripting

Applying the same awk over a directory of files with individual file output

I am trying to apply an awk action over multiple files in a directory. It is a simple action, I want to print out the 1st 2 columns (i.e. $1 and $2) in each tab-separated document and output the result in a new file *.pp This is the awk that I have come up with so far, which is not giving me a... (6 Replies)
Discussion started by: owwow14
6 Replies

3. Shell Programming and Scripting

Applying sed against a file from a list of values stored in a variable

Hi Forum. I have the following challenge at work that I need to write a script for. I have a file abc.txt with the following contents: 4560123456 4570987654 4580654321 I want to be able to search/replace in abc.txt - the first 4 characters anything starting with 4560 to 7777; 4570... (3 Replies)
Discussion started by: pchang
3 Replies

4. Ubuntu

How to lock a file through UNIX KSH shell script?

I wrote two shell scripts in UNIX that renames the same file and scheduled them at the same time. The following are the steps that I followed:- 1. I wrote 2 scripts named s1.sh and s2.sh, both trying to add “exec_” prefix to the name of the files present in a folder i which already don't start... (4 Replies)
Discussion started by: piuli
4 Replies

5. UNIX for Dummies Questions & Answers

Applying python on all the files in one folder --UNIX

Dear all, I know this might be a simple and silly question but I am struggling with it I have a long unix script and in a section of if I want to use a python script which should select all the files and run the script on it (as a whole) the loop I have is selecting file by file and I dont know... (2 Replies)
Discussion started by: A-V
2 Replies

6. UNIX for Dummies Questions & Answers

applying unix command in EBCIDIC file

Hi, I'm having a general query. If we do cat <file name> on a Ebcidic format file then many unknown characters are displayed in my screen. Can we change the character set related to EBCIDIC in session level and apply the cat command on a EBCIDIC file? By doing so can we able to see the... (2 Replies)
Discussion started by: poova
2 Replies

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

8. UNIX for Dummies Questions & Answers

How do i lock a ksh shell script?

Hi, I have a ksh shell script that accesses databases to drop and create tables and the script also creates text files. This shell script is accessed thru a java application that i would like to turn multi-user, but the only way that i can do that is if I can figure out a way to lock the shell... (2 Replies)
Discussion started by: ndedhia1
2 Replies

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

10. UNIX for Dummies Questions & Answers

How to lock a file in unix?

We wish to keep a sequence number in a file. When someone wants to get the next sequence number we need to lock the file, get the next number and increment it by one. How do you do that? I know how to get the number and increment it but how do I lock the file and test that it is locked or not... (1 Reply)
Discussion started by: tammy_schmuki
1 Replies
Login or Register to Ask a Question