Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

locking mechanism

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 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 Unix and Linux 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 Unix and Linux 09-23-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 3 December 2016, 11:52 AM EST
Location: NM
Posts: 10,875
Thanks: 457
Thanked 985 Times in 916 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 Unix and Linux 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 Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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 01:43 PM.