0

I would like to run a simple-http-server (a blocking command) and have it automatically restart when specified files change on Linux. Something like this:

hotreload -w src/ -w index.html simple-http-server

To restart the command whenever the directory src or file index.html change.

Is there a command like this for linux? I have only found extensions for npm and the very low level inotify API.

stimulate
  • 1,199
  • 1
  • 11
  • 30

1 Answers1

1

cargo watch is actually a plugin for the Rust build tool cargo, but it can watch any files and also run shell commands:

cargo watch -w src/ -w index.html -s "./start_server.sh &"

The start_server.sh script should contain something like this:

kill $(pidof simple-http-server) # kill any running instances to free up port
simple-http-server

because when a server is still running in the background, the new instance won't be able to access the port.

This will run the command specified with -s "…" and will rerun it whenever any files or directories watched with -w change.

stimulate
  • 1,199
  • 1
  • 11
  • 30