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 thelib
andsrc
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.