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
|
---|
8 | [email protected] #[email protected]
|
---|
9 |
|
---|
10 |
|
---|
11 | #macronLogToDBjar=
|
---|
12 | [email protected]_folder@ #macronizer/web/classes (containing util.MacroniserLogFileProcessor)
|
---|
13 | [email protected]@ #macronizer/web/logs
|
---|
14 | [email protected]@/processed #/macronizer/logproc/processed
|
---|
15 | [email protected]@/reprocess #/macronizer/logproc/reprocess
|
---|
16 | [email protected]@/macronLogToDB.err #/macronizer/logproc/macronLogToDB.err
|
---|
17 | [email protected]@ #macronizer/web/WEB-INF/lib
|
---|
18 | [email protected]@ #/macronizer/web/WEB-INF/classes
|
---|
19 |
|
---|
20 | echo ""
|
---|
21 | echo "In script $0 to process macron.log.* files"
|
---|
22 | echo " 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
|
---|
25 | if [ -e $errfile ] ; then
|
---|
26 | rm $errfile
|
---|
27 | fi
|
---|
28 |
|
---|
29 | if [ ! -d $macronlogs_folder ] ; then
|
---|
30 | echo "$macronlogs_folder does not exist. Terminating script $0." >> $errfile
|
---|
31 | fi
|
---|
32 |
|
---|
33 | # create these directories iff they don't exist
|
---|
34 | mkdir -p $processed_folder
|
---|
35 | mkdir -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
|
---|
40 | IFS="$(printf '\n\t')" # Remove space.
|
---|
41 |
|
---|
42 | # Correct glob use:
|
---|
43 | # Always use for-loop, prefix glob, check if exists file.
|
---|
44 | for 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
|
---|
70 | done
|
---|
71 |
|
---|
72 | IFS="$(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 |
|
---|
82 | echo "Finished processing macron.log.* files in $macronlogs_folder"
|
---|
83 | echo ""
|
---|
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 |
|
---|
91 | if [ -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"
|
---|
98 | fi
|
---|
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 | #
|
---|