The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Need to run same script multiple times in parallel rahman_riyaz Shell Programming and Scripting 2 08-15-2009 11:19 PM
How to avoid multiple while loop? sharif Shell Programming and Scripting 2 01-24-2009 10:09 AM
Running same script multiple times concurrently... ckhowe Shell Programming and Scripting 4 12-12-2007 06:39 PM
running multiple rsh command in a script lweegp Shell Programming and Scripting 0 10-31-2006 02:37 AM
Running a script on multiple machines nattynatty UNIX for Advanced & Expert Users 8 07-07-2003 05:43 PM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-22-2009
edenCC edenCC is offline
Registered User
  
 

Join Date: Sep 2009
Posts: 4
Avoid script running multiple times by filelock

Sometimes we need a single instance of a script to run at a time. Meaning, the script itself should detects whether any instances of himself are still running and act accordingly.

When multiple instances of one script running, it’s easy to cause problems. I’ve ever seen that about 350 instances of a status checking script running there without doing anything, but eat lots of system resource.


It's simple to implement it in Bash Shell:


Code:
# This is to examine whether the lockfile existed
[ -f "${0}.lock" ] && exit -1
# Create the lock file
lockfile ${0}.lock
sleep 40
# Release the lock file manually
rm -f ${0}.lock


Last edited by Neo; 10-24-2009 at 01:34 PM.. Reason: code tags - deleted links to blog
  #2 (permalink)  
Old 10-22-2009
zaxxon's Avatar
zaxxon zaxxon is offline Forum Staff  
Moderator
  
 

Join Date: Sep 2007
Location: Germany
Posts: 2,289
To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags [code] and [/code] by hand.)

Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums
  #3 (permalink)  
Old 10-22-2009
ripat ripat is offline Forum Advisor  
Registered User
  
 

Join Date: Oct 2006
Location: Belgium
Posts: 438

Code:
# This is to examine whether the lockfile existed
[ -f "${0}.lock" ] && exit -1
# Create the lock file
lockfile ${0}.lock
sleep 40
# Release the lock file manually
rm -f ${0}.lock

The first test is superfluous as the lockfile command will "wait" untill the lock is released by the other process. By default it will retry forever every 8 seconds (default sleeptime). If you want the script to exit instead of waiting, just define retry 0.

Example, first process:

Code:
lockfile /tmp/lock; sleep 10; rm -f /tmp/lock

Second process will wait if first process is busy

Code:
lockfile /tmp/lock && echo "Finaly, I'am running..."; rm -f /tmp/lock

Second process abort if first one is busy:

Code:
lockfile -r0 /tmp/lock && echo "Finaly, I'am running..."; rm -f /tmp/lock

  #4 (permalink)  
Old 10-22-2009
edenCC edenCC is offline
Registered User
  
 

Join Date: Sep 2009
Posts: 4
(Hi, Mod, thanks for your note, I'd pay attention next time)
  #5 (permalink)  
Old 10-23-2009
edenCC edenCC is offline
Registered User
  
 

Join Date: Sep 2009
Posts: 4
Thanks, Ripat, nice examples, so the code can be eased like this


Code:
# Create the lock file
lockfile -r0 ${0}.lock && {
     # Your code goes here!
}
# Release the lock file manually
rm -f ${0}.lock



---------- Post updated at 10:12 PM ---------- Previous update was at 10:12 AM ----------

Here's an alternative way to lock the file:



Code:
lock_on()
{
    local f=$1
    local freefd=6  ## do not use fd 5

    ## make sure the file be there
    mkdir -p "$( dirname $f )"
    touch "$f"

    ## find a free fd
    while (( freefd <= 9 )); do
        [[ -L /dev/fd/$freefd ]] || break
        (( freefd++ ))
    done

    (( freefd == 10 )) && return 1

    ## open the lock file
    eval "exec $freefd< \"$f\""
}

is_locked()
{
    local f=$1

    fuser "$f" &> /dev/null
}

lock="/tmp/.$( basename $0 ).lock"
is_locked "$lock" && exit 1
lock_on "$lock"
## do something here

Reply

Bookmarks

Tags
admon, filelock, perl, python

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 02:52 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0