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 | ###################################
|
---|
20 | macronlogs_folder=/home/wjkw1/comp520/MacroniserLogs/tmp #/home/wjkw1/comp520/MacroniserLogs/logs #/Scratch/wjkw1/macron-restoration/web/logs
|
---|
21 | #macronLogToDBjar=
|
---|
22 | exec_folder=/home/wjkw1/IdeaProjects/LogFileExtraction/out/production/LogFileExtraction
|
---|
23 | processed_folder=/Scratch/wjkw1/processed #/Scratch/wjkw1/macron-restoration/processed
|
---|
24 | reprocess_folder=/Scratch/wjkw1/reprocess #/Scratch/wjkw1/macron-restoration/reprocess
|
---|
25 | errfile=/Scratch/wjkw1/macronLogToDB.err #/Scratch/wjkw1/macron-restoration/macronLogToDB.err
|
---|
26 | [email protected] #[email protected]
|
---|
27 | jars_folder=/Scratch/wjkw1/mysql-connector-java-8.0.14 #/Scratch/wjkw1/macron-restoration/web/WEB-INF/lib
|
---|
28 | mysqlprops_folder=/Scratch/wjkw1/classes #/Scratch/wjkw1/macron-restoration/web/WEB-INF/classes
|
---|
29 |
|
---|
30 | echo ""
|
---|
31 | echo "In script $0 to process macron.log.* files"
|
---|
32 | echo " 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
|
---|
35 | if [ -e $errfile ] ; then
|
---|
36 | rm $errfile
|
---|
37 | fi
|
---|
38 |
|
---|
39 | if [ ! -d $macronlogs_folder ] ; then
|
---|
40 | echo "$macronlogs_folder does not exist. Terminating script $0." >> $errfile
|
---|
41 | fi
|
---|
42 |
|
---|
43 | # create these directories iff they don't exist
|
---|
44 | mkdir -p $processed_folder
|
---|
45 | mkdir -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
|
---|
50 | IFS="$(printf '\n\t')" # Remove space.
|
---|
51 |
|
---|
52 | # Correct glob use:
|
---|
53 | # Always use for-loop, prefix glob, check if exists file.
|
---|
54 | for 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
|
---|
80 | done
|
---|
81 |
|
---|
82 | IFS="$(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 |
|
---|
92 | echo "Finished processing macron.log.* files"
|
---|
93 | echo ""
|
---|
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
|
---|
97 | if [ -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."
|
---|
105 | fi
|
---|
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 | #
|
---|