How to Use the Command 'entr' (With Examples)

How to Use the Command 'entr' (With Examples)

The entr command is a highly useful tool for software developers and system administrators, allowing the execution of arbitrary commands when files change. This functionality is essential for automating tasks such as rebuilding and testing projects, running server processes, or handling configuration files. With its simple syntax, entr helps streamline repetitive tasks by monitoring file changes in real-time and executing commands only when necessary.

Use case 1: Rebuild with make if any file in any subdirectory changes

Code:

ag -l | entr make

Motivation:

Imagine you are working on a large project where any file in one of the numerous subdirectories can affect the build. Manually tracking changes and triggering builds can be tedious and error-prone. The above command automates this process by continuously monitoring all files using the ag command and triggering a rebuild whenever a file changes.

Explanation:

  • ag -l: The ag command, known as “The Silver Searcher,” is used here to list all files recursively in the project directory. The -l option ensures that only filenames are listed, which entr can then monitor.
  • | entr make: The pipe (|) directs the output (file list) from ag to entr, which watches for changes and runs the make command whenever a change is detected.

Example Output:

cc -o myapp main.o utils.o

Use case 2: Rebuild and test with make if any .c source files in the current directory change

Code:

ls *.c | entr 'make && make test'

Motivation:

In scenarios where you want to ensure that any change to C source files is immediately tested, this example showcases how to set up a simple continuous integration workflow at a local level. This encourages early detection of issues during development.

Explanation:

  • ls *.c: Lists all C source files in the current directory.
  • | entr 'make && make test': Uses entr to watch these files. When any changes occur, it executes the specified commands. The make command builds the project, and make test runs the test suite to verify the changes.

Example Output:

cc -o myapp main.c
Running tests...
Test suite complete: All tests passed.

Use case 3: Send a SIGTERM to any previously spawned ruby subprocesses before executing ruby main.rb

Code:

ls *.rb | entr -r ruby main.rb

Motivation:

When working on Ruby applications, especially those involving persistent processes like web servers, restarting the server upon file changes is often necessary. Here, the -r option ensures any previous instance of the server is terminated before a new instance starts, preventing script conflicts or retaining outdated configurations.

Explanation:

  • ls *.rb: Lists Ruby source files in the current directory.
  • | entr -r ruby main.rb: The -r option makes entr send a SIGTERM to any existing Ruby subprocess, ensuring a clean environment before running ruby main.rb.

Example Output:

Terminating old instance of main.rb
Starting new instance...
Server running on port 3000

Use case 4: Run a command with the changed file (/_) as an argument

Code:

ls *.sql | entr psql -f /_

Motivation:

Database administrators often need to update or query databases using SQL scripts. Automatically running a changed script not only saves manual execution time but also reduces the risk of forgetting to execute updates.

Explanation:

  • ls *.sql: Lists all SQL files in the current directory.
  • | entr psql -f /_: Uses entr to monitor these files. When a file changes, it runs psql -f, using the changed file (/_) as an argument to execute the script with PostgreSQL.

Example Output:

Executing my_query.sql...
Results updated successfully.

Use case 5: [c]lear the screen and run a query after the SQL script is updated

Code:

echo my.sql | entr -cp psql -f /_

Motivation:

For a cleaner and more organized output, especially in a console environment, clearing the screen ensures that only the latest results are visible. This is particularly useful for repetitive script runs or when monitoring outputs over time.

Explanation:

  • echo my.sql: Outputs the specific SQL file that entr will watch.
  • | entr -cp psql -f /_: The -c option clears the screen before executing the subsequent command, while the -p option enables interactive mode to ensure output appears immediately.

Example Output:

[Screen clears]
Executing my.sql...
Query executed successfully.

Use case 6: Rebuild the project if source files change, limiting output to the first few lines

Code:

find src/ | entr -s 'make | sed 10q'

Motivation:

When dealing with large builds, it can be beneficial to immediately view a succinct portion of the output for quick error checking. This helps in identifying build issues quickly without sifting through pages of console output.

Explanation:

  • find src/: Recursively lists all files in the src/ directory.
  • | entr -s 'make | sed 10q': The -s option makes entr execute the command in a new shell. The sed 10q command limits the output to the first ten lines, providing a quick overview of the build process.

Example Output:

cc -o myapp main.o
...

Use case 7: Launch and auto-[r]eload a Node.js server

Code:

ls *.js | entr -r node app.js

Motivation:

Node.js developers frequently modify files that require the server to restart to reflect updates. This setup ensures that the development server runs the most recent version of the code, enhancing the feedback loop and reducing manual restarts.

Explanation:

  • ls *.js: Lists all JavaScript files in the current directory.
  • | entr -r node app.js: The -r option restarts the Node.js application by terminating existing processes and starting anew whenever a file changes.

Example Output:

Restarting Node.js server...
Server running at http://localhost:3000

Conclusion:

The entr command is a versatile and powerful utility that enhances developer productivity by automating task execution upon file changes. Its ability to integrate with various commands allows for seamless and efficient workflows tailored to different programming environments and projects. These examples demonstrate how entr can aid developers in building, testing, and deploying applications more efficiently and reliably.

Related Posts

How to use the command 'translationd' (with examples)

How to use the command 'translationd' (with examples)

Translationd is a daemon that facilitates enabling translation features for applications and services that require automatic language translation.

Read More
How to use the command 'df' (with examples)

How to use the command 'df' (with examples)

The df command, found in Unix and Unix-like operating systems, is a handy tool used for displaying the amount of disk space available on file systems.

Read More
How to Use the Command 'vectorize-pixelart' (with examples)

How to Use the Command 'vectorize-pixelart' (with examples)

The vectorize-pixelart command is a useful tool for converting PNG pixel art graphics into scalable vector image formats such as SVG or EPS.

Read More