使用python做web開發,現在流行使用uwsgi調用python程式,但是使用uwsgi一段時間發現有一個弊端,就是每次更改原始碼後必須重啟uwsgi才會生效,包括更改模板檔案也是,我是個懶人,再經過一段時間反覆的更改-重啟後我終於忍受不了,決定寫一個指令碼來定時程式目錄的檔案改動,並及時自動重啟uwsgi,來解放我的雙手可以不用理會這些瑣碎的重啟工作. 用了點時間來編寫了一個指令碼用來判斷是否更改,然後判斷是否需要重啟uwsgi.
下面放出指令碼內容:
 #!/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" ]  # 不想檢測的目錄(這裡是使用virtualenv產生的環境檔案)            then                continue            fi            dir="$1/$i"            checkisdir $dir        else            files=$files'\n'$1'/'$i        fi    done    echo -e $files}while truedo    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    fidone
 
這裡主要測試變更後重啟uwsgi,使用方法:我的bottle程式在/code/python下:
 複製代碼 代碼如下:
 
sh checkchange.sh /code/python &
 
如果使用svn可以參考下面代碼:
 #!/bin/bash# Author    : cold# Filename   : checkupdate.sh# Describle   : To Check update of svnwhile truedo    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    fidone