linux(和OSX)shell命令在每次保存文件时执行

时间:2011-08-05 21:39:16

标签: linux file macos shell

我正在尝试让jsdoc在我保存javascript文件时自动生成。我有一个工作脚本存储文件的最后更新时间(当前是硬编码的),并与该文件的当前时间戳进行比较。我在while循环中运行它,直到按下CTRL-C,然后插入0.1秒的睡眠以停止处理器被消耗。

这是工作脚本:

while :
do

if [ $(( lastTime )) -ne `stat -f %m -t %s javascript.js` ]
then
        lastTime=`stat -f %m -t %s javascript.js`
        # custom jsdoc generating script
        jsdoc javascript.js
        echo +++ Run: `date`  +++
fi

# stops while loop from consuming a lot of resources
# and making my fan whirr like he wants the computer to take off
sleep .1

done

我知道有更好的方法 - 不是那种方式。任何帮助表示赞赏。

编辑:安装了inotify-tools的linux机器的更新应该可以正常工作

#!/bin/bash

# with inotify-tools installed...

# only watches first parameter for modification
while inotifywait -e modify $1; do
        echo
        echo +++ Building JSDocs +++
        jsdoc $@
        echo +++ Last run: `date`  +++
done

但是,我希望这适用于Linux和OSX shell,因此我可以在两种环境中使用

1 个答案:

答案 0 :(得分:3)

有一个名为INotify的Linux内核功能可以监视文件系统是否有任何更改。它作为许多系统API公开。

对于脚本,有一个名为inotify-tools的包,它提供了对通知系统的脚本访问。