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

Revision 32749, 4.1 KB (checked in by ak19, 20 months ago)

More Western Wilson stuff: finally got the bash script working. Cron task file is still a sample at present

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