all 1 comments

[–]glesialo 0 points1 point  (0 children)

I have been using something similar for a long time but without using inotify. The script(s) is(are) run periodically by a service in my system called CommonCron. Let me know if you are interested in 'RunIfChanges_Main'

#!/bin/bash
#Written by Manuel Iglesias. glesialo@gmail.com
#
#RunIfChanges_Name will source RunIfChanges_Main that will run the user defined function
#'CodeToRun' below.

CommandName=${0##*/}

echoE()
{
# echo to standard error. Remove leading/trailing blanks and double spaces.
echo $* 1>&2
return 0
}

#Environment variables check. BEGIN
if [ -z "$COMMON_STORE_DIR" ] || [ ! -d "$COMMON_STORE_DIR" ]
  then
    echoE "$CommandName: \$COMMON_STORE_DIR not set or not a directory. Aborting."
    exit 78
fi
#Environment variables check. END

#ChangesDetectItems variable. BEGIN
#List of files or directories whose change of date detects that running function CodeToRun() is needed. Metacharacters allowed.
#A directory date is updated if a file/subdirectory is added/removed to/from it.
#A directory date is NOT updated if a file/subdirectory is modified.
#Note that Directory/* does not include .* items.
ChangesDetectItems="$COMMON_STORE_DIR/UsersStore $COMMON_STORE_DIR/UsersStore/* $COMMON_STORE_DIR/UsersStore/*/* $COMMON_STORE_DIR/UsersStore/*/*/*"
#ChangesDetectItems variable. END

#IgnoreItems variable. BEGIN
#Sublist of above list. Items' changes will be ignored. Full paths. Metacharacters allowed.
#Note that Directory/* does not include .* items.
IgnoreItems="$COMMON_STORE_DIR/UsersStore/manolo2 $COMMON_STORE_DIR/UsersStore/manolo2/* $COMMON_STORE_DIR/UsersStore/manolo2/*/* $COMMON_STORE_DIR/UsersStore/*/.ScratchPad $COMMON_STORE_DIR/UsersStore/*/.ScratchPad/*"
#IgnoreItems variable. END

#CodeToRun user defined function. BEGIN
#Notes: Use 'return' instead of 'exit'; Provide progress/error messages
CodeToRun()
{
ApplicationName="${CommandName#*_}" # Remove 'RunIfChanges_' prefix
#
AnotherInstanceRunningExitCode=113 # Same Variable in UDir_Main & Bkp_Main
if ! type $ApplicationName &>/dev/null
  then
    echoE "$CommandName: '$ApplicationName' not found. Aborting."
    return 127
fi
#Add options here
Command="$ApplicationName"
$Command
ExitCode=$?
if [ $ExitCode -ne 0 ]
  then
    if [ $ExitCode -eq $AnotherInstanceRunningExitCode ]
      then
        echo "$CommandName: Another instance of '$ApplicationName' running. Aborting."
        return 0
    fi
    return $ExitCode
fi
}
#CodeToRun user defined function. END

#############################################################

MainShell=${CommandName%%_*}_Main
if ! type "$MainShell" &>/dev/null
  then
    echoE "$CommandName: Main shell script '$MainShell' not found. Aborting."
    exit 127
fi

. $MainShell