Linux send logs to my email every 4 hours

Hello, I need to create a Linux script that will send the faillog and the maillog located in var/log to my email address every 4 hours.
toocoolamdAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

farzanjCommented:
All you need is a cron entry to run every 4 hours

Create a schedule
crontab -e


* */4 * * * mutt -a /path/faillog -a /var/log/maillog -s "Mail log $(date)" Email@address.com

Open in new window

farzanjCommented:
Other things you need to consider is whether you would need the entire files of just last few lines in each.  You could even put the lines since the last time you had sent.  Also, you need to decide whether you would need to send these logs in the body of email or as some attachment.  In any case it should be scheduled using the cron.

toocoolamdAuthor Commented:
If I could just put the lines from the last time it sent in the body of the email that would be great. I am a Linux noob so any help I can get is appreciated. If I wanted to use the command above would It just be set up as:
#!/bin/bash
* */4 * * * mutt -a /path/faillog -a /var/log/maillog -s "Mail log $(date)" Email@address.com

exit
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

farzanjCommented:
Suppose you want to see the last ten lines in the body of your email, then here would be the code to do this

Create file ~/myscript.sh
#!/bin/bash

TMPFILE="~/tmpfile"
EMAIL_ADD="myemail@company.com"

echo "** FAIL LOG **" > $TMPFILE
tail -10 /path/faillog >> $TMPFILE
echo "** MAIL LOG **" >> $TMPFILE
tail -10 /var/log/maillog >> $TMPFILE

cat $TMPFILE | mail -s "MAIL LOGS $(date)" $EMAIL_ADD

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
farzanjCommented:
If you want to send the log lines since the last time you sent it, you will have to keep a copy of your last send message.  Lets try this:

You can use the previous version or you can use this, if you like
Please adjust the names of MAILLOG and FAILLOG


#!/bin/bash


#FILES
FAILLOG=/path/faillog
MAILLOG=/var/log/maillog
FAILLOG_TMP=/home/mydir/faillog
MAILLOG_TMP=/home/mydir/maillog
TMPFILE=/home/mydir/tmpfile

echo "** FAILLOG **" > $TMPFILE
diff $FAILLOG $FAILLOG_TMP | egrep "[<>]" >> $TMPFILE
echo "** MAILLOG **" >> $TMPFILE
diff $MAILLOG $MAILLOG_TMP | egrep "[<>]" >> $TMPFILE

#Update the TMP FILES
cp $MAILLOG $MAILLOG_TMP
cp $FAILLOG $FAILLOG_TMP

cat $TMPFILE | mail -s "MAIL LOGS $(date)" $EMAIL_ADD

Open in new window

toocoolamdAuthor Commented:
Thank you so much! I don't want to sound to stupid here, but Which MAILLOG and FAILLOG do I need to change? and why? Just curious. thank you.
farzanjCommented:
No question stupid, it is better to clarify.

I mean adjust the paths, so your actual MAILLOG file may be, suppose

/var/log/sendmail

Then you have to write

MAILLOG=/var/log/sendmail

I meant that you have to make sure those file exist and if the names are little different put the actual names.

Lastly, you need to make the script executable and put it in the cron

chmod +x /path/script

crontab -e -u <USER>
* */4 * * * /path/script

Again /path/script is just a supposed name.  Adjust this name to may be
/home/toocoolamd/myscript.sh
farzanjCommented:
Please feel free to clarify
toocoolamdAuthor Commented:
Okay!! That makes sense now! Thank you soo much! I'm going to test this all out here shortly! I appreciate it greatly!
farzanjCommented:
Not a problem.  Glad to help and if you see any error messages, please do NOT panic.  Just let me know.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux

From novice to tech pro — start learning today.