Shell quiz: implement a mutex


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell quiz: implement a mutex
# 1  
Old 05-20-2009
Shell quiz: implement a mutex

How would you make sure that a shell script (or a portion thereof) does not run (it waits or it terminates with error) when another instance of it is already running?
# 2  
Old 05-21-2009
The simplest way would be to use a lock file - let the contents of a text file act as a traffic cop.
# 3  
Old 05-21-2009
Quote:
Originally Posted by jim mcnamara
The simplest way would be to use a lock file - let the contents of a text file act as a traffic cop.
Hmmm... contents of the file?

How would you make sure that, in between the point in time you check the file and the point in time you set the lock, no other process does the same resulting in two processes running?
# 4  
Old 05-21-2009
I'd use the presence or absence of an interlock file and record your shell's process id in the file.
Create the file with /usr/bin/touch for speed and refer to the full hierarchial filename throughout the script (i.e. avoid directory searches). After creating the file append your shell's process id. Read back the file and check the process id to be 100% sure that your instance holds the interlock before processing, otherwise back off.
# 5  
Old 05-21-2009
I like using 'fuser' - making sure there's only ONE pid 'locking' the script.

One problem with this method is someone may be running "cat <script>" and
fuser will show this . So you might have to check the processes that "fuser"
returns and make sure they are indeed running the script.

Something along these lines:
Code:
#!/bin/ksh

thisFILE="$(whence ${0})"
progName="${0##*/}"

     myPID="$$"

     FUSERout=$(fuser ${thisFILE} 2>/dev/null)
     typeset -i numProc=$(echo "${FUSERout}" | nawk '{print NF}')
     if [[ "${numProc}" -gt 1 ]]; then
        echo "${progName}: another instance(s) of [${thisFILE}] is currently still running [$(echo ${FUSERout} | sed -e 's/  */ /g')] - exiting THIS ${myPID}] run."
        exit 1
     fi

# 6  
Old 05-21-2009
Quote:
Originally Posted by methyl
After creating the file append your shell's process id. Read back the file and check the process id to be 100% sure that your instance holds the interlock before processing, otherwise back off.
Depends on what you mean for "back off".

Suppose that while you write the pid another process is doing the same. This results in the other pid appearing inside the file before or after your pid, the position does not matter here.
Then you check and see another pid:
  • You wait, leaving in place your pid. Deadlock: both processes waiting for the other pid to disappear.
  • You delete your pid (not a trivial task) and wait for some interval, then you write your pid again and check. The other process is doing the same, therefore it is likely that both processes again see another pid and have to wait again. A pseudo-random wait interval would take care of the problem... it reminds me of Ethernet collision detection. It would perhaps work, but it seems just too involved.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

Ok, now I got it: the serialization protocol dictates that the pid appearing in line 1 of the interlock file is the one whose process is allowed to go on, while the other processes are inside a wait-and-check loop (well, perhaps it is smarter to just wait $pid1 where pid1 is read from line 1); when the process ends, it removes its pid; then another pid is in line 1... and so on, until the pid queue is empty.

Not bad. Still a bit involved though.

You have to be absolutely sure that the script cannot end without having removed its pid from the queue. An exit handler implemented with trap would be enough, but a SIGKILL would bypass the trap.

Last edited by colemar; 05-21-2009 at 11:36 AM.. Reason: Solved
# 7  
Old 05-21-2009
Quote:
Originally Posted by vgersh99
I like using 'fuser' - making sure there's only ONE pid 'locking' the script.
I like using fuser too, but:
  • it works only when the processes to serialize are instances of the exact same script file
  • it is not very portable
  • as you said, any process reading the script file would block all the script instances (perhaps forever)
On the plus side, the lock is automatically removed when the process ends or is terminated in any way.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Multiple choice quiz im shell not working

Hi, I have to create a quiz im form of bash-shell-script. The problem is: I don't find a way to bring multiple choice questions to work. As long as someone selects only one of the possible answers everything is fine, but if one selects two or more options it doesn't work. The code I used is... (4 Replies)
Discussion started by: Naky
4 Replies

2. UNIX for Dummies Questions & Answers

Implement the '&&' function in a shell

Hello, I'm currently implementing the && function in a shell using C. For example, if we input cmd1 && cmd2, then cmd2 executes only when cmd1 exits successfully. I'm thinking about: int main() { int i; char **args; while(1) { printf("yongfeng's shell:~$ "); args =... (5 Replies)
Discussion started by: Yongfeng
5 Replies

3. Shell Programming and Scripting

Need to implement one check in shell script

Hi, I am facing one issue.I am working on one script where I want to implement one check PSS/TFM/Coupon/trunk/CouponProject/CouponService/src/test/java/com/travelport/service/pss/coupon/validator/CouponValidationReqValidatorTest.java Now my problem is or requirement is if the above... (1 Reply)
Discussion started by: anuragpgtgerman
1 Replies

4. Shell Programming and Scripting

How to implement the logical express --- A(B+C) within single "if" statment in shell script?

On Linux OS, bash environment, I want implement the following code: if && ( || ) A,B,C represents some compare conditions. How to realize it ? Thanks! (1 Reply)
Discussion started by: qcmao
1 Replies

5. Programming

Shell programing to implement SQL package

hi all, i have a table with employee details like his name,addr,DOB etc. i need a shell program which takes one date as input from user and print the employees whose DOB is less than that date. in sql package the select query is written (2 Replies)
Discussion started by: vidyaj
2 Replies

6. Shell Programming and Scripting

Select query implement in a shell

I have been asked to create a shell script that accepts a number of SQL select queries as input, runs them in sequence, spools the output to an EXCEL workbook, where, each worksheet is an output of a Select statement run above. The workbook should be in a .XLS format. If a particular select... (2 Replies)
Discussion started by: ShellNovice1
2 Replies

7. Shell Programming and Scripting

Shell quiz: emulate an associative array

Most shells flavors do not have associative arrays a.k.a. maps. How would you emulate an associative array? I had this problem once and found a working solution, but I don't want to spoil the game hence I wont tell it. Wonder if anyone comes up with something better. (5 Replies)
Discussion started by: colemar
5 Replies

8. Shell Programming and Scripting

batch file not running after implement dummy shell

Hi, I want to ask if anyone has any ideas, why the batch file i created to transfer files on my desktop doesn't run after i put this line in /etc/passwd and any idea to overcome it ? Thanks user:!:xxx:xxx::/home/user:/usr/local/bin/ssh-dummy-shell (2 Replies)
Discussion started by: DarReNz
2 Replies

9. Shell Programming and Scripting

mutex in shell programing

A shell in crontab per 5 min write a file B shell in crontab per 6 min read a file how to lock the share file a ;avioid confilict in write and read? Thx : -) (1 Reply)
Discussion started by: zz_xm
1 Replies
Login or Register to Ask a Question