David Wolever
David Wolever

Reputation: 154504

Program to re-run, eg, `make` when files are modified?

Is there a program that will automatically re-run, eg, make, when files are modified?

For example, when I'm writing sphinx documentation, it would be nice if make html was run automatically each time I edit any relevant files.

Upvotes: 10

Views: 4777

Answers (7)

arauzo
arauzo

Reputation: 185

In Linux, you can use this command line:

while true; do inotifywait -e close_write  *.py; make; done

Uses standard system command inotifywait, if not available, install with something like:

sudo apt install inotify-tools

Upvotes: 6

masterxilo
masterxilo

Reputation: 2778

This question was also asked here: https://superuser.com/questions/181517/how-to-execute-a-command-whenever-a-file-changes/

You could try reflex

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

Upvotes: 0

David Wolever
David Wolever

Reputation: 154504

As per answer https://stackoverflow.com/a/22907316/71522 watchman seems to work very well:

 $ watchman-make -p '*.c' 'Makefile' -t all

Will re-run make all each time any *.c or Makefile file changes.

It can be installed with:

$ brew install watchman

Upvotes: 2

Walker Hale IV
Walker Hale IV

Reputation: 3328

For simple things, rerun could be a good fit: http://pypi.python.org/pypi/rerun

"Command-line executable Python script to re-run the given command every time files are modified in the current directory or its subdirectories."

It requires a Python interpreter, but does not care if your command or files are written in Python.

Usage

rerun [--help|-h] [--verbose|-v] [--ignore|-i=<file>] [--version] <command>
Where:

<command>           Command to execute
--help|-h           Show this help message and exit.
--ignore|-i=<file>  File or directory to ignore. Any directories of the
                    given name (and their subdirs) are excluded from the
                    search for changed files. Any modification to files of
                    the given name are ignored. The given value is
                    compared to basenames, so for example, "--ignore=def"
                    will skip the contents of directory "./abc/def/" and
                    will ignore file "./ghi/def". Can be specified multiple
                    times.
--verbose|-v        Display the names of changed files before the command
                    output.
--version           Show version number and exit.

Upvotes: 12

singpolyma
singpolyma

Reputation: 11241

You could use inotifywait in a loop: https://github.com/rvoicilas/inotify-tools/wiki/#info

Upvotes: 2

singpolyma
singpolyma

Reputation: 11241

You could use incron: http://inotify.aiken.cz/?section=incron&page=about&lang=en

Upvotes: 1

Ulrich Schwarz
Ulrich Schwarz

Reputation: 7727

Well, since make will not do anything if nothing has changed, how about

while true; do sleep 60; make html; done

or the equivalent in your shell of choice? I don't think the usual file system layers are event-driven in such a way that they will you notify you of file changes without doing some similar themselves, but it's possibly DBUS can do that sort of stuff.

Upvotes: 4

Related Questions