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