LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-22-2009, 12:51 AM   #1
edenCC
Member
 
Registered: May 2006
Location: Gz,China
Distribution: RH,FB
Posts: 198
Blog Entries: 1

Rep: Reputation: 32
Avoid script running multiple times by file lock


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.

I create a simple blog post on my website which show some implementation examples by Perl, Python and Shell.

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

An updated version:

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

Last edited by edenCC; 10-22-2009 at 11:09 PM.
 
Old 10-22-2009, 10:58 PM   #2
Jerre Cope
Member
 
Registered: Oct 2003
Location: Texas (central)
Distribution: ubuntu,Slackware,knoppix
Posts: 323

Rep: Reputation: 37
ps -ef | grep -q "myscriptname" | grep -v grep && exit
 
Old 10-22-2009, 11:08 PM   #3
edenCC
Member
 
Registered: May 2006
Location: Gz,China
Distribution: RH,FB
Posts: 198
Blog Entries: 1

Original Poster
Rep: Reputation: 32
Quote:
Originally Posted by Jerre Cope View Post
ps -ef | grep -q "myscriptname" | grep -v grep && exit
It's not cost effective, and will use more resource.
 
Old 10-22-2009, 11:10 PM   #4
edenCC
Member
 
Registered: May 2006
Location: Gz,China
Distribution: RH,FB
Posts: 198
Blog Entries: 1

Original Poster
Rep: Reputation: 32
One of my friends gave me another solution like this:


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
 
Old 10-23-2009, 02:21 AM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
Slight amendment to post #2:

-q = quiet, but also "Exit immediately with zero status if any match is found, even if an error was detected."
http://linux.die.net/man/1/grep
Personally I'd want to know if an error occurred.
The '&& exit' is also redundant.

Otherwise, yes, use ps. The problem with lockfiles is that if a program crashes, the lockfile is not removed(!).
Check for the program actually running, imho.
 
Old 10-23-2009, 03:52 AM   #6
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,036

Rep: Reputation: 96
Another thing to consider is:

If the script that you are checking for is the script you are running from, check the process id and compare it against the value of $$
 
Old 10-23-2009, 07:21 AM   #7
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by edenCC View Post
One of my friends gave me another solution
It's not as good as your solution because of the race condition between
Code:
fuser "$f" &> /dev/null
and
Code:
eval "exec $freefd< \"$f\""
Another instance of the script could use fuser, find no lock and also open the lock file.

Co-operative locking between asynchronous processes requires an atomic operation, that is an operation which is guaranteed not to be interrupted between initiation and completion. AFAIK there are only two such filesystem-related primitives: ln and mkdir.

The existence of procmail's "lockfile" command saves shellscript coders a lot of work in this area! It includes a -l timeout function to set the maximum age of a lockfile -- useful if you can be sure of the maximum time the lock must be held for.

For debugging and sysadmin it is helpful if the process that created a log file writes its ID info into the lockfile. I don't think procmail's lockfile supports that.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
If a script is already running 3 times wait till one finishes before starting another Techno Guy Linux - Newbie 3 04-21-2009 08:58 PM
script to execute a command multiple times snr8fl3 Programming 5 11-17-2008 01:49 PM
Help on editing perl script to avoid file names with colon (:) stelmed Programming 2 12-12-2006 06:17 PM
Caps lock only if pressed multiple times or in a combination bleargh Linux - Software 0 11-27-2004 10:22 PM
running wget multiple times quickk Linux - Newbie 1 09-16-2004 07:53 AM


All times are GMT -5. The time now is 06:00 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration