加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

shell – 使inotifywait将多个文件更新组合成一个?

发布时间:2020-12-15 21:46:07 所属栏目:安全 来源:网络整理
导读:我有一个文件夹,其中包含我使用inotifywait(从 inotify-tools开始)观看的Sphinx文档.该脚本重新构建了html singlehtml并刷新Chrome. #!/bin/shinotifywait -mr source --exclude _build -e close_write -e create -e delete -e move | while read file event
我有一个文件夹,其中包含我使用inotifywait(从 inotify-tools开始)观看的Sphinx文档.该脚本重新构建了html& singlehtml并刷新Chrome.

#!/bin/sh
inotifywait -mr source --exclude _build -e close_write -e create -e delete -e move | while read file event; do
    make html singlehtml
    xdotool search --name Chromium key --window %@ F5
done

保存单个文件时,此工作正常.但是,当我更新旧版本或粘贴源文件夹中的多个文件时,它会为每个文件触发脚本.

是否有一个简单的解决方法(没有编写自定义python脚本 – 我可以这样做)让它在启动脚本之前等待几分之一秒?

解决方法

我制作了一个更复杂的shell脚本并在 the article中发布了它:

inotifywait -mr source --exclude _build -e close_write -e create -e delete -e move --format '%w %e %T' --timefmt '%H%M%S' | while read file event tm; do
    current=$(date +'%H%M%S')
    delta=`expr $current - $tm`
    if [ $delta -lt 2 -a $delta -gt -2 ] ; then
        sleep 1  # sleep 1 set to let file operations end
        make html singlehtml
        xdotool search --name Chromium key --window %@ F5
    fi
done

它使inotifywait不仅可以记录文件名和文件名.行动,但也是时间戳.该脚本将时间戳与当前的unixtime进行比较,如果delta小于2秒,则运行make html.但在此之前它会睡1秒钟让文件操作系统结束.对于下一个修改过的文件,时间戳将是旧的,增量将超过2秒,并且不会执行任何操作.

我发现这种方式消耗的CPU最少且最可靠.

我也试过运行一个简单的Python脚本,但这意味着如果我将一些像jQueryUI一样大的东西粘贴到文件夹中,就会产生一千个进程然后变成僵尸.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读