![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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 |
|
||||
|
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 |
|
||||
|
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
|
![]() |
| Bookmarks |
| Tags |
| admon, filelock, perl, python |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|