#! /bin/bash ################################### # Set email_address only if you have permissions to run the mail client # on your linux box email_address=wilsonw9378@gmail.com #tetaka1@gmail.com #macronLogToDBjar= exec_folder=@logfileproc.exec_folder@ #macronizer/web/classes (containing util.MacroniserLogFileProcessor) macronlogs_folder=@macronizer.logs@ #macronizer/web/logs processed_folder=@logfileproc.dir@/processed #/macronizer/logproc/processed reprocess_folder=@logfileproc.dir@/reprocess #/macronizer/logproc/reprocess errfile=@logfileproc.dir@/macronLogToDB.err #/macronizer/logproc/macronLogToDB.err jars_folder=@jarlibs.folder@ #macronizer/web/WEB-INF/lib mysqlprops_folder=@classes.folder@ #/macronizer/web/WEB-INF/classes echo "" echo "In script $0 to process macron.log.* files" echo " Note that any logfile named exactly 'macron.log' (instead of matching macron.log.*) will not be processed" # get rid of any old error files if [ -e $errfile ] ; then rm $errfile fi if [ ! -d $macronlogs_folder ] ; then echo "$macronlogs_folder does not exist. Terminating script $0." >> $errfile fi # create these directories iff they don't exist mkdir -p $processed_folder mkdir -p $reprocess_folder # "Bash does carry out filename expansion [1] -- a process known as globbing" # https://www.tldp.org/LDP/abs/html/globbingref.html IFS="$(printf '\n\t')" # Remove space. # Correct glob use: # Always use for-loop, prefix glob, check if exists file. for logfile in $macronlogs_folder/macron.log.* ; do # Use ./* ... NEVER bare * [EDIT: if in current folder?] if [ -e "$logfile" ] ; then # Check whether file exists. echo "****** processing $logfile" # https://askubuntu.com/questions/625224/how-to-redirect-stderr-to-a-file echo "Running command:" echo " java -cp .:$exec_folder:$jars_folder/mysql-connector-java-8.0.14.jar:$mysqlprops_folder util.MacroniserLogFileProcessor $logfile 2>> $errfile" echo "" java -cp .:$exec_folder:$jars_folder/mysql-connector-java-8.0.14.jar:$mysqlprops_folder util.MacroniserLogFileProcessor $logfile 2>> $errfile # The above returns 0 on success, -1 on failure # if 0, then move $logfile to processed_folder # and if -1, move to reprocess_folder # https://askubuntu.com/questions/324423/how-to-access-the-last-return-value-in-bash # https://www.tldp.org/LDP/abs/html/comparison-ops.html result=$? if [ "$result" -eq "0" ]; then echo "Done processing $logfile, moving to $processed_folder" mv $logfile "$processed_folder/." else echo "Need to reprocess $logfile, moving to $reprocess_folder" >> $errfile mv $logfile "$reprocess_folder/." fi fi done IFS="$(printf '\n\t')" # Remove space. # Correct glob use: # Always use for-loop, prefix glob, check if exists file. #for file in ./macron.log.* ; do # Use ./* ... NEVER bare * # if [ -e "$file" ] ; then # Check whether file exists. # echo "$file" # fi #done echo "Finished processing macron.log.* files in $macronlogs_folder" echo "" # if email address provided, attempt to send email #if [[ -z "${//$email_address }" ]]; then # If error file exists and has non-zero size, send email # https://stackoverflow.com/questions/30080997/if-file-is-exists-and-is-not-empty-always-gives-me-the-false-value if [ -s $errfile ] ; then # send email with contents of error file echo "cat $errfile | mail -s 'Macron.log to DB cron failure output' $email_address" # TODO: uncomment on a machine that's allowed to email #cat $errfile | mail -s 'Macron.log to DB cron failure output' $email_address echo "Supposed to have sent MAIL by now to $email_address" fi #fi # Cronjob: https://stackoverflow.com/questions/8938120/how-to-run-cron-once-daily-at-10pm # In an x-term, on a machine: #