Skip to content

Instantly share code, notes, and snippets.

@lmas
Last active March 25, 2021 21:10
Show Gist options
  • Save lmas/ac50a40dcaaf4311e019 to your computer and use it in GitHub Desktop.
Save lmas/ac50a40dcaaf4311e019 to your computer and use it in GitHub Desktop.
rerun - Simple script to rerun a command when files in the current directory has been modified
#!/bin/bash
COMMAND=$@
DIR=$PWD
function block_for_change {
inotifywait -q -q -r -e modify $DIR
}
function kill_command {
# TODO BUG: When $COMMAND contains special regexp chars, like (), pgrep will
# fail to find the command in the process tree.
pid=$(pgrep -xf "$COMMAND")
if [ "$pid" ]; then
kill -s SIGKILL -$pid
fi
}
function run_command {
setsid $COMMAND &
}
trap kill_command INT
echo -e ">> Watching for file changes in current directory, press Ctrl+C to stop.\n"
run_command
while block_for_change; do
kill_command
run_command
echo -e "\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n"
done
@lmas
Copy link
Author

lmas commented Mar 25, 2021

This old, dirty thing is too buggy and shit! Discontinued.

Go see this new version that works waayy better.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment