Skip to content

Instantly share code, notes, and snippets.

@njregenwether
Created November 24, 2017 20:34
Show Gist options
  • Select an option

  • Save njregenwether/42688d77792b002071da7d88375b0cf5 to your computer and use it in GitHub Desktop.

Select an option

Save njregenwether/42688d77792b002071da7d88375b0cf5 to your computer and use it in GitHub Desktop.
#!/bin/bash
##########################################################################################################################
# This is a template for a script that only runs when it's not already running. The first thing this will do is to check #
# to make sure that it is not running. #
# #
# Then this script will check to see if the latest file it has processed is its 'touched' file which is compares the MAC #
# times to ensure it only runs if there is work to be done. #
# #
# The idea is that you should schedule this in a cron job. Here is my example of how I scheduled my script. #
# #
# */5 * * * * PerformActionOnNewFilesOnly.sh >> statuslog 2>> errorlog #
##########################################################################################################################
LOCKFILE="/directory/.moverscriptrunning"
if [ -e "${LOCKFILE}" ]; then
echo "Script already running."
exit 1
else
HEADSEP="================================================================================================================"
REGSEP="----------------------------------------------------------------------------------------------------------------"
NEWL=""
echo $HEADSEP
echo "PerformActionOnNewFilesOnly.sh is starting the rsync of somewhere to somewhere else."
echo `date`
echo $HEADSEP
touch "${LOCKFILE}"
# I use this for an rsync, then the follow up sections to change permissions or do whatever else is needed. Modify this
echo $NEWL
echo $REGSEP
echo "Starting copy"
rsync -rihPtz --ignore-existing user@server:directory localdirectory
echo "Finished"
echo $NEWL
# This is the file to check if there are any new files since this last processed.
NEWFILECHECK="$(ls -t /directory/ | head -1 )"
if [ $NEWFILECHECK != "lastscanned" ]; then
# Example of something you might want to do, modify this as needed.
echo $REGSEP
echo "Starting chown-ing of directory"
chown -R user:user directory/*
echo "Finished"
echo $NEWL
echo $REGSEP
echo "Starting chmod-ing of directory"
chmod -R 770 directory/*
echo "Finished"
echo $NEWL
# End of example
# Add whatever processing needs to be done here.
# End whatever processing needs to be done here.
touch /directory/lastscanned
else
echo $REGSEP
echo "No new files."
fi
echo "PerformActionOnNewFilesOnly.sh finished and exiting."
echo $HEADSEP
echo $NEWL
echo $NEWL
rm "${LOCKFILE}"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment