Today during development I had the need for a small script that would compile and relaunch my service so I came up with this small bash snippet. If there are better solutions out there I would love to be informed about them as I did not manage to find anything when looking around.
#!/bin/bash inotifywait --monitor --event close_write,moved_to,create src | while read -r directory events filename; do if [ -z "$PID" ]; then echo "$PID was not set" else echo "killing $PID" kill $PID unset PID fi cargo build if [ $? -eq 0 ]; then $1 & PID=$! echo "Subshell started at $PID" fi done
Usage: script target/debug/binary
I gave it a small go and it seems to be able to handle error cases (currently it kills the old running program but launches a new one when compilation passes).
Having this functinality as part of cargo would be awesome, something like cargo run --watch or similar.