在Linux上的文件或目录更改后运行命令

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

如何使用做,改动后的 .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/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值