watch.sh

A small Bash utility that reruns a command when files change.

It is designed to be embedded into another project (for example in a scripts/ directory) rather than installed globally.


Overview

watch.sh monitors files in one or more directories and restarts a given command whenever a change is detected.

It supports multiple backends:


Installation

From this repository, install into another project:

make install TARGET_DIR=../your-project/scripts

This will copy the script to:

../your-project/scripts/watch.sh

Alternatively, create a symlink (recommended during development):

make link TARGET_DIR=../your-project/scripts

Usage

From the target project:

./scripts/watch.sh [options] -- <command>

Example:

./scripts/watch.sh -e js,ts -- node server.js

Options

Option Description —————- —————————————————— -w <dir> Directory to watch (default: .) -e <exts> File extensions (default: js,mjs,cjs,json,ts) -i <pattern> Ignore pattern (can be used multiple times) -d <secs> Restart delay in seconds (default: 1) -s <signal> Signal used to stop the process (default: SIGTERM) -k <secs> Time before force kill (default: 3) -c Clear screen before restarting -v Enable verbose output -h Show help


Examples

Watch a Node.js app:

./scripts/watch.sh -e js -- node server.js

Watch multiple directories:

./scripts/watch.sh -w src -w config -- npm run dev

Ignore directories:

./scripts/watch.sh -i node_modules -i dist -- npm start

Clear screen on restart:

./scripts/watch.sh -c -- npm run dev

How it works

  1. Detects an available file watching backend
  2. Starts the provided command as a child process
  3. Waits for file changes
  4. On change:
    • stops the running process
    • waits for the configured delay
    • restarts the command

Notes


License

MIT