root/other-projects/the-macronizer/trunk/src/scripts-and-sql/macronLogToDB.bash.in @ 32753

Revision 32753, 3.8 KB (checked in by ak19, 17 months ago)

Western Wilson stuff. Automating MacroniserLogFileProcessor? setup from the build.xml

  • Property svn:executable set to *
Line 
1#! /bin/bash
2
3
4###################################
5
6# Set email_address only if you have permissions to run the mail client
7# on your linux box
8email_address=wilsonw9378@gmail.com #tetaka1@gmail.com
9
10
11#macronLogToDBjar=
12exec_folder=@logfileproc.exec_folder@ #macronizer/web/classes (containing util.MacroniserLogFileProcessor)
13macronlogs_folder=@macronizer.logs@ #macronizer/web/logs
14processed_folder=@logfileproc.dir@/processed #/macronizer/logproc/processed
15reprocess_folder=@logfileproc.dir@/reprocess #/macronizer/logproc/reprocess
16errfile=@logfileproc.dir@/macronLogToDB.err #/macronizer/logproc/macronLogToDB.err
17jars_folder=@jarlibs.folder@ #macronizer/web/WEB-INF/lib
18mysqlprops_folder=@classes.folder@ #/macronizer/web/WEB-INF/classes
19
20echo ""
21echo "In script $0 to process macron.log.* files"
22echo "  Note that any logfile named exactly 'macron.log' (instead of matching macron.log.*) will not be processed"
23
24# get rid of any old error files
25if [ -e $errfile ] ; then
26    rm $errfile
27fi
28
29if [ ! -d $macronlogs_folder ] ; then
30    echo "$macronlogs_folder does not exist. Terminating script $0." >> $errfile
31fi
32
33# create these directories iff they don't exist
34mkdir -p $processed_folder
35mkdir -p $reprocess_folder
36
37
38# "Bash does carry out filename expansion [1] -- a process known as globbing"
39# https://www.tldp.org/LDP/abs/html/globbingref.html
40IFS="$(printf '\n\t')"   # Remove space.
41
42#  Correct glob use:
43#  Always use for-loop, prefix glob, check if exists file.
44for logfile in $macronlogs_folder/macron.log.* ; do         # Use ./* ... NEVER bare * [EDIT: if in current folder?]
45    if [ -e "$logfile" ] ; then   # Check whether file exists.
46      echo "****** processing $logfile"     
47
48      # https://askubuntu.com/questions/625224/how-to-redirect-stderr-to-a-file
49
50      echo "Running command:"
51      echo "   java -cp .:$exec_folder:$jars_folder/mysql-connector-java-8.0.14.jar:$mysqlprops_folder util.MacroniserLogFileProcessor $logfile 2>> $errfile"
52      echo ""
53      java -cp .:$exec_folder:$jars_folder/mysql-connector-java-8.0.14.jar:$mysqlprops_folder util.MacroniserLogFileProcessor $logfile 2>> $errfile
54     
55     
56      # The above returns 0 on success, -1 on failure
57      # if 0, then move $logfile to processed_folder
58      # and if -1, move to reprocess_folder
59      # https://askubuntu.com/questions/324423/how-to-access-the-last-return-value-in-bash
60      # https://www.tldp.org/LDP/abs/html/comparison-ops.html
61      result=$?
62      if [ "$result" -eq "0" ]; then
63      echo "Done processing $logfile, moving to $processed_folder"
64      mv $logfile "$processed_folder/."
65      else
66      echo "Need to reprocess $logfile, moving to $reprocess_folder" >> $errfile
67      mv $logfile "$reprocess_folder/."
68      fi
69  fi
70done
71
72IFS="$(printf '\n\t')"   # Remove space.
73
74#  Correct glob use:
75#  Always use for-loop, prefix glob, check if exists file.
76#for file in ./macron.log.* ; do         # Use ./* ... NEVER bare *
77#  if [ -e "$file" ] ; then   # Check whether file exists.
78#     echo "$file"
79#  fi
80#done
81
82echo "Finished processing macron.log.* files in $macronlogs_folder"
83echo ""
84
85# if email address provided, attempt to send email
86#if [[ -z "${//$email_address }" ]]; then
87
88# If error file exists and has non-zero size, send email
89# https://stackoverflow.com/questions/30080997/if-file-is-exists-and-is-not-empty-always-gives-me-the-false-value
90
91if [ -s $errfile ] ; then   
92    # send email with contents of error file
93    echo "cat $errfile | mail -s 'Macron.log to DB cron failure output' $email_address"
94    # TODO: uncomment on a machine that's allowed to email
95    #cat $errfile | mail -s 'Macron.log to DB cron failure output' $email_address       
96   
97    echo "Supposed to have sent MAIL by now to $email_address"
98fi
99#fi
100
101
102# Cronjob: https://stackoverflow.com/questions/8938120/how-to-run-cron-once-daily-at-10pm
103
104# In an x-term, on a machine:
105#
Note: See TracBrowser for help on using the browser.