How do i lock a ksh shell script?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How do i lock a ksh shell script?
# 1  
Old 07-07-2011
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 script so that only 1 user can access it at a time.
If 2 or more users access it, then bad things happen when 2 users and trying to drop and create the same database at the same time, or trying to create the same text file at the same time.

I would also like for the user to wait till the shell script is unlocked, instead of just passing over it or ending the application.

please help!!

thanks!!
# 2  
Old 07-07-2011
have the script issue a "lock file"...either touch or write to a file that it alone knows about (ie, formatted file name), and then the script would check for this file upon launch. If the lock file is present, then someone else is running...or was, if not set it, process and then remove it. Secondary scripts can use a while [ -f $touch_file ] ;then sleep x; fi...and then run once it clears out.

Be sure that your Admins can identify the lock file externally in case it might fail mid-process, and would need to be deleted. Also, it might not hurt to use a file name format that allows for a random or session-based component so that each script launch would have its own lock file to manage, but one that could still be seen by other runs of the script.
# 3  
Old 07-07-2011
One way...

Maybe someone else will have a more elegant idea, but here's a way that comes to mind.

At the top of your script test for the presence of a file which indicates the script is running. If it is, loop, checking every 5 minutes or whatever until the file does not exist.

If it does not exist, then create it to indicate we are running and remove it at the end.

You could also write a row to a table in the database which logically does the same thing.

Partial code to show the logic:
Code:
#!/bin/ksh

#  Define constants
typeset -r LOCKFILE=/tmp/${0}.lock
typeset -ir LOCKWAITTIME=300   # seconds

# Remove the lock file if the script is interrupted before it ends normally.
trap 'rm $LOCKFILE' 0 2 3 9 15  # 0=normal exit

#  If lockfile exists, wait for it to go away.
if [[ -f $LOCKFILE ]]; then
     print "Waiting...\c"
     while [[ -f $LOCKFILE ]]
     do
       sleep $LOCKWAITTIME
       print ".\c"
     done
fi
# LOCKFILE does not exist so create it.
print "$0: locked by $(logname)" > $LOCKFILE

#script body here

exit 0

This example loops forever, you would most likely want to put a timeout in there I suppose that would exit after maybe 2 or 3 LOCKWAITTIME iterations. Also if more than one process is waiting there could be trouble if they both see the lock file go away at the same time.

Gary

Last edited by gary_w; 07-07-2011 at 02:21 PM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

2. Shell Programming and Scripting

Help with ksh Shell Script

My goal is to create a script that will check if in a test or production environment. I wrote this script to check $host variable to check which server I'm on but this script does not work. if then BASE=/home/fmtest; export BASE else BASE=/home/fmprod; export BASE fi ... (5 Replies)
Discussion started by: Bperl1967
5 Replies

3. Shell Programming and Scripting

Ksh Shell Script Error

Hi, While running below code i am getting error as below 0403-004 Specify a parameter with this command. Please look it below code and let me know about if condition. Code is As below #!/usr/bin/ksh Infa_Src_Dir=$DIR/SrcFiles/pp Load_Info_Lst_Path=$DIR/SrcFiles/pp... (1 Reply)
Discussion started by: samadhanpatil
1 Replies

4. Shell Programming and Scripting

Help with KSH Shell Script

From a shell script I'm trying to remove the first two files of whats returned from the head -2 command so I tried piping it to xargs rm -f but I can't get it to work. How do I remove the files from the head command? ls -al *clit* *servr* |sort -t_ -nk2 | head -2 |xargs rm -f (3 Replies)
Discussion started by: Bperl1967
3 Replies

5. Shell Programming and Scripting

Help with ksh shell script

Anyone know how to check a filename that contains a date and compare whiich file is older using a ksh shell script? The filename looks like aaaaa_20110615 (1 Reply)
Discussion started by: Bperl1967
1 Replies

6. Shell Programming and Scripting

How can I execute another shell from my ksh script?

I am newbie in UNIX, so please excuse me for the stupid question.:) Here is a problem: I created ksh script where the part of the functionality include an opening of a second session with another shell process "runrep"(runrep is a custom reporting shell designed by Advent Geneva). When I run my... (3 Replies)
Discussion started by: alexstar
3 Replies

7. Shell Programming and Scripting

what does this ksh shell script do?

Can someone tell me when the script is called, what does it do? I can't see it is going to run anything. (1 Reply)
Discussion started by: dp100022
1 Replies

8. Shell Programming and Scripting

How to write a directory lock shell script?

Hi there, pleas I want this script urgently. how to lock a directory by shell script? (12 Replies)
Discussion started by: joneggk
12 Replies

9. Shell Programming and Scripting

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... (3 Replies)
Discussion started by: kum5256
3 Replies

10. Shell Programming and Scripting

Help with ksh shell script

I am using /usr/bin/ksh in AIX I am reading the values of $dbname, $dbatmpdir/dbdir.$$, and $scope from a different file All I have to do is check if $dbname exists in file $dbatmpdir/dbdir.$$ and $scope should have a value either 'TABLE' or 'SCHEMA'. When I execute the following code. I am... (3 Replies)
Discussion started by: tenderfoot
3 Replies
Login or Register to Ask a Question