From 1212f892fd4395622b44be75ddbfeff73c8889fd Mon Sep 17 00:00:00 2001 From: Andrew Tomaka Date: Fri, 6 Mar 2015 07:55:58 -0500 Subject: [PATCH] Add watchd to watch directories watchd will poll the sha1sum of a specified directory listing and notify whenever that sha1sum changes. This allows active monitoring of all files in that specific directory. --- bin/watchd | 99 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100755 bin/watchd diff --git a/bin/watchd b/bin/watchd new file mode 100755 index 0000000..b4ffcbb --- /dev/null +++ b/bin/watchd @@ -0,0 +1,99 @@ +#!/bin/bash + +readonly PROGRAM_NAME=$(basename "$0") +readonly PROGRAM_LOC=$(readlink -m "$(dirname $0)") +readonly ARGS="$@" +readonly ARG_COUNT="$#" + +LAST_SHA=0 + +usage () { + echo "usage: $PROGRAM_NAME [DIRECTORY] [WAIT_TIME]" +} + +error () { + local readonly message=$1; shift + + echo "$PROGRAM_NAME: $message" + usage + exit 1 +} + +current_sha () { + ls -lR "$DIRECTORY" \ + | sha1sum +} + +sha_changed? () { + [[ "$(current_sha)" != "$LAST_SHA" ]] +} + +update_sha () { + LAST_SHA=$(current_sha) +} + +alert_update () { + now=$(date +"%Y-%m-%d %I:%M:%S") + echo "[$now] Directory Updated" +} + +is_dir? () { + local readonly directory=$1; shift + + [[ -d $directory ]] +} + +is_int? () { + local readonly number=$1; shift + + [[ $number =~ ^[0-9]+$ ]] +} + +is_empty? () { + local var=$1; shift + + [[ -z $var ]] +} + +check_arguments () { + if is_empty? "$DIRECTORY" || is_empty? "$WAIT_TIME"; then + error "missing operand" + fi + + if ! is_dir? "$DIRECTORY"; then + error "$DIRECTORY is not a directory" + fi + + if ! is_int? "$WAIT_TIME"; then + error "$WAIT_TIME is not an integer" + fi +} + +run () { + while true; do + if sha_changed?; then + update_sha + alert_update + fi + + sleep "$WAIT_TIME" + done +} + +main () { + local readonly d=$1; shift + local readonly w=$1; shift + + readonly DIRECTORY=${d:-$(pwd)} + readonly WAIT_TIME=${w:-15} + + check_arguments + update_sha + + echo "--------------------------------------------------------------------------------" + echo "+ Monitoring $DIRECTORY at interval $WAIT_TIME seconds" + echo "--------------------------------------------------------------------------------" + run +} + +main $ARGS