shell脚本怎么实现实时检测文件变更

发布时间:2021-08-02 16:39:57 作者:chen
来源:亿速云 阅读:140

这篇文章主要讲解了“shell脚本怎么实现实时检测文件变更”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“shell脚本怎么实现实时检测文件变更”吧!

使用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 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

这里主要测试变更后重启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脚本怎么实现实时检测文件变更”的内容了,经过本文的学习后,相信大家对shell脚本怎么实现实时检测文件变更这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

推荐阅读:
  1. 怎么在html5中实现input输入实时检测
  2. shell脚本实现自动化下载并检测ftp文件备份

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

shell

上一篇:Python中怎么拆分字符串入到字典里面

下一篇:linux、unix下如何快速清空文件

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》