How to Use the Command 'watchexec' (with examples)

How to Use the Command 'watchexec' (with examples)

Watchexec is a versatile command-line tool developed to improve productivity and efficiency in software development. It offers the convenience of watching files for changes and executing specified commands whenever a modification is detected. This allows developers to automate tasks such as building, testing, or restarting applications, thereby freeing them from manual intervention and enhancing the speed of their development workflow.

Use Case 1: Call ls -la when any file in the current directory changes

Code:

watchexec ls -la

Motivation:

Developers often need to keep an eye on the state of files within a directory, especially when working in environments where file contents or metadata are frequently updated. By monitoring the directory for changes and automatically listing all files in detailed format, this command can help developers quickly identify new, deleted, or modified files, improving situational awareness without constant manual checks.

Explanation:

  • watchexec: The command-line tool that listens for file changes and executes defined commands.
  • ls -la: This is the command executed whenever a file change is detected. ls lists directory contents, -l outputs in a long listing format, and -a includes hidden files.

Example output:

Every time a file changes, you might see a listing like this:

total 16
drwxr-xr-x  3 username  staff  102 Jan  1 12:00 .
drwx------  5 username  staff  170 Jan  1 12:00 ..
-rw-r--r--  1 username  staff  0   Jan  1 12:01 example.txt

Use Case 2: Run make when any JavaScript, CSS, and HTML file in the current directory changes

Code:

watchexec --exts js,css,html make

Motivation:

In web development, changes to JavaScript, CSS, and HTML files often necessitate rebuilding assets, which is commonly done using tasks defined in a Makefile. Automating this process with watchexec allows developers to focus on writing code while ensuring that their builds are always up-to-date following any modification to these files.

Explanation:

  • --exts js,css,html: Specifies file extensions to watch for. Here, .js, .css, and .html files are being monitored.
  • make: Command executed when changes are detected; it typically builds the project by executing tasks defined in a Makefile.

Example output:

You might see build output like this when a file changes:

Building...
...compiling assets...
Build complete.

Use Case 3: Run make when any file in the lib or src directory changes

Code:

watchexec --watch lib --watch src make

Motivation:

For projects with organized directory structures, it’s crucial to maintain an up-to-date build reflecting changes in specific source directories, such as lib and src. Watching these folders help developers initiate a build only when core files are modified, conserving resources and time by avoiding unnecessary builds.

Explanation:

  • --watch lib: Specifies the lib directory as a watch target.
  • --watch src: Similarly specifies the src directory. The command watches both directories for changes.
  • make: Executes the build process upon any detected change in the nominated directories.

Example output:

When a change is registered in either directory, you may encounter the following output:

Detected changes, running make...
Compiling source code...
Completed successfully.

Use Case 4: Call/restart my_server when any file in the current directory changes, sending SIGKILL to stop the child process

Code:

watchexec --restart --stop-signal SIGKILL my_server

Motivation:

Restarting server processes upon code changes is a common need, especially in development environments where testing new code iterations without manual server restarts enhances productivity. This command caters to situations where an immediate kill and restart of the server is necessary to ensure updated code is served.

Explanation:

  • --restart: The option to restart the program upon detecting file changes.
  • --stop-signal SIGKILL: This specifies that the SIGKILL signal should be used to terminate the current child process before restarting, ensuring an immediate and definite stop of the process.
  • my_server: A placeholder for the actual server process or script that needs to be restarted when files are modified.

Example output:

On every file change, the following sequence may appear:

Changes detected - Restarting server...
Server stopped with SIGKILL.
Server started.

Conclusion:

By leveraging watchexec, developers can save considerable time in their development processes by automating tasks that depend on file system changes, whether it is rebuilding assets, monitoring file states, or restarting services. Each use case demonstrates the command’s flexibility to adapt to varied workflow scenarios, empowering developers to concentrate on more critical aspects of their work.

Related Posts

Exploring the `coproc` Command in Bash (with examples)

Exploring the `coproc` Command in Bash (with examples)

The coproc command in Bash enables users to create interactive asynchronous subshells.

Read More
How to Filter and Select Data using 'tsv-filter' (with examples)

How to Filter and Select Data using 'tsv-filter' (with examples)

The tsv-filter command-line tool is a powerful utility for processing Tab-Separated Value (TSV) files.

Read More
How to Use the Command 'calligrasheets' (with Examples)

How to Use the Command 'calligrasheets' (with Examples)

Calligra Sheets is a part of the Calligra Suite, which is an integrated set of applications sharing a common foundation written using the Qt toolkit.

Read More