659

I want a quick and simple way to execute a command whenever a file changes. I want something very simple, something I will leave running on a terminal and close it whenever I'm finished working with that file.

Currently, I'm using this:

while read; do ./myfile.py ; done

And then I need to go to that terminal and press Enter, whenever I save that file on my editor. What I want is something like this:

while sleep_until_file_has_changed myfile.py ; do ./myfile.py ; done

Or any other solution as easy as that.

BTW: I'm using Vim, and I know I can add an autocommand to run something on BufWrite, but this is not the kind of solution I want now.

Update: I want something simple, discardable if possible. What's more, I want something to run in a terminal because I want to see the program output (I want to see error messages).

About the answers: Thanks for all your answers! All of them are very good, and each one takes a very different approach from the others. Since I need to accept only one, I'm accepting the one that I've actually used (it was simple, quick and easy-to-remember), even though I know it is not the most elegant.

42 Answers42

584

Simple, using inotifywait (install your distribution's inotify-tools package):

while inotifywait -e close_write myfile.py; do ./myfile.py; done

or

inotifywait -q -m -e close_write myfile.py |
while read -r filename event; do
  ./myfile.py         # or "./$filename"
done

The first snippet is simpler, but it has a significant downside: it will miss changes performed while inotifywait isn't running (in particular while myfile is running). The second snippet doesn't have this defect. However, beware that it assumes that the file name doesn't contain whitespace. If that's a problem, use the --format option to change the output to not include the file name:

inotifywait -q -m -e close_write --format %e myfile.py |
while read events; do
  ./myfile.py
done

Either way, there is a limitation: if some program replaces myfile.py with a different file, rather than writing to the existing myfile, inotifywait will die. Many editors work that way.

To overcome this limitation, use inotifywait on the directory:

inotifywait -e close_write,moved_to,create -m . |
while read -r directory events filename; do
  if [ "$filename" = "myfile.py" ]; then
    ./myfile.py
  fi
done

Alternatively, use another tool that uses the same underlying functionality, such as incron (lets you register events when a file is modified) or fswatch (a tool that also works on many other Unix variants, using each variant's analog of Linux's inotify).

272

entr (https://github.com/eradman/entr) provides a more friendly interface to inotify (and also supports *BSD & Mac OS X).

It makes it very easy to specify multiple files to watch (limited only by ulimit -n), takes the hassle out of dealing with files being replaced, and requires less bash syntax:

$ find . -name '*.py' | entr ./myfile.py

I've been using it on my entire project source tree to run the unit tests for the code I'm currently modifying, and it's been a huge boost to my workflow already.

Flags like -c (clear the screen between runs) and -d (exit when a new file is added to a monitored directory) add even more flexibility, for example you can do:

$ while sleep 1 ; do find . -name '*.py' | entr -d ./myfile.py ; done

As of early 2018 it's still in active development and it can be found in Debian & Ubuntu (apt install entr); building from the author's repo was pain-free in any case.

wesanyer
  • 103
Paul Fenney
  • 2,851
151

I wrote a Python program to do exactly this called when-changed.

Usage is simple:

when-changed FILE COMMAND...

Or to watch multiple files:

when-changed FILE [FILE ...] -c COMMAND

FILE can be a directory. Watch recursively with -r. Use %f to pass the filename to the command.

joh
  • 1,715
74

How about this script? It uses the stat command to get the access time of a file and runs a command whenever there is a change in the access time (whenever file is accessed).

#!/bin/bash

### Set initial time of file
LTIME=`stat -c %Z /path/to/the/file.txt`

while true    
do
   ATIME=`stat -c %Z /path/to/the/file.txt`

   if [[ "$ATIME" != "$LTIME" ]]
   then    
       echo "RUN COMMAND"
       LTIME=$ATIME
   fi
   sleep 5
done
Pablo A
  • 1,733
VDR
  • 1,081
60

For those who can't install inotify-tools like me, this should be useful:

watch -d -t -g ls -lR

This command will exit when the output changes, ls -lR will list every file and directory with its size and dates, so if a file is changed it should exit the command, as man says:

-g, --chgexit
          Exit when the output of command changes.

I know this answer may not be read by anyone, but I hope someone would reach to it.

Command line example:

~ $ cd /tmp
~ $ watch -d -t -g ls -lR && echo "1,2,3"

Open another terminal:

~ $ echo "testing" > /tmp/test

Now the first terminal will output 1,2,3

Simple script example:

#!/bin/bash
DIR_TO_WATCH=${1}
COMMAND=${2}

watch -d -t -g ls -lR ${DIR_TO_WATCH} && ${COMMAND}
Sebastian
  • 711
36

Solution using Vim:

:au BufWritePost myfile.py :silent !./myfile.py

But I don't want this solution because it's kinda annoying to type, it's a bit hard to remember what to type, exactly, and it's a bit difficult to undo its effects (need to run :au! BufWritePost myfile.py). In addition, this solution blocks Vim until the command has finished executing.

I've added this solution here just for completeness, as it might help other people.

To display the program output (and completely disrupt your editting flow, as the output will write over your editor for a few seconds, until you press Enter), remove the :silent command.

34

rerun2 (on github) is a 10-line Bash script of the form:

#!/usr/bin/env bash

function execute() {
    clear
    echo "$@"
    eval "$@"
}

execute "$@"

inotifywait --quiet --recursive --monitor --event modify --format "%w%f" . \
| while read change; do
    execute "$@"
done

Save the github version as 'rerun' on your PATH, and invoke it using:

rerun COMMAND

It runs COMMAND every time there's a filesystem modify event within your current directory (recursive.)

Things one might like about it:

  • It uses inotify, so is more responsive than polling. Fabulous for running sub-millisecond unit tests, or rendering graphviz dot files, every time you hit 'save'.
  • Because it's so fast, you don't have to bother telling it to ignore large subdirs (like node_modules) just for performance reasons.
  • It's extra super responsive, because it only calls inotifywait once, on startup, instead of running it, and incurring the expensive hit of establishing watches, on every iteration.
  • It's just 12 lines of Bash
  • Because it's Bash, it interprets commands you pass it exactly as if you had typed them at a Bash prompt. (Presumably this is less cool if you use another shell.)
  • It doesn't lose events that happen while COMMAND is executing, unlike most of the other inotify solutions on this page.
  • On the first event, it enters a 'dead period' for 0.15 seconds, during which other events are ignored, before COMMAND is run exactly once. This is so that the flurry of events caused by the create-write-move dance which Vi or Emacs does when saving a buffer don't cause multiple laborious executions of a possibly slow-running test suite. Any events which then occur while COMMAND is executing are not ignored - they will cause a second dead period and subsequent execution.

Things one might dislike about it:

  • It uses inotify, so won't work outside of Linuxland.
  • Because it uses inotify, it will barf on trying to watch directories containing more files than the max number of user inotify watches. By default, this seems to be set to around 5,000 to 8,000 on different machines I use, but is easy to increase. See https://unix.stackexchange.com/questions/13751/kernel-inotify-watch-limit-reached
  • It fails to execute commands containing Bash aliases. I could swear that this used to work. In principle, because this is Bash, not executing COMMAND in a subshell, I'd expect this to work. I'd love to hear If anyone knows why it doesn't. Many of the other solutions on this page can't execute such commands either.
  • Personally I wish I was able to hit a key in the terminal it's running in to manually cause an extra execution of COMMAND. Could I add this somehow, simply? A concurrently running 'while read -n1' loop which also calls execute?
  • Right now I've coded it to clear the terminal and print the executed COMMAND on each iteration. Some folks might like to add command-line flags to turn things like this off, etc. But this would increase size and complexity many-fold.

This is a refinement of @cychoi's anwer.

33

If you happen to have npm installed, nodemon is probably the easiest way to get started, especially on OS X, which apparently doesn't have inotify tools. It supports running a command when a folder changes.

Eric Leschinski
  • 7,393
  • 7
  • 51
  • 51
21

if you have nodemon installed, then you can do this:

nodemon -w <watch directory> -x "<shell command>" -e ".html"

In my case I edit html locally and ship it to my remote server when a file changes.

nodemon -w <watch directory> -x "scp filename jaym@jay-remote.com:/var/www" -e ".html"
Jay
  • 311
16

Here's a simple shell Bourne shell script that:

  1. Takes two arguments: the file to be monitored and a command (with arguments, if necessary)
  2. Copies the file you are monitoring to the /tmp directory
  3. Checks every two seconds to see if the file you are monitoring is newer than the copy
  4. If it's newer it overwrites the copy with the newer original and executes the command
  5. Cleans up after itself when you press Ctr-C

    #!/bin/sh  
    f=$1  
    shift  
    cmd=$*  
    tmpf="`mktemp /tmp/onchange.XXXXX`"  
    cp "$f" "$tmpf"  
    trap "rm $tmpf; exit 1" 2  
    while : ; do  
        if [ "$f" -nt "$tmpf" ]; then  
            cp "$f" "$tmpf"  
            $cmd  
        fi  
        sleep 2  
    done  
    

This works on FreeBSD. The only portability issue I can think of is if some other Unix doesn't have the mktemp(1) command, but in that case you can just hard code the temp file name.

MikeyMike
  • 169
12

Have a look at incron. It's similar to cron, but uses inotify events instead of time.

10

Improved upon edit#3 of Gilles's answer.

This version runs inotifywait once and monitors for events (.e.g.: modify) thereafter. Such that inotifywait doesn't need to be re-executed upon every event encountered.

It's quick and fast!(even when monitoring large directory recursively)

inotifywait --quiet --monitor --event modify FILE | while read; do
    # trim the trailing space from inotifywait output
    REPLY=${REPLY% }
    filename=${REPLY%% *}
    # do whatever you want with the $filename
done
Greenonline
  • 2,390
cychoi
  • 446
9

If your program generates some sort of log/output, you can create a Makefile with a rule for that log/output that depends on your script and do something like

while true; do make -s my_target; sleep 1; done

Alternately, you can create a phony target and have the rule for it both call your script and touch the phony target (while still depending on your script).

ctgPi
  • 199
9

Under Linux:

man watch

watch -n 2 your_command_to_run

Will run the command every 2 seconds.

If your command takes more than 2 seconds to run, watch will wait until it's done before doing it again.

Eric Leschinski
  • 7,393
  • 7
  • 51
  • 51
9

Another solution with NodeJs, fsmonitor :

  1. Install

    sudo npm install -g fsmonitor
    
  2. From command line (example, monitor logs and "retail" if one log file change)

    fsmonitor -s -p '+*.log' sh -c "clear; tail -q *.log"
    
Kevin Panko
  • 7,466
Atika
  • 209
8

Watchdog is a Python project, and may be just what you're looking for:

Supported platforms

  • Linux 2.6 (inotify)
  • Mac OS X (FSEvents, kqueue)
  • FreeBSD/BSD (kqueue)
  • Windows (ReadDirectoryChangesW with I/O completion ports; ReadDirectoryChangesW worker threads)
  • OS-independent (polling the disk for directory snapshots and comparing them periodically; slow and not recommended)

Just wrote a command-line wrapper for it watchdog_exec:

Example runs

On fs event involving files and folders in current directory, run echo $src $dst command, unless it the fs event is modified, then run python $src command.

python -m watchdog_exec . --execute echo --modified python

Using short arguments, and restricting to only execute when events involve "main.py":

python -m watchdog_exec . -e echo -a echo -s __main__.py

EDIT: Just found Watchdog has an official CLI called watchmedo, so check that out also.

8

Look into Guard, in particular with this plugin:

https://github.com/hawx/guard-shell

You can set it up to watch any number of patterns in your project's directory, and execute commands when changes occur. Good chance even that there's a plugin available for that what you're trying to do in the first place.

7

I like the simplicity of while inotifywait ...; do ...; done however it has two issues:

  • File changes happening during the do ...; will be missed
  • Slow when using in recursive mode

Therefor I made a helper script that uses inotifywait without those limitations: inotifyexec

I suggest you put this script in your path, like in ~/bin/. Usage is described by just running the command.

Example: inotifyexec "echo test" -r .

Wernight
  • 671
6

Improved Sebastian's solution with watch command:

watch_cmd.sh:

#!/bin/bash
WATCH_COMMAND=${1}
COMMAND=${2}

while true; do
  watch -d -g "${WATCH_COMMAND}"
  ${COMMAND}
  sleep 1     # to allow break script by Ctrl+c
done

Call example:

watch_cmd.sh "ls -lR /etc/nginx | grep .conf$" "sudo service nginx reload"

It works but be careful: watch command has known bugs (see man): it reacts on changes only in VISIBLE in terminal parts of -g CMD output.

6

You could try reflex.

Reflex is a small tool to watch a directory and rerun a command when certain files change. It's great for automatically running compile/lint/test tasks and for reloading your application when the code changes.

# Rerun make whenever a .c file changes
reflex -r '\.c$' make
6

Check out https://github.com/watchexec/watchexec.

watchexec is a simple, standalone tool that watches a path and runs a command whenever it detects modifications.

Example

Watch all JavaScript, CSS and HTML files in the current directory and all subdirectories for changes, running make when a change is detected:

$ watchexec --exts js,css,html make

Tails
  • 161
5

A little more on the programming side, but you want something like inotify. There are implementations in many languages, such as jnotify and pyinotify.

This library allows you to monitor single files or entire directories, and returns events when an action is discovered. The information returned includes the file name, the action (create, modify, rename, delete) and the file path, among other useful information.

5

For those of you who are looking for a FreeBSD solution, here is the port:

/usr/ports/sysutils/wait_on
slhck
  • 235,242
akond
  • 256
  • 2
  • 4
5

A oneliner answer that I'm using to keep track on a file change:

$ while true ; do NX=`stat -c %Z file` ; [[ $BF != $NX ]] && date >> ~/tmp/fchg && BF=$NX || sleep 2 ; done

You don't need to initialize BF if you know that the first date is the starting time.

This is simple and portable. There is another answer based on the same strategy using a script here. Take a look also.


Usage: I'm using this to debug and keep an eye on ~/.kde/share/config/plasma-desktop-appletsrc; that for some unknown reason keeps loosing my SwitchTabsOnHover=false

DrBeco
  • 2,125
4

I use this script to do it. I'm using inotify in monitor-mode

#!/bin/bash
MONDIR=$(dirname $1)
ARQ=$(basename $1)

inotifywait -mr -e close_write $MONDIR | while read base event file 
do
  if (echo $file |grep -i "$ARQ") ; then
    $1
  fi
done

Save this as runatwrite.sh

Usage: runatwrite.sh myfile.sh

it will run myfile.sh at each write.

Archemar
  • 1,707
4

For people who find this by Googling for changes to a particular file, the answer is much simpler (inspired by Gilles's answer).

If you want to do something after a particular file has been written to, here's how:

while true; do
  inotifywait -e modify /path/to/file
  # Do something *after* a write occurs, e.g. copy the file
  /bin/cp /path/to/file /new/path
done

Save this as, for example, copy_myfile.sh and put the .sh file into the /etc/init.d/ folder to have it run on startup.

LondonRob
  • 455
3

For those using OS X, you can use a LaunchAgent to watch a path/file for changes and do something when that happens. FYI - LaunchControl is a good app to easily make/modify/remove daemons/agents.

(example taken from here)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC -//Apple Computer//DTD PLIST 1.0//EN
http://www.apple.com/DTDs/PropertyList-1.0.dtd>
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>test</string>
    <key>ProgramArguments</key>
    <array>
        <string>say</string>
        <string>yy</string>
    </array>
    <key>WatchPaths</key>
    <array>
        <string>~/Desktop/</string>
    </array>
</dict>
</plist>
Hefewe1zen
  • 1,862
3

I have a GIST for this and the usage is pretty simple

watchfiles <cmd> <paths...>

https://gist.github.com/thiagoh/5d8f53bfb64985b94e5bc8b3844dba55

thiagoh
  • 161
3

As a few others have done, I've also written a lightweight command line tool to do this. It's fully documented, tested and modular.

Watch-Do

Installation

You can install it (if you have Python3 and pip) using:

pip3 install git+https://github.com/vimist/watch-do

Usage

Use it straight away by running:

watch-do -w my_file -d 'echo %f changed'

Features Overview

  • Supports file globbing (use -w '*.py' or -w '**/*.py')
  • Run multiple commands on a file change (just specify the -d flag again)
  • Dynamically maintains the list of files to watch if globbing is used (-r to turn this on)
  • Multiple ways to "watch" a file:
    • Modification time (default)
    • File hash
    • Trivial to implement your own (this is the ModificationTime watcher)
  • Modular design. If you want to have commands run, when a file is accessed, it's trivial to write your own watcher (mechanism that determines if the doers should be run).
OdinX
  • 211
3

If you don't want to install anything new for this, here is a small shell-script you can put in your path (e.g. under $HOME/bin). It runs a command when the provided one or more files are changed. For example:

$ onchange './build' *.txt
#!/bin/sh
cmd="$1"; shift
files="$@"
changed() { tar -c $files | md5sum; } # for on every save use: `stat -c %Z $files`
while true; do
  if [ "$(changed)" != "$last" ]; then
    last="$(changed)"
    $cmd
  fi
  sleep 1
done

It tars, and then hashes the contents of the files and/or directories, so it won't run every time you compulsively hit CTRL-S (or type :w), but only once something actually changes. Note it will check every second, so don't include to much or your machine could get slow. If you want it to run on every save, use stat in stead (see comment). Also, for mac md5sum is called md5 if I remember correctly.

Neat little trick: The moment you want to use it, you'll probably want to repeat the last command you just ran, but over and over. You can use the !! shortcut to 'inject' the last command into this one:

$ onchange "!!" *.txt
Pablo A
  • 1,733
Leon S.
  • 189
3

Facebook's Watchman has built some community. You might already be using a tool that requires it.

It detects new files matching given patterns. For large projects, unlike entr, I don't need to bump system file limits and restart the computer. The following glob just works, against all project e.g. Python files.

watchman-make --pattern '**/*.py' --run './myfile.py'

It waits for a file to change before it rebuilds. You may want to run the command once without changes, then start watching.

(export CMD="$SHELL -c './myfile.py'" && eval "$CMD"; watchman-make --pattern '**/*.py' --run "$CMD")
Bluu
  • 246
2

I wrote a Python program to do exactly this, called rerun.

UPDATE: This answer is a Python script that polls for changes, which is useful in some circumstances. For a Linux-only Bash script that uses inotify, see my other answer, search this page for 'rerun2'.

Install for Python2 or Python3 with:

pip install --user rerun

and usage is very simple:

rerun "COMMAND"

The command is expected as a single arg, not a sequence of space-separated args. Hence quote it as shown, which reduces any extra escaping you'd have to add. Just type the command as you would have typed it at the command line, but surrounded by quotes.

By default it watches all files in or under the current directory, skipping things like known source control dirs, .git, .svn, etc.

Optional flags include '-i NAME' which ignores changes to named files or directories. This can be given multiple times.

Since it's a Python script, it needs to run the command as a sub-process, and we use a new instance of the user's current shell to interpret 'COMMAND' and decide what process to actually run. However, if your command contains shell aliases and the like which are defined in .bashrc, these will not be loaded by the subshell. To fix this, you can give rerun a '-I' flag, to use interactive (aka 'login') subshells. This is slower and more error-prone than starting a regular shell, because it has to source your .bashrc.

I use it with Python 3, but last I checked rerun still worked with Python 2.

Double-edged sword is that it uses polling instead of inotify. On the upside, this means it works on every OS. Plus, it's better than some other solutions shown here in terms of only running the given command once for a bunch of filesystem changes, not once per modified file, while at the same time it does run the command a second time if any files change again while command is running.

On the downside, polling means that there is a 0.0 to 1.0 second latency, and of course it's slow to monitor extremely large directories. Having said that, I've never encountered a project large enough that this is even noticeable so long as you use '-i' to ignore big things like your virtualenv and node_modules.

Hmmm. rerun has been indispensible to me for years - I basically use it eight hours every day for running tests, rebuilding dot files as I edit them, etc. But now I come to type this up here, it's clear that I need to switch to a solution that uses inotify (I no longer use Windows or OSX.) and is written in Bash (so it works with aliases without any extra fiddling.)

1

Basic usage

Here is a solution that does not require installing more software and works out of the box.

tail -q --follow=name myfile.txt | head -n 0

This command exits under the following conditions:

  • A line is added to myfile.txt after the command is run
  • The myfile.txt is replaced with another after the command is run

You say you are using vim, and vim will replace the file on save. I have tested this works with vim.

You can ignore the output of this command, it may mention something like:

tail: ‘myfile.txt’ has been replaced; following end of new file

Advanced usage

You can combine this with timeout to return true or false. You can use it like this:

timeout 5s bash -c 'tail -q --follow=name pipe 2> /dev/null | head -n 0' && echo changed || echo timeout

Discussion

tail uses inotify under the hood. That's how you get this fancy asynchronous behavior without any polling. There is probably some other standard unix program that uses inotify which we can abuse more elegantly.

Sometimes these commands will exit right away but if you immediately run them a second time then they work as advertised. I have made an off-by-one error somewhere, please help me correct this.

On RHEL I can use:

timeout 5s sh -c 'gio monitor pipe | head -n 0' && echo changed || echo timeout

But I am not sure if that is portable.

1

inotifywait one-liner

This uses inotifywait while avoiding the use of while read -r:

inotifywait -q --format '%f' -e close_write,moved_to -m . |
    grep --line-buffered -F -x 'myfile.py' |
    xargs -l -i './myfile.py'

Explanation: inotifywait outputs a line with the filename when it detected a change, grep filters the targeted filename, and xargs executes a command for each filename.

inotifywait parameters:

  • -q: Remove stderr messages.
  • --format '%f' Only output the filename, we don't filter on events anyway.
  • -e close_write,moved_to Detect only close_write (file has been written to), and moved_to (most editors use swap files, and move the buffer to the file when saving).
  • -m Keep listening indefinitely (press CTRL+C to interrupt).
  • . Target directory that contains the targeted file.

grep parameters:

  • --line-buffered: Flush lines immediately, treat as stream (like sed).
  • -F: Literal filename, don't parse regular expression (otherwise we'd need to escape the dot: myfile\.py).
  • -x: Match the whole line, not just a substring of the filename.

xargs parameters:

  • -l: Execute for each input line, don't gather lines up.
  • -i: Prevent the filename being added as an argument to the command, it replaces {} in the command with the input line.

Generic function to execute command on file change

For a more generic case, you may use this function:

exec-onchange()
{
    local file="$1"
    shift
# strip path
local filename=&quot;${file##*/}&quot;

# strip filename
local path=&quot;${file%/*}&quot;
if [ -z &quot;$path&quot; ]; then path=&quot;.&quot;; fi

# catch a custom command
local cmd=&quot;$@&quot;
local literalFlag=&quot;&quot;
if [ -z &quot;$cmd&quot; ]; then cmd=&quot;$path/$filename&quot;; literalFlag=&quot;-Fx&quot;; fi

exec inotifywait -q --format '%f' -e close_write,moved_to -m &quot;$path&quot; |
grep --line-buffered $literalFlag &quot;$filename&quot; |
xargs -l -i /bin/bash -c &quot;$cmd&quot;

}

Usage:

exec-onchange [file-to-watch] [command-to-execute]

Example usage:

exec-onchange myfile.py

Note that the argument is literal, unless a custom command is added, in which case the first argument becomes a regular expression. Although in this case, regular expressions are only allowed for matching the filename, and not the path.

This function allows for more complex usage. Such as automatically compiling, while running the compiled executable as soon as compilation is complete (by using a separate exec-onchange, compilation can be done while still running build/main):

exec-onchange 'src/.*\.cpp' 'echo "{} changed"; gcc src/*.cpp -o build/main' &
exec-onchange build/main
Yeti
  • 171
1

for those that are using docker, none of these solutions would work because files modified on host side do not trigger file changes inside docker containers. my solution was a simple script:

#!/bin/bash

command="ls -al *" out=$($command | md5sum) echo $out exit last=$($command | md5sum) while true do if [ "$out" != "$last" ] then #command to run when change is detected

    touch src/index.js
    out=$($command | md5sum)
    echo &quot;change detected&quot;
fi
sleep 5
last=$($command | md5sum)

done

I made this script to be able to develop for nodejs/react using nodemon to restart nodejs server as needed.

0

The 'fido' tool may be yet another option for this need. See https://www.joedog.org/fido-home/

0

Description

This will watch a file for changes and execute whatever command (including further arguments) was given as second statement. It will also clear the screen and print the time of last execution. Note: you can make the function more (or less) reactive by changing the number of seconds the function should sleep after each while loop cycle.

Example usage

watch_file my_file.php php my_file.php

This line will watch a php file my_file.php and run through php interpreter whenever it changes.


Function definition

function watch_file (){

### Set initial time of file
LTIME=`stat -c %Z $1`
printf "\033c"
echo -e "watching: $1 ---- $(date '+%Y-%m-%d %H:%M:%S')\n-------------------------------------------\n"
${@:2}

while true
do
   ATIME=`stat -c %Z $1`

   if [[ "$ATIME" != "$LTIME" ]]
   then
    printf "\033c"
    echo -e "watching: $1 ---- $(date '+%Y-%m-%d %H:%M:%S')\n-------------------------------------------\n"
    ${@:2}
    LTIME=$ATIME
   fi
   sleep 1
done
}

Credit

This is basically a more general version of VDR's answer.

petermeissner
  • 451
  • 4
  • 8
0

find can do the trick.

while true; do
    find /path/to/watched/file -ctime 1s | xargs do-what-you-will
done

find -ctime 1s prints the filename if it was changed in the last 1s.

lvijay
  • 1
0

I had a slightly different situation. But I feel this may be useful to someone reading this question.

I needed to be notified when a log file changed size, but not necessary immediately. And it could be days or weeks in the future, so I could not use inotify (which was not installed/activated on that server anyway) on the command line (I didn't want to use nohup or similar). So I decided to run a bash script on cron to check

The script writes the file size of the watched file in a text file and on every cron run and checks, if that value has changed and mail the last line to me if changed

#!/bin/bash
FILE_TO_WATCH="/path/to/log_file.log"
FILESIZE_FILE="/path_to/record.txt"
SUBJECT="Log file 'log_file.log' has changed"
MAILTO="info@example.com"
BODY="Last line of log file:\n"
LAST_LINES=1

# get old recorded file size from file
OLD_FILESIZE=$(cat "${FILESIZE_FILE}")
# write current file size into file
stat --printf="%s" "${FILE_TO_WATCH}" > "${FILESIZE_FILE}"
# get new recorded file size from file
NEW_FILESIZE=$(cat "${FILESIZE_FILE}")


if [ "${OLD_FILESIZE}" != "${NEW_FILESIZE}" ]; then
    echo -e "${BODY}"$(tail -${LAST_LINES} ${FILE_TO_WATCH}) | mail -s "${SUBJECT}" "${MAILTO}"
fi
yunzen
  • 137
  • 9
0

I used incron. This needs to be installed but it's easy to use and set up.

incron is an "inotify cron" system. It consists of a daemon and a table manipulator. You can use it a similar way as the regular cron. The difference is that the inotify cron handles filesystem events rather than time periods.

0

A great tool that I came across is called iwatch that is written in perl and has an xml-based config file that let's you define paths and events to watch with inotify and it will call a script to handle the event. It's great for handling sftp uploads for example.

https://iwatch.sourceforge.net/index.html

dannyw
  • 141