watch
Watch files for changes and execute commands, tasks, or scripts automatically.
Synopsisβ
xec watch <target> [paths...] [options]
Descriptionβ
The watch
command monitors files and directories for changes and automatically executes specified actions. It supports watching files on local filesystem, SSH hosts, Docker containers, and Kubernetes pods.
Argumentsβ
<target>
- Target to watch (local, hosts.name, containers.name, pods.name)[paths...]
- Paths to watch (default: current directory)
Optionsβ
General Optionsβ
-p, --profile <profile>
- Configuration profile to use-i, --interactive
- Interactive mode for configuring watch settings
Watch Configurationβ
--pattern <pattern>
- File patterns to watch (can be used multiple times)--exclude <pattern>
- Patterns to exclude (can be used multiple times)-d, --debounce <ms>
- Debounce interval in milliseconds (default: 300)--initial
- Run command immediately on start--poll
- Use polling instead of native watchers--interval <ms>
- Polling interval when --poll is used (default: 1000)
Action Optionsβ
--command <command>
- Command to execute on change--task <task>
- Task to run on change--script <script>
- Script file to execute on change
Examplesβ
Interactive Modeβ
# Interactive mode to configure file watching
xec watch --interactive
Basic File Watchingβ
# Watch TypeScript files and run tests
xec watch local "src/**/*.ts" --command "npm test"
# Watch current directory and run build script
xec watch local . --script ./scripts/build.js
# Watch specific files and run task
xec watch local "src/**/*.js" --task build
Remote Watchingβ
# Watch remote directory and run deploy task
xec watch hosts.dev /app --task deploy
# Watch files in Docker container
xec watch containers.app /src --pattern "*.js" --command "npm run build"
# Watch pod files excluding node_modules
xec watch pods.frontend /app --exclude "node_modules" --task reload
Advanced Configurationβ
# Watch with debouncing and initial run
xec watch local "src/**/*.ts" --command "npm test" --debounce 1000 --initial
# Use polling for network filesystems
xec watch hosts.nfs /shared --poll --interval 2000 --task sync
# Watch multiple patterns with exclusions
xec watch local . \
--pattern "*.ts" \
--pattern "*.js" \
--exclude "node_modules" \
--exclude "*.test.*" \
--command "npm run lint"
Pattern Matchingβ
# Watch specific file types
xec watch local . --pattern "*.ts" --pattern "*.js" --command "npm run build"
# Watch with complex patterns
xec watch local . \
--pattern "src/**/*.{ts,js}" \
--pattern "config/**/*.yaml" \
--exclude "**/*.test.*" \
--exclude "**/node_modules/**" \
--task build-and-test
Supported Targetsβ
Local Filesystemβ
Uses native file system watchers or polling:
# Watch local files with native watchers
xec watch local src/ --command "npm run build"
# Use polling for better compatibility
xec watch local src/ --poll --command "npm run build"
SSH Hostsβ
Uses inotifywait
on remote system or fallback polling:
# Watch remote files via SSH
xec watch hosts.server /app/src --command "systemctl reload myapp"
# Watch with custom patterns
xec watch hosts.server /etc --pattern "*.conf" --task reload-config
Docker Containersβ
Executes inotifywait
inside the container:
# Watch files in running container
xec watch containers.app /app/src --command "npm run build"
# Watch with exclusions
xec watch containers.web /var/www --exclude "*.log" --task reload-nginx
Kubernetes Podsβ
Uses kubectl exec
to run watchers in pods:
# Watch files in pod
xec watch pods.frontend /app/src --command "npm run build"
# Watch in specific container of multi-container pod
xec watch pods.app /data --task process-data
Pattern Syntaxβ
The watch command supports glob-style patterns:
*
- Match any characters except path separators**
- Match any characters including path separators (recursive)?
- Match single character{a,b,c}
- Brace expansion[abc]
- Character ranges
Pattern Examplesβ
# TypeScript and JavaScript files
--pattern "**/*.{ts,js}"
# Configuration files
--pattern "**/*.{yaml,yml,json}"
# Source files excluding tests
--pattern "src/**/*.ts" --exclude "**/*.test.*"
# Multiple specific patterns
--pattern "*.ts" --pattern "*.css" --pattern "*.html"
Exclude Patternsβ
Exclude patterns prevent files from triggering watch events:
# Exclude common directories
--exclude "node_modules" --exclude ".git" --exclude "dist"
# Exclude file types
--exclude "*.log" --exclude "*.tmp" --exclude "*.cache"
# Exclude test files
--exclude "**/*.test.*" --exclude "**/*.spec.*"
Action Typesβ
Commandsβ
Execute shell commands when files change:
# Simple command
xec watch local src/ --command "npm run build"
# Complex command with pipes
xec watch local . --command "npm run lint && npm run test"
# Command with environment variables
xec watch local . --command "NODE_ENV=development npm start"
Tasksβ
Run configured tasks from .xec/config.yaml
:
# Run simple task
xec watch local src/ --task build
# Run task with parameters (if supported by task)
xec watch local . --task deploy
# Run complex multi-step task
xec watch local . --task build-test-deploy
Scriptsβ
Execute Xec scripts when files change:
# Run TypeScript script
xec watch local src/ --script ./scripts/build.ts
# Run JavaScript script
xec watch local . --script ./automation/deploy.js
# Script with full path
xec watch local . --script /usr/local/bin/custom-build.sh
Debouncingβ
Debouncing prevents excessive executions when multiple files change rapidly:
# Short debounce for quick feedback
xec watch local src/ --command "npm run lint" --debounce 100
# Standard debounce (default)
xec watch local . --command "npm run build" --debounce 300
# Long debounce for expensive operations
xec watch local . --task full-rebuild --debounce 2000
Polling vs Native Watchersβ
Native Watchers (Default)β
- Uses filesystem events (inotify on Linux, FSEvents on macOS)
- More efficient and responsive
- May not work on network filesystems
# Use native watchers (default)
xec watch local src/ --command "npm run build"
Pollingβ
- Checks files at regular intervals
- Works on all filesystems including network mounts
- Higher resource usage
# Use polling
xec watch local /network/share --poll --interval 1000 --task sync
# Polling with custom interval
xec watch hosts.nfs /shared --poll --interval 5000 --command "rsync -av /shared/ /local/"
Interactive Modeβ
Interactive mode provides a guided setup for complex watch configurations:
xec watch --interactive
Interactive mode prompts for:
- Target selection - Choose from configured targets
- Paths to watch - Specify directories and files
- File patterns - Include/exclude patterns
- Action type - Command, task, or script
- Advanced options - Debouncing, polling, initial run
Remote Watching Implementationβ
SSH Hostsβ
The watch command uses inotifywait
on the remote system:
# Command executed on remote host
inotifywait -mr -e modify,create,delete --format '%w%f' /path/to/watch
If inotifywait
is not available, falls back to stat-based polling:
# Fallback polling command
while true; do
current_mtime=$(find /path -type f -exec stat -c '%Y' {} \; | sort -n | tail -1)
if [ "$current_mtime" != "$last_mtime" ]; then
echo "/path MODIFY"
last_mtime="$current_mtime"
fi
sleep 1
done
Docker Containersβ
Similar to SSH, but uses docker exec
:
docker exec container_name sh -c "inotifywait -mr -e modify,create,delete --format '%w%f' /path"
Kubernetes Podsβ
Uses kubectl exec
to run watchers:
kubectl exec -n namespace pod_name -- sh -c "inotifywait -mr -e modify,create,delete --format '%w%f' /path"
Performance Considerationsβ
- File count: Large directories may impact performance
- Pattern complexity: Simple patterns are more efficient
- Debounce timing: Balance responsiveness vs. resource usage
- Remote watching: Network latency affects response time
- Polling interval: Lower intervals increase CPU usage
Error Handlingβ
The watch command handles common scenarios:
- File not found: Reports missing paths
- Permission denied: Shows access errors
- Command failures: Continues watching after failed executions
- Network issues: Attempts to reconnect for remote targets
- Tool unavailability: Falls back to polling when
inotifywait
is missing
Signal Handlingβ
The watch command responds to signals:
- SIGINT (Ctrl+C): Graceful shutdown, cleanup watchers
- SIGTERM: Clean termination
- SIGUSR1: Reload configuration (if supported)
Logging and Outputβ
Watch output includes:
[12:34:56] Change detected: src/index.ts
β Command executed successfully
[12:35:02] Change detected: src/utils.ts
β Execution failed: Command failed with exit code 1
Use --quiet
to suppress output or --verbose
for detailed logging.
Configurationβ
Default watch settings can be configured in .xec/config.yaml
:
commands:
watch:
debounce: 300
poll: false
interval: 1000
clear: true
initial: false
Related Commandsβ
- run - Execute scripts and tasks
- on - Execute commands on SSH hosts
- in - Execute commands in containers/pods
- logs - View logs from targets
Exit Codesβ
0
- Success (watch stopped gracefully)1
- General error2
- Invalid arguments3
- Target not found4
- Watch setup failed5
- Permission error