Doing actions upon changes of files and directories is very useful. Examples like compiling a project after the source code files are changed, sending emails after important configuration files are modified, building the PDF after a TeX file is modified. On Linux, the inotify-tools
provide good support for trigger actions after changes. In this post, I will introduce a small tool do-after-change.sh which continuously runs a command upon certain files and directories change.
对文件和目录的更改进行操作非常有用。 例如更改源代码文件后编译项目 ,修改重要配置文件后发送电子邮件,修改TeX文件后构建PDF等示例。 在Linux上 , inotify-tools
为更改后的触发操作提供了良好的支持。 在本文中,我将介绍一个小工具do-after-change.sh ,该工具会在某些文件和目录更改后连续运行命令。
The script makes use of inotifywait
which is from the inotify-tools
package. On Fedora/RHEL/CentOS/SL, you can install the package by yum install inotify-tools
. You may need to install similar packages on other Linux distros such as Debian/Ubuntu/Linux Mint.
该脚本利用了inotify-tools
包中的inotifywait
。 在Fedora / RHEL / CentOS / SL上,您可以通过yum install inotify-tools
安装软件包。 您可能需要在其他Linux发行版(例如Debian / Ubuntu / Linux Mint)上安装类似的软件包。
![action photo](https://i-blog.csdnimg.cn/blog_migrate/2b2ebd8627293f955f9930a0df76f911.png)
如何使用做,改动后的 .SH ∞ (How to use do-after-change.sh ∞)
用法∞ (The usage ∞)
The command format
命令格式
do-after-change.sh cmd watched_files
where cmd
is the command to execute and watched_files
are the files to be watched.
其中cmd
是要执行的命令, watched_files
是要监视的文件。
One example, to make a project after certain .c, .h or Makefile files change:
一个示例,在某些.c,.h或Makefile文件更改后制作项目:
do-after-change.sh "make" *.c *.h Makefile
or pass options to the command like make install
:
或将选项传递给命令,例如make install
:
do-after-change.sh "make install" *.c *.h Makefile
The script will continuously monitor these files and invoke the command if they are updated.
脚本将连续监视这些文件,并在更新后调用命令。
Note that if the files are changed during the execution of the cmd
, these updates will not trigger the cmd
again.
请注意,如果在执行cmd
期间更改了文件,则这些更新将不会再次触发cmd
。
强制执行命令∞ (Force executing the command ∞)
Do this to force an execution of the command even there is no updates to the monitored files
这样做即使没有监视文件的更新也可以强制执行命令
touch /tmp/doafterchanges
它是如何工作的∞ (How does it work ∞)
The most important part in the script is:
脚本中最重要的部分是:
while true; do
inotifywait -q -r $files $cmdfile
exec_cmd
done
inotifywait
waits for the changes from $files
which is from the options to do-after-change.sh
and $cmdfile
which is /tmp/doafterchanges
for the forcing cmd
execution function.
inotifywait
等待$files
的更改(从选项到do-after-change.sh
和$cmdfile
/tmp/doafterchanges
对于强制cmd
执行功能来说是/tmp/doafterchanges
。
exec_cmd
is to execute the cmd
from the option with some pre and post messages indicating what’s going on.
exec_cmd
用于从该选项执行cmd
,并带有一些指示发生了什么事情的前后消息。
This small tool provides the basic loop of “file change” -> “run command”. Many applications can be built upon it. Let me know by commenting if you find it useful or build some interesting commands on top of it. Enjoy.
这个小工具提供了“文件更改”->“运行命令”的基本循环。 可以在其上构建许多应用程序。 通过评论让我知道您是否认为它有用或在其之上构建一些有趣的命令。 请享用。
翻译自: https://www.systutorials.com/running-a-command-upon-files-or-directories-changes-on-linux/