Avoid script running multiple times by filelock


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Avoid script running multiple times by filelock
# 1  
Old 10-22-2009
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  
Old 10-22-2009
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  
Old 10-22-2009
(Hi, Mod, thanks for your note, I'd pay attention next time)
# 4  
Old 10-22-2009
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

# 5  
Old 10-23-2009
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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Avoid running unnecessary repetitive ps command

i have the following code: APIDS=$(echo $(ps -ef | awk -v gpid="${gpid}" '$2 == gpid || $3 == gpid {print $2,$3}') | sed 's~ ~|~g') AllProcs=$(ps -ef | awk -v allpids="${APIDS}" '$2 ~ allpids || $3 ~ allpids {print $0}' | sed '/^$/d') it seems the above... (6 Replies)
Discussion started by: SkySmart
6 Replies

2. Shell Programming and Scripting

How to check same process running 10 times?

At run time Without knowing job name how to check the job running in specific user "ABCD" ,If the running job duplicate more then 10 then script it self send alert message to the users with the process ID name so that will kill the processed to avoid hung issue ,tried below script please check and... (15 Replies)
Discussion started by: Kalia
15 Replies

3. Shell Programming and Scripting

How to avoid running duplicated task?

Hi Gurus, I have requirement to run different task based on input value. the sample file as below: file1 (contains code need to be run) code aaa1 aaa2 bbb ccc ddd file2 (contains all codes and job name) code job1 job2 aaa1, job_aa1, job_a2 aaa2, job_aa2, job_a2 aaa3,... (5 Replies)
Discussion started by: ken6503
5 Replies

4. Shell Programming and Scripting

Running a program multiple times to search pattern and assign structure

Hi all, I have a big file (n.txt) with following pattern: ATOM 1 N SER A 1 122.392 152.261 138.190 1.00 0.00 N ATOM 2 CA SER A 1 122.726 151.241 139.183 1.00 0.00 C TER ENDMDL ATOM 1 N SER A 1 114.207 142.287 135.439 1.00 0.00 ... (3 Replies)
Discussion started by: bioinfo
3 Replies

5. Shell Programming and Scripting

Running Multiple Times ?

hey mates, I was wondering if someone could assist me with this one, I have couple scripts that I would like to invoke from .sh ( One after another ) Script1 Script2 Script3 Is it possible to run script 2 after script one finished running ? And again start script 3 after script 2... (6 Replies)
Discussion started by: NDxiak
6 Replies

6. Shell Programming and Scripting

call a passwd file to a script multiple times

Hello everybody, I have a requirement in my script.. When i'am executing a script, it'll ask a passwd of some service account.. I need to pass it to the script through a zipped file when it asks for it. The script can be executed by more people many number times. So for securty purpose, it... (1 Reply)
Discussion started by: raghu.iv85
1 Replies

7. Shell Programming and Scripting

Need to run same script multiple times in parallel

Hi all, I have a requirement in which a script invokes a Java program. Lets say script ABC invokes a java program with cfg file a parameter. This script takes 10 minutes to execute . Like this ineed to run the program 10 times meaning 100 minutes if i do it sequentially. If i open... (2 Replies)
Discussion started by: rahman_riyaz
2 Replies

8. Shell Programming and Scripting

How to avoid multiple while loop?

Hi, How can I avoid multiple 'cat while read ....? in my script. In my script, I am taking the inputs from the temp text file and doing the ( cat while read input do ... ... done ) task and deleting later. I know it'll raise the perfomance issue. How to avoid this? (2 Replies)
Discussion started by: sharif
2 Replies

9. Shell Programming and Scripting

Running same script multiple times concurrently...

Hi, I was hoping someone would be able to help me out. I've got a Python script that I need to run 60 times concurrently (with the number added as an argument each time) via nightly cron. I figured that this would work: 30 1 * * * for i in $(seq 0 59); do $i \&; done However, it seems to... (4 Replies)
Discussion started by: ckhowe
4 Replies

10. UNIX for Advanced & Expert Users

Running a script on multiple machines

To clear the web cache on my web server, I run this command: find $APACHE_HOME/cache/plsql/plsql -type d -name "*" -exec rm -R {} \; To clear the cache on all the web servers(we have 4), I log on to any one machine, clear its cache, ssh to another machine, clear cache etc; Is there any way... (8 Replies)
Discussion started by: nattynatty
8 Replies
Login or Register to Ask a Question