使用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 directoryfor i in `ls $1 | sed -e "s/ /
/g"`doif [ -d $1/$i ]thenif [ $i == "bin" -o $i == "lib" -o $i == "include" ]# 不想检测的目录(这里是使用virtualenv生成的环境文件)thencontinuefidir="$1/$i"checkisdir $direlsefiles=$files"
"$1"/"$ifidoneecho -e $files}while truedoif [ -e /tmp/stat.tmp ]thenfor i in `checkisdir $1`doif [ -e /tmp/patch.tmp ]thenstat $i | grep Change > /tmp/nstat.tmprm -f /tmp/patch.tmpcontinuefistat $i | grep Change >> /tmp/nstat.tmpdonediff /tmp/stat.tmp /tmp/nstat.tmp > /tmp/patch.tmpif [ $? -eq 0 ]thensleep 10else/etc/init.d/uwsgi.py restart# 将此处更改为想要做的操作patch /tmp/stat.tmp /tmp/patch.tmpfielsefor i in `checkisdir $1`dostat $i | grep Change >> /tmp/stat.tmpdonecontinuefidone
这里主要测试变更后重启uwsgi,使用方法:我的bottle程序在/code/python下:
复制代码 代码如下:
sh checkchange.sh /code/python &
如果使用svn可以参考下面代码:
#!/bin/bash# Author: cold# Filename : checkupdate.sh# Describle : To Check update of svnwhile truedocd /code/pythonsvn up | grep At > /dev/null 2>&1if [ $? -eq 0 ]thensleep 30fisvn up | grep Updated > /dev/null 2>&1if [ $? -eq 0 ]then/etc/init.d/uwsgi.py restartfidone