Last active
July 21, 2025 22:39
-
-
Save thetoine/d50a814bc8a07a9b4209cf86669b0840 to your computer and use it in GitHub Desktop.
supervisor process restart when PHP files are updated with a delayed timer preventing chain reaction of restarts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
WATCH_DIR="/your-path/" | |
PROCESS_NAME="supervisor-process-name" | |
RESTART_DELAY=30 | |
TIMER_FILE="/tmp/supervisor_restart_timer" | |
echo "Watching $WATCH_DIR for changes..." | |
# echo "Watch $1 for file changes..." | |
# Be careful not to confuse the output of `inotifywait` with that of `echo`. | |
# e.g. missing a `\` would break the pipe and write inotifywait's output, not | |
# the echo's. | |
inotifywait \ | |
--monitor \ | |
-r -e close_write,modify $WATCH_DIR \ | |
--timefmt '%Y-%m-%dT%H:%M:%S' \ | |
--format '%T %w %f %e' \ | |
| while read datetime dir filename event; do | |
echo $filename; | |
if [[ $filename == *.php ]]; then | |
echo "$datetime: $filename was $event" | |
# If timer file doesn't exist, create it and start countdown | |
if [ ! -f "$TIMER_FILE" ]; then | |
echo "Starting restart countdown for $RESTART_DELAY seconds." | |
touch "$TIMER_FILE" | |
# Background restart process | |
( | |
sleep $RESTART_DELAY | |
if [ -f "$TIMER_FILE" ]; then | |
echo "$(date): Executing restart of $PROCESS_NAME" | |
supervisorctl restart "$PROCESS_NAME" | |
rm -f "$TIMER_FILE" | |
fi | |
) & | |
else | |
echo "Restart already scheduled, ignoring change" | |
fi | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment