Skip to content

Instantly share code, notes, and snippets.

@ohgyun
Last active December 11, 2015 04:19
Show Gist options
  • Save ohgyun/4544524 to your computer and use it in GitHub Desktop.
Save ohgyun/4544524 to your computer and use it in GitHub Desktop.
A bash file monitor. Watch a bashfile and run if it changed.
#!/bin/bash
# Watch a bashfile and run it if changed
function bash_monitor () {
if [[ -z "$1" ]]; then
echo "usage: bash_monitor <bashfilename>"
exit 1
fi
local file="$1"
local tmp1="$HOME/.tmp_bash_monitor_1"
local tmp2="$HOME/.tmp_bash_monitor_2"
# create temporary files to compare status
touch "$tmp1" "$tmp2"
while true; do
ls -l "$file" > "$tmp1"
diff "$tmp1" "$tmp2" > /dev/null \
|| {
echo
echo '[BASH MONITOR] File changed.'
echo '----------------------------'
# run bash with parameters from second if changed
source "$file" "${@:2}"
}
cp "$tmp1" "$tmp2"
sleep 1
done
}
bash_monitor "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment