How to use the command watchexec (with examples)

How to use the command watchexec (with examples)

Watchexec is a command-line tool that allows you to run arbitrary commands when files change. It can be useful during development when you want to automate repetitive tasks such as compiling code, running tests, or refreshing a server.

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

Code:

watchexec ls -la

Motivation: This use case is useful when you want to monitor any changes happening in the current directory. By running the ls -la command, you can see detailed file information whenever a file changes.

Explanation:

  • watchexec: The command used to execute the watchexec tool.
  • ls -la: The arbitrary command to be run when file changes are detected.

Example output:

total 16
drwxr-xr-x  3 user  staff   96 Mar 10 10:40 .
drwxr-xr-x 18 user  staff  576 Mar 10 10:15 ..
-rw-r--r--  1 user  staff  134 Mar 10 10:40 file1.txt
-rw-r--r--  1 user  staff    0 Mar 10 10:40 file2.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: This example is useful when you are working on a project involving JavaScript, CSS, and HTML files. Whenever any of these file types change, the make command will be executed, allowing you to automatically rebuild your project.

Explanation:

  • watchexec: The command used to execute the watchexec tool.
  • --exts js,css,html: Specifies the file extensions to monitor for changes. In this case, it will monitor JavaScript, CSS, and HTML files.
  • make: The arbitrary command to be run when file changes are detected.

Example output:

Building project...
Done!

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

Code:

watchexec --watch lib --watch src make

Motivation: In larger projects, it is common to have specific directories dedicated to different parts of the codebase. To automate the build process, you can use watchexec to monitor changes in multiple directories simultaneously. In this example, the lib and src directories are watched and the make command is executed when any file changes within them.

Explanation:

  • watchexec: The command used to execute the watchexec tool.
  • --watch lib --watch src: Specifies the directories to monitor for changes. In this case, it will monitor the lib and src directories.
  • make: The arbitrary command to be run when file changes are detected.

Example output:

Building project...
Done!

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: This example is useful when you are working on a server application and want to automatically restart the server whenever a file changes. By using the --restart option, watchexec will restart the my_server process whenever it detects a file change. The --stop-signal SIGKILL option specifies that the child process should be stopped using the SIGKILL signal.

Explanation:

  • watchexec: The command used to execute the watchexec tool.
  • --restart: Restarts the command (my_server) when file changes are detected.
  • --stop-signal SIGKILL: Specifies the signal to be used to stop the child process. In this case, the SIGKILL signal is used.
  • my_server: The arbitrary command to be run when file changes are detected.

Example output:

Restarting my_server...
my_server listening on port 8080.

Conclusion:

Watchexec is a versatile command-line tool that can greatly enhance your development workflow by allowing you to automate tasks based on file changes. Whether it’s running test suites, rebuilding projects, or refreshing servers, watchexec provides an elegant solution for executing arbitrary commands on file change events. Explore the various options and use cases provided to tailor watchexec to fit your specific needs.

Related Posts

w32tm Command Examples (with examples)

w32tm Command Examples (with examples)

Example 1: Show the current status of time synchronization w32tm /query /status /verbose Motivation The motivation for checking the current status of time synchronization is to ensure that the clock on the system is synchronized correctly with a reliable time source.

Read More
How to use the command pvecm (with examples)

How to use the command pvecm (with examples)

The pvecm command, also known as the Proxmox VE Cluster Manager, is a tool used for managing a Proxmox VE cluster.

Read More
An HTTP benchmarking tool - http_load (with examples)

An HTTP benchmarking tool - http_load (with examples)

Emulate 20 requests based on a given URL list file per second for 60 seconds: http_load -rate 20 -seconds 60 path/to/urls.

Read More