防止shell指令碼重複執行的代碼_linux shell

來源:互聯網
上載者:User

例如,要求指令碼只能順序訪問某個資源,例如磁碟檔案等,就可以參考下面的實現。

複製代碼 代碼如下:

#!/bin/bash
#
# file locking using bash.
# ver 0.1.6
#
# author : malundao ( malundao@sina.com )
# date   : 2011-08-31  
# ref    : http://unix.derkeiler.com/Newsgroups/comp.unix.shell/2005-09/0472.html
#
# note:
#   shflock_cleanhook() is a user defined function to clean up user-specific sth.
#
# /path/to/lock/. note; directory, not a file.
# should be modified
LOCKPATH="/tmp"
cleanup() {
        shflock_cleanhook
        cd $LOCKPATH
        [ -e lock.pid ] || exit
        read pid >/dev/null 2>&1 <lock.pid
        if [ -n "$pid" ]; then
            if [ "$pid" == "$$" ]; then
                rm -f lock.$pid
                rm -f lock.pid
                exit
            fi 
        fi 
        exit
}
#  trap EXIT ?
trap 'cleanup' HUP INT TERM
getlock() {
        oldpath=`pwd`
        cd $LOCKPATH
        while
                echo $$ > lock.$$
                [ -e lock.pid ]
        do
                rm lock.$$
                read pid >/dev/null 2>&1 <lock.pid
                if [ -n "$pid" ]; then
                        if [ -e /proc/$pid ]; then
                                cd $oldpath
                                return 1 # Lock is taken by others
                        else
                                #unsafe: rm -f lock.$pid
                                echo WARN: please delete stale lock.pid by HAND.
                                return 2
                        fi
                else
                        # sleep some seconds,then back to 'while' loop
                        # 11 is a prime number, $$ as a random. 
                        echo sleep $(( $$ % 11 ))
                        sleep $(( $$ % 11 ))
                fi
        done
        # 'ln -s' is an atom op.
        ln -s lock.$$ lock.pid >/dev/null 2>&1
        if [ $? -eq 0 ]; then
                cd $oldpath
                return 0 # We got the lock
        else
                [ -e lock.pid ] || echo WARN: please delete hanging lock.pid by HAND.
                cd $oldpath
                return 3 # Lock is probably taken by others.
        fi
}
putlock () {
    oldpath=`pwd`
    cd $LOCKPATH && rm -f lock.$$ lock.pid
    cd $oldpath
}
## test
shflock_cleanhook() {
    echo i\'m a hook.
}
while true; do
    while ! getlock; do
        #echo wait a second...
        sleep 1
    done
    echo \[$$\] `date` ,now hold lock for 3 seconds...
    sleep 3
    #echo putlock
    putlock
    sleep 1 # yield
done

防止指令碼多次執行(一)
複製代碼 代碼如下:

while [ `ps x |grep -v grep|grep filename.sh|wc -l` -gt 0 ];
do
        exit
done

把內容放在指令碼最上面

filename.sh 替換為你要監控的指令碼名字
還有一種方法是 死迴圈 可以根據不同的環境,不同的指令碼內容,使用的方法也同步,方法很簡單日後在寫出來

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.