locking mechanism | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

locking mechanism

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 09-23-2008
tjay83 tjay83 is offline
Registered User
 
Join Date: Jan 2008
Last Activity: 8 November 2008, 6:43 PM EST
Posts: 98
Thanks: 0
Thanked 0 Times in 0 Posts
locking mechanism

I have a shell script.
How can use some kind of locking mechanism to ensure that the script is not being executed by two people
at the same time?
Sponsored Links
    #2  
Old 09-23-2008
aliahsan81 aliahsan81 is offline
Registered User
 
Join Date: Sep 2008
Last Activity: 29 October 2009, 4:19 PM EDT
Posts: 62
Thanks: 0
Thanked 0 Times in 0 Posts
You can use setfacl command to set acl on the file that what user can run it.That what i think maby be some other have better idea
Sponsored Links
    #3  
Old 09-23-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 22 December 2014, 12:57 PM EST
Location: NM
Posts: 10,313
Thanks: 297
Thanked 823 Times in 765 Posts

Code:
#!/bin/ksh

lockit()
{
     [[ -w /tmp/lockfile ]] ||  > /tmp/lockfile
     let count=0
     echo 'checking lock...'
     while [[ -s /tmp/lockfile ]]
     do
           sleep 30           
           count=#(( count  + 1 ))
           if [[ count -eq 10 ]] ; then
                  echo 'cannot obtain lock'
                  exit 1
           fi
     done
      echo "$$" > /tmp/lockfile
     echo 'got lock'
}

unlock()
{
      > /tmp/lockfile

}
# main --------------------------
lockit

trap '> /tmp/lockfile; kill -9 $$ ' EXIT  HUP  INT

.  /path/to/myscript.sh
STATUS=$?

unlock
trap ' ' EXIT
exit $STATUS

Put your scriptname inside this type of script. If your script has trap commands they may override the ones here. This is just meant as a simple starting point.
    #4  
Old 09-23-2008
tjay83 tjay83 is offline
Registered User
 
Join Date: Jan 2008
Last Activity: 8 November 2008, 6:43 PM EST
Posts: 98
Thanks: 0
Thanked 0 Times in 0 Posts
That is really helpful

Thanks a lot
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Best IPC mechanism to be used rupeshkp728 Programming 2 02-12-2012 10:50 AM
An alternative to IPC mechanism helltrex Shell Programming and Scripting 4 05-06-2010 06:04 PM
Open source my OIOIC, a completely new object-oriented mechanism for the C. pervise.zhao Programming 7 04-27-2009 01:40 AM
mechanism of AIX ? anonys AIX 4 01-15-2009 08:48 AM
Mechanism reqd for knowing TCP buffer occupancy level saptarshi Programming 2 06-28-2007 07:53 AM



All times are GMT -4. The time now is 03:24 PM.