Bash script to put log files into single file and email
I am looking for a bash script that will take my log files, put them into a single file, and email that file to my email account. This is what I have so far, but it doesn't work, and besides I know there is a more effective way to do the same thing.
Code:
#!/bin/bash Code:
Collecting Logs Thanks, DragonM15 |
There is a typo in your script. You did not define
MYFILENAME, instead, you defined MYFILEMAME -- MYFILEMAME="$HOME/systats" !(MAME should be NAME) |
Ok, I admit I feel like a complete and utter moron for that one. But now for the next hump:
Code:
Collecting Logs Code:
#!/bin/bash Thanks, DragonM15 |
If you read this post before, I DID NOT fix the problem. I only eliminated the error messages by putting '''s around the $LOGDATE 's. It still wont output the data to the file. Any ideas?
Thanks, DragonM15 |
Without changing the structure of the script, a proper construct is
Code:
echo "/var/log/secure" >> $MYFILENAME The errors you obtain are related to the interpretation of the LOGDATE and APACHEACDATE variables. As you already discovered the quotes are useful to let the shell interpret a string literally, despite of the presence of special characters or blank spaces, but you should put every variable inside quotes when you use it. In other words, it is not enough to put quotes only in the variable assigments at the beginning of the script. Every time the shell encounter a variable, expands it whitout preserving the surrounding quotes. So, the line Code:
cat /var/log/secure | $GREP $LOGDATE >> $MYFILENAME Code:
cat /var/log/secure | /usr/bin/grep Oct 8 >> /home/user/systats Code:
cat /var/log/apache/access_log | $GREP $APACHEACDATA >> $MYFILENAME Just a note about debugging: executing the script by "bash -x" will give the execution trace with all the variable expansions performed by the shell. The output can be difficult to read, but it gives many more debugging info. Another hint is to look for differences in the usage of single quotes ' and double quotes ". |
As stated above you should run the script with bash -x scriptname.sh and you will see a lot of errors (if there is any). I used too think that using the -x option didn't help me much...but now I use it all the time for debugging...
-custangro ps I like using ksh...just my preferance... ;) |
Hi,
There should be double quotes around the $LOGDATE part. This: cat /var/log/apache/error_log | $GREP $LOGDATE >> $MYFILENAME Should be: cat /var/log/apache/error_log | $GREP "$LOGDATE" >> $MYFILENAME Grep sees this: grep oct 09 infile as grep oct in files 09 and infile, so the oct 09 part should be: grep "oct 09" infile. Hope this helps. |
Worked like a charm colucix, thanks for the help...
|
cat file file2 file3 | mailx -vs "logs" user@blah.com
|
The wait statements in the OP are not necessary. wait will pause until background jobs have completed, but you are not running the cat commands in the background.
i.e. this might make sense: Code:
command1 & In the case where you are appending files somewhere, running in parallel is probably not what you want to do, unless you want all the data mixed up. Something else which may be of interest to you: the more command, when called with multiple input files named as arguments, and re-directed to a file will prepend the content of each file with Code:
:::::::::::::: Code:
more /var/log/secure /var/log/messages /var/log/maillog ... > "$MYFILENAME" Code:
more /var/log/secure \ |
Quote:
DragonM15, to make the script more generic, you may want to generate the date you're looking for, e.g., Code:
LOGDATE=`date -d yesterday '+%b %e'` Also beware that when the log is rolled, you might miss a part. E.g., my /var/log/messages gets rolled every Sunday at 4:02; presumably the FC2 default. In that case, some relevant messages may be in /var/log/messages.1. You could add that as another argument to grep , e.g., Code:
$GREP -h "^$LOGDATE" /var/log/messages.1 /var/log/messages >> $MYFILENAME If you're willing to change the structure, you could reduce code duplication by using a loop (and curly braces): Code:
for log in /var/log/{secure,messages,maillog,apache/error_log,syslog}; do BTW, if you'd like a ready-made solution, look at LogWatch. It's running on my machine, and it does a decent job of summarizing all noteworthy log entries in a daily e-mail. |
Quote:
Thanks, DragonM15 |
Did you read Quigi's post?
This might work, too: Code:
F=<your_output_file>
|
Quote:
Awfiully sorry I haven't got time to sit there and work out a perfect solution, but it was a pointer to an easy way to mail log files. surely you could work out how to limit it to today's log file only? phew |
All times are GMT -5. The time now is 04:25 AM. |