Skip to content

Instantly share code, notes, and snippets.

@arttuladhar
Created August 5, 2015 20:22
Show Gist options
  • Save arttuladhar/1a7d47e89f6e7aea7866 to your computer and use it in GitHub Desktop.
Save arttuladhar/1a7d47e89f6e7aea7866 to your computer and use it in GitHub Desktop.
Scheduled Auto File Move/Copy Script
#Scheduled Auto File Move/Copy Script
#------------------------------------
# #Filename : config
# #Config File for File Dropper
# AUTHOR=Aayush Tuladhar
# DESTINATION_DIR=/appserver/userDocs
# 5 -- 5 Sec
# 300 - 5 Minutes
# 900 -- 15 Minutes
# 1200 - 20 Minutes
# WAIT_TIME=500
# BATCH_NUM=1
# SOURCE_DIR=/home/asadmin/art/file_dropper/Source/*.txt
#!/bin/bash
CONFIG_FILENAME="config"
# Reading Configuration from CONFIG_FILENAME
echo "Reading Config"
AUTHOR=$(readProp AUTHOR $CONFIG_FILENAME)
DIR=$(readProp DESTINATION_DIR $CONFIG_FILENAME)
WAIT_TIME=$(readProp WAIT_TIME $CONFIG_FILENAME)
BATCH_NUM=$(readProp BATCH_NUM $CONFIG_FILENAME)
SOURCE_DIR=$(readProp SOURCE_DIR $CONFIG_FILENAME)
echo $AUTHOR
echo $DIR
echo $WAIT_TIME
echo $BATCH_NUM
#Running as Demon Process
while true; do
. config_read.sh
echo "Executing Process"
#Check if there is file in /userdocs
#-- Start Hack to Remove Files if gets clogged --#
userdoc_num=0
# Sleep for 5 Minutes if file exists in DIR
while [ "$(ls -A $DIR)" ]; do
echo "Loading Files to "$DIR
num=0
FILE=$SOURCE_DIR
for f in $FILE
do
echo $num
echo "Copying File"
if [[ -f $f ]]; then
echo $f
# cat $f
cd /appserver/userDocs
cp $f .
echo $f
echo "Copy Complete"
rm $f
echo "Remove Complete"
let num++
fi
if [ $num -eq $BATCH_NUM ]; then
echo "Sleeping"
break
fi
done
if [ $num -eq 0 ]; then
echo "No Files left in " $DIR
echo "Deamon in Sleep Mode for $WAIT_TIME sec"
# Deamon in Sleep Mode
# exit 0
fi
sleep $WAIT_TIME
done
# Reads Configuration file
# Format : KEY=VALUE
# Ignores Comments
function readProp {
# Arg 1 : Properties Term
# Arg 2 : FileName
while read line
do
# echo "$line"
pattern="$1=(.+)"
# echo $pattern
if [[ $line =~ $pattern ]]; then
# echo "Matched"
echo "${BASH_REMATCH[1]}"
fi
done < $2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment