How to lock a file through UNIX KSH shell script?


 
Thread Tools Search this Thread
Operating Systems Linux Ubuntu How to lock a file through UNIX KSH shell script?
# 1  
Old 07-16-2014
Display 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 with “exec_”. When any script performs the renaming operation, then that step is noted in their respective log file.
For e.g., if script s1.sh tries to rename f1 file, then the message “f1 has been renamed to exec_f1” along with the time of the action is written in log1 file.
3. I scheduled both the scripts at the same time with the following 2 commands :-
a. at -f s1.sh 5:36 PM
b. at -f s2.sh 5:36 PM
The output that we saw was that in both log1 and log2 files same actions were logged at the same time. So, both the scripts accessed the same file at the same time.


Now what I want to do is if suppose s1.sh has already accessed a file called f1 then s2.sh will not access it and will take the next file say f2.

Can anyone please tell me how to do that?
# 2  
Old 07-16-2014
Why did you schedule both scripts to run at the exact same time? Is there a reason for that?

If the script(s) only operate on files NOT starting "exec" then once the rename is done, the second script won't find those files.

What command(s) are you using to rename the file(s)? mv?

Please post the script.
# 3  
Old 07-16-2014
I assume this is a test case/prototype for something more complicated? Exactly what do you need to achieve? It's simpler to stop a duplicate script from running rather than trying to lock files individually.

Quote:
Originally Posted by hicksd8
If the script(s) only operate on files NOT starting "exec" then once the rename is done, the second script won't find those files.
If the scripts are using a for ls/find loop then they may well get the same (or very similar, depending on exact scheduling) list of files to operate on, since the renames (or most of them) won't have happened yet.
# 4  
Old 07-16-2014
@CarloM......yes, but, even if both scripts identify the same list of files to operate on only one script can actually do the renaming and the second script should throw an error that the file has disappeared.
# 5  
Old 07-16-2014
If they are both doing the same thing, then are they really two scripts? If this is a common function that they would both do, then perhaps it would be better to break that out into a separate function and organise your scheduler a little differently.

If you have Job1 & Job2 currently running in parallel, consider having Job0 to do the common renaming work and then make the remainder of Job1 & Job2 as a dependency on Job0 completing. I suppose it depends on your scheduler.

If it's cron, then it's a little tricky. Commercial schedulers should provide this job dependency control. if you are stuck with cron then consider using some sort of a file to flag that Job0 has completed and have Job1 & Job2 wait for it to appear.

As hicksd8 requests, Please post the script.
As CarloM asks though, what do you really need to achieve?



Robin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Execute a shell script in UNIX m/c from win 7 using ksh

I need to run a shell script on unix machine from windows 7. I used the "RSH" command in win XP successfully for this, But on win 7 it is not working. Can any body tell me how to run RSH on win 7 or any other command for similar use. I use the command as below rsh <unix m/c> -l <username>... (1 Reply)
Discussion started by: arup1980
1 Replies

2. UNIX for Dummies Questions & Answers

File Inbound/Outbound shell script(ksh)

URGENT ---------- Post updated at 04:26 AM ---------- Previous update was at 04:23 AM ---------- (I could not post I didn't know why so I need to put the contents via reply, sorry) Hi all :D, I am a newbie of Unix shell script and I was assigned the work from user that I need to... (3 Replies)
Discussion started by: gogkub
3 Replies

3. Shell Programming and Scripting

How to add trailer record at the end of the flat file in the unix ksh shell scripting?

Hi, How to add trailer record at the end of the flat file in the unix ksh shell scripting can you please let me know the procedure Regards Srikanth (3 Replies)
Discussion started by: srikanth_sagi
3 Replies

4. Shell Programming and Scripting

how to execute ksh simple shell script without creating .sh file

please help me to execute a simple shell script like for i in `ls echo $i done . i dont want to create a new sh file to execute it. Can i just type and execute it ? because I always this kind of simple for loops . Please help . Thanks (7 Replies)
Discussion started by: Sooraj_Linux
7 Replies

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

6. Shell Programming and Scripting

Unzip the input file using shell script (ksh)

Hi, I need help in unziping input file through shell script. I had written script, which checks for input file extention. If Extension is "zip" or "gz", then I want to do unzip/uncompress that file. Caould you please let me know that, How to unzip a file through shell script (ksh). Thanks... (16 Replies)
Discussion started by: Poonamol
16 Replies

7. Shell Programming and Scripting

Solaris KSH shell script to copy all lines from one file to another

Hello, more of a windows wscript guy. However I took a new position that requires me to support some solaris servers. So... issue is that I need to copy all lines from a file to a temporary file and then copy them back into the original file starting at line 1. Reason I need to do this is... (5 Replies)
Discussion started by: ZigZaggin
5 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. 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