【问题标题】:How do I run a script every time I save a file in Linux?每次在 Linux 中保存文件时如何运行脚本?
【发布时间】:2011-06-24 16:18:11
【问题描述】:

每次在 Linux 中保存文件时如何运行脚本?

【问题讨论】:

    标签: linux


    【解决方案1】:

    Linux 有一个子系统调用 inotify,它可以导致文件系统向应用程序报告文件系统中的更改。

    您的 linux 系统可能有一个名为 incron 的软件包,这使得使用 inotify 变得非常容易。 (或者在您的可用软件包中搜索其描述中包含“inotify”一词的任何软件包)。

    设置incron 与设置cron 非常相似,除了cron 在指定时间和日期执行脚本,incron 在指定文件或目录更改时执行脚本。

    PS。在 Ubuntu 上(例如),incronthis package 并安装了

    sudo apt-get install incron
    

    【讨论】:

      【解决方案2】:

      我试图做同样的事情并最终编写了一个 python 脚本来为我做这件事。

      https://github.com/bawigga/spy

      用法:

      $ ./spy myscript.py
      ... your script output ...
      
      $ ./spy --help
      Usage: spy [OPTIONS] [WATCH]
      
      Options:
        --version             show program's version number and exit
        -h, --help            show this help message and exit
        -c, --clear           clear the screen after each execution
        -e COMMAND, --exec=COMMAND
                              path to the program to execute
        -i INTERVAL, --interval=INTERVAL
                              (in seconds) set the time interval to check for file
                              modifications
      

      【讨论】: