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/properties #/Scratch/wjkw1/macron-restoration/web/WEB-INF/classes
|
---|
29 |
|
---|
30 | echo "HELLO WORLD"
|
---|
31 |
|
---|
32 | # get rid of any old error files
|
---|
33 | if [ -e $errfile ] ; then
|
---|
34 | rm $errfile
|
---|
35 | fi
|
---|
36 |
|
---|
37 | if [ ! -d $macronlogs_folder ] ; then
|
---|
38 | echo "$macronlogs_folder does not exist. Terminating script $0." >> $errfile
|
---|
39 | fi
|
---|
40 |
|
---|
41 | # create these directories iff they don't exist
|
---|
42 | mkdir -p $processed_folder
|
---|
43 | mkdir -p $reprocess_folder
|
---|
44 |
|
---|
45 | pushd $macronlogs_folder > /dev/null
|
---|
46 | # "Bash does carry out filename expansion [1] -- a process known as globbing"
|
---|
47 | # https://www.tldp.org/LDP/abs/html/globbingref.html
|
---|
48 | IFS="$(printf '\n\t')" # Remove space.
|
---|
49 |
|
---|
50 | # Correct glob use:
|
---|
51 | # Always use for-loop, prefix glob, check if exists file.
|
---|
52 | for logfile in ./macron.log.* ; do # Use ./* ... NEVER bare *
|
---|
53 | if [ -e "$logfile" ] ; then # Check whether file exists.
|
---|
54 | echo "****** $0 script processing $logfile"
|
---|
55 |
|
---|
56 | # https://askubuntu.com/questions/625224/how-to-redirect-stderr-to-a-file
|
---|
57 |
|
---|
58 | # this works: java -cp /Scratch/wjkw1/mysql-connector-java-8.0.14/mysql-connector-java-8.0.14.jar:/Scratch/wjkw1/properties/mysql.properties:. util.MacroniserLogFileProcessor /home/wjkw1/comp520/MacroniserLogs/tmp/macron.log
|
---|
59 | #java -cp $jars_folder/mysql-connector-java-8.0.14.jar:$mysqlprops_folder:. util.MacroniserLogFileProcessor $logfile > /dev/null 2>> $errfile
|
---|
60 |
|
---|
61 | echo "java -cp $exec_folder:$jars_folder/mysql-connector-java-8.0.14.jar:$mysqlprops_folder:. util.MacroniserLogFileProcessor $logfile > /dev/null 2>> $errfile"
|
---|
62 | java -cp $exec_folder:$jars_folder/mysql-connector-java-8.0.14.jar:$mysqlprops_folder:. util.MacroniserLogFileProcessor $logfile
|
---|
63 |
|
---|
64 | # The above returns 0 on success, -1 on failure
|
---|
65 | # if 0, then move $logfile to processed_folder
|
---|
66 | # and if -1, move to reprocess_folder
|
---|
67 | # https://askubuntu.com/questions/324423/how-to-access-the-last-return-value-in-bash
|
---|
68 | # https://www.tldp.org/LDP/abs/html/comparison-ops.html
|
---|
69 | result=$?
|
---|
70 | if [ "$result" -eq "0" ]; then
|
---|
71 | echo "Done processing $logfile, moving to $processed_folder"
|
---|
72 | mv $logfile "$processed_folder/."
|
---|
73 | else
|
---|
74 | echo "Need to reprocess $logfile, moving to $reprocess_folder" >> $errfile
|
---|
75 | mv $logfile "$reprocess_folder/."
|
---|
76 | fi
|
---|
77 | fi
|
---|
78 | done
|
---|
79 |
|
---|
80 | IFS="$(printf '\n\t')" # Remove space.
|
---|
81 |
|
---|
82 | # Correct glob use:
|
---|
83 | # Always use for-loop, prefix glob, check if exists file.
|
---|
84 | for file in ./macron.log.* ; do # Use ./* ... NEVER bare *
|
---|
85 | if [ -e "$file" ] ; then # Check whether file exists.
|
---|
86 | echo "$file"
|
---|
87 | fi
|
---|
88 | done
|
---|
89 |
|
---|
90 | echo "GOODBYE"
|
---|
91 |
|
---|
92 | # If error file exists and has non-zero size, send email
|
---|
93 | # https://stackoverflow.com/questions/30080997/if-file-is-exists-and-is-not-empty-always-gives-me-the-false-value
|
---|
94 | if [ -s $errfile ] ; then
|
---|
95 | # send email with contents of error file
|
---|
96 | echo "cat $errfile | mail -s 'Macron.log to DB cron failure output' $email_address"
|
---|
97 | # TODO: uncomment on a machine that's allowed to email
|
---|
98 | #cat $errfile | mail -s 'Macron.log to DB cron failure output' $email_address
|
---|
99 |
|
---|
100 |
|
---|
101 | #echo "Supposed to have sent MAIL by now."
|
---|
102 | fi
|
---|
103 |
|
---|
104 | popd
|
---|
105 |
|
---|
106 | # Cronjob: https://stackoverflow.com/questions/8938120/how-to-run-cron-once-daily-at-10pm
|
---|
107 |
|
---|
108 | # In an x-term, on a machine:
|
---|
109 | #
|
---|