溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

shell腳本怎么實(shí)現(xiàn)實(shí)時(shí)檢測(cè)文件變更

發(fā)布時(shí)間:2021-08-02 16:39:57 來(lái)源:億速云 閱讀:152 作者:chen 欄目:開(kāi)發(fā)技術(shù)

這篇文章主要講解了“shell腳本怎么實(shí)現(xiàn)實(shí)時(shí)檢測(cè)文件變更”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“shell腳本怎么實(shí)現(xiàn)實(shí)時(shí)檢測(cè)文件變更”吧!

使用python做web開(kāi)發(fā),現(xiàn)在流行使用uwsgi調(diào)用python程序,但是使用uwsgi一段時(shí)間發(fā)現(xiàn)有一個(gè)弊端,就是每次更改源代碼后必須重啟uwsgi才能生效,包括更改模板文件也是,我是個(gè)懶人,再經(jīng)過(guò)一段時(shí)間反復(fù)的更改-重啟后我終于忍受不了,決定寫(xiě)一個(gè)腳本來(lái)定時(shí)程序目錄的文件改動(dòng),并及時(shí)自動(dòng)重啟uwsgi,來(lái)解放我的雙手可以不用理會(huì)這些瑣碎的重啟工作. 用了點(diǎn)時(shí)間來(lái)編寫(xiě)了一個(gè)腳本用來(lái)判斷是否更改,然后判斷是否需要重啟uwsgi.

下面放出腳本內(nèi)容:

#!/bin/bash
# Author   : cold
# Filename  : checkchange.sh
# Useage   : sh checkchange.sh [dir]
checkisdir()
    # Have one argument
    # The argument is a directory
    for i in `ls $1 | sed -e 's/ /\n/g'`
    do
        if [ -d $1/$i ]
        then
            if [ $i == "bin" -o $i == "lib" -o $i == "include" ]  # 不想檢測(cè)的目錄(這里是使用virtualenv生成的環(huán)境文件)
            then
                continue
            fi
            dir="$1/$i"
            checkisdir $dir
        else
            files=$files'\n'$1'/'$i
        fi
    done
    echo -e $files
}
while true
do
    if [ -e /tmp/stat.tmp ]
    then
        for i in `checkisdir $1`
        do
            if [ -e /tmp/patch.tmp ]
            then
                stat $i | grep Change > /tmp/nstat.tmp
                rm -f /tmp/patch.tmp
                continue
            fi
            stat $i | grep Change >> /tmp/nstat.tmp
        done
        diff /tmp/stat.tmp /tmp/nstat.tmp > /tmp/patch.tmp
        if [ $? -eq 0 ]
        then
            sleep 10
        else
            /etc/init.d/uwsgi.py restart          # 將此處更改為想要做的操作
            patch /tmp/stat.tmp /tmp/patch.tmp
        fi
    else
        for i in `checkisdir $1`
        do
            stat $i | grep Change >> /tmp/stat.tmp
        done
        continue
    fi
done

這里主要測(cè)試變更后重啟uwsgi,使用方法:我的bottle程序在/code/python下:

代碼如下:


sh checkchange.sh /code/python &

如果使用svn可以參考下面代碼:

#!/bin/bash
# Author    : cold
# Filename   : checkupdate.sh
# Describle   : To Check update of svn

while true
do
    cd /code/python
    svn up | grep At > /dev/null 2>&1
    if [ $? -eq 0 ]
    then
        sleep 30
    fi

    svn up | grep Updated > /dev/null 2>&1
    if [ $? -eq 0 ]
    then
        /etc/init.d/uwsgi.py restart
    fi
done

感謝各位的閱讀,以上就是“shell腳本怎么實(shí)現(xiàn)實(shí)時(shí)檢測(cè)文件變更”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)shell腳本怎么實(shí)現(xiàn)實(shí)時(shí)檢測(cè)文件變更這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI