Skip to content

Instantly share code, notes, and snippets.

@aneury1
Created December 24, 2025 10:10
Show Gist options
  • Select an option

  • Save aneury1/c7a84fd5fd1eb487ae21305ab5f377c9 to your computer and use it in GitHub Desktop.

Select an option

Save aneury1/c7a84fd5fd1eb487ae21305ab5f377c9 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# =========================
# Configuration
# remember install inotify-tools
# =========================
# Folders to watch (add as many as needed)
WATCH_DIRS=(
"./AHttpServer/src"
"./AHttpServer/includes"
# "./lib"
# "./modules"
"./Apps"
"./third_party"
)
BUILD_CMD="cmake --build ./build" # or: cmake --build build
EXECUTABLE="./build/App" # path to your executable
PID_FILE="/tmp/app.pid"
# =========================
# Functions
# =========================
stop_app() {
if [[ -f "$PID_FILE" ]]; then
PID=$(cat "$PID_FILE")
if kill -0 "$PID" 2>/dev/null; then
echo "Stopping app (PID $PID)..."
kill "$PID"
wait "$PID" 2>/dev/null
fi
rm -f "$PID_FILE"
fi
}
build_and_run() {
echo "Building..."
if $BUILD_CMD; then
echo "Build successful. Starting app..."
$EXECUTABLE &
echo $! > "$PID_FILE"
else
echo "Build failed."
fi
}
cleanup() {
echo "Shutting down..."
stop_app
exit 0
}
trap cleanup SIGINT SIGTERM
# =========================
# Initial run
# =========================
build_and_run
# =========================
# Watch loop
# =========================
echo "Watching directories:"
printf ' %s\n' "${WATCH_DIRS[@]}"
inotifywait -m -r \
-e modify,create,delete,move \
--format '%w%f' \
"${WATCH_DIRS[@]}" | while read file; do
if [[ "$file" =~ \.(c|cpp|h|hpp)$ ]]; then
echo "Change detected: $file"
stop_app
build_and_run
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment