simulate
simulate

Reputation: 1264

Automatically restart server on file change

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.

Upvotes: 0

Views: 1679

Answers (1)

simulate
simulate

Reputation: 1264

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.

Upvotes: 1

Related Questions