entr¶
entr is a tool available on Debian-based systems.
It's used for running arbitraty commands when a file is changed.
This tool is not available in the default RedHat repositories. To get similar
functionality on RedHat-based systems, look into inotify-tools,
specifically inotifywait.
Note: If you're looking to use
entron a logfile to monitor for new logs or changes, you might wanttail -Finstead.
Using entr with a single file¶
entr expects the path of a file (or list of files) as standard input.
This can be done with redirection < or with a pipe |.
- The
realpathcommand can be used to get the absolute path of a file if needed.
The entr program can rerun a program each time the given file is changed (written).
- This tells
entrto runbash -c "clear; ./my_script"each timemy_scriptis changed.- Clears the screen, and then runs the file
./my_script.
- Clears the screen, and then runs the file
-
<<<is aherestringoperator (bash-only).- It allows a string to be used as the standard input to a command.
- It means "take this string and send it to
stdinas if it were a file." - Not POSIX-compliant.
-
Alternatively, for a POSIX-compliant solution, pipe the output of an
lsorfindcommand toentr.
-
Another alternative would be using process substitution, using the
<()syntax (bash-only).
To automatically clear the screen, use the -c flag for entr (entr -c).
Then you don't have to call clear; in the command passed to bash.
entr -c bash -c "/tmp/t.sh;" < <(realpath /tmp/t.sh)
# or
realpath /tmp/t.sh | entr -c bash -c "/tmp/t.sh;"
Using entr with multiple files¶
You can set entr to run a command when any file in a list of files changes.
Pass in a list of files as input:
./my_script each time any file with a .sh extensionin the current directory or any subdirectoriesis changed.
Example with Golang Project Testing¶
entr bash -c "clear; go test -v ./..." < <(find . -name '*.go')
# Or, with globstar enabled:
entr bash -c "clear; go test -v ./..." < <(ls **/*.go)
./... is Go's way of saying "all packages in the current directoryand subdirectories."
Other ways to automate¶
Using watch¶
Another option is watch.
This will (by default) run the given command every 2 seconds.
It will clear the screen for each iteration, so there's no need to put a clear; in.
man watch
Infinite Loop (Generally a Bad Idea)¶
A noob option is to use a loop (poor man's watch).
If you do this, remember to use
sleep!