Skip to content

watchexec - Run Commands on File Changes Cheatsheet

watchexec - Run Commands on File Changes Cheatsheet

watchexec watches a set of files or directories and runs a command whenever they change. It is language- and project-agnostic — no config file required — and handles the fiddly parts of file-watching for you: debouncing rapid changes, respecting .gitignore, restarting long-running processes cleanly, and passing the changed paths to your command. Use it to auto-run tests, restart a dev server, or rebuild on save.

Installation

PlatformCommand
Cargo (all platforms)cargo install watchexec-cli
macOS (Homebrew)brew install watchexec
Arch Linuxsudo pacman -S watchexec
Debian/Ubuntudownload the .deb from Releases
Windows (Scoop)scoop install watchexec
Verifywatchexec --version

Basic Usage

CommandDescription
watchexec "cargo test"Run tests on any change
watchexec -e py "pytest"Only watch .py files
watchexec -e js,ts,jsx "npm run build"Watch multiple extensions
watchexec -- ls -laEverything after -- is the command
watchexec --helpFull option list

Filtering What to Watch

OptionDescription
-e, --exts py,rsOnly trigger on these file extensions
-w, --watch DIRWatch a specific path (repeatable)
-i, --ignore GLOBIgnore paths matching a glob
--ignore-file FILELoad ignore patterns from a file
--no-vcs-ignoreDon’t use .gitignore rules
-f, --filter GLOBOnly watch files matching a glob

Process Management

OptionDescription
-r, --restartRestart the command on change (kill + rerun)
-s, --signal SIGSignal to send when restarting (e.g. SIGTERM)
--stop-timeout 5sGrace period before force-kill
-o, --on-busy-update MODEqueue, do-nothing, restart, or signal
-nNo process group (send signal to the direct child only)

Use -r for long-running processes (servers), and the default for one-shot commands (tests, builds).

Debouncing & Timing

OptionDescription
--debounce 200msWait for changes to settle before running
--delay-run 1sDelay before the command runs
-p, --postponeDon’t run until the first change (skip initial run)

Useful Behavior

OptionDescription
-c, --clearClear the screen before each run
--emit-events-to environmentPass changed paths via env vars
-N, --notifyDesktop notification on completion
--project-origin DIRSet the project root for ignore rules
-vVerbose (debug what triggers)

Environment Variables (Changed Paths)

When events are emitted to the environment, your command can see what changed:

VariableContains
WATCHEXEC_COMMON_PATHCommon prefix of changed paths
WATCHEXEC_WRITTEN_PATHFiles written
WATCHEXEC_CREATED_PATHFiles created
WATCHEXEC_REMOVED_PATHFiles removed

Common Workflows

# Auto-run the test suite on any source change
watchexec -e rs "cargo test"

# Restart a dev server cleanly on change
watchexec -r -e go "go run ./cmd/server"

# Rebuild and clear the screen, ignoring the dist folder
watchexec -c -i "dist/**" -e ts "npm run build"

# Only after changes settle (avoid thrashing on bulk saves)
watchexec --debounce 500ms -e py "pytest -x"
ToolModel
watchexecStandalone, config-free file watcher
entrMinimal, reads file list from stdin
nodemonNode-focused, restarts node apps
mise/just + watchexecTask runners that shell out to watchexec

Task runners like just and mise often pair with watchexec to add file-watching to their recipes.

Resources