Skip to content

Instantly share code, notes, and snippets.

@osrec
Created August 6, 2018 21:31
Show Gist options
  • Save osrec/120db1470ca9281188052d2ac4223d64 to your computer and use it in GitHub Desktop.
Save osrec/120db1470ca9281188052d2ac4223d64 to your computer and use it in GitHub Desktop.
Watch a particular directory for changes and kill and restart a process on change
# In the example below, the command we want to run is `php -f "$DIR/../run.php"` - change this on lines 30 and 46
# $DIR is just the directory containing this script file (computed automatically, but you can hardcode it if you want).
# Use $DIR to specify paths relative to the script's path.
# The PROCESS_NAME variable is the name of the process to kill and restart
# You must ensure your process has the same name each time it runs (we use a file at $DIR/../servername to store the name)
# Alternatively, you can hard code the PROCESS_NAME on line 15 if you like
#!/bin/bash
ulimit -n 100000
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
PROCESS_NAME=`cat $DIR/../servername`
# printf "$PROCESS_NAME\n"
pkill -f "$PROCESS_NAME"
function clean_up
{
# Perform program exit housekeeping
pkill -f -9 "$PROCESS_NAME"
exit
}
trap clean_up SIGHUP SIGINT SIGTERM
php -f "$DIR/../run.php" &
while true; do
inotifywait -e modify,create,delete -r $DIR/.. && \
pkill -f "$PROCESS_NAME"
while pkill -0 -f "$PROCESS_NAME"; do
sleep 0.5
done
printf "\n---\n"
php -f "$DIR/../run.php" &
printf "\n\n\n"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment