LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-29-2004, 04:45 AM   #1
akilles
Member
 
Registered: Sep 2004
Location: Vestfold, Norway
Distribution: Slackware
Posts: 139

Rep: Reputation: 15
Question Scripting and mailing


Hi.
I have been "playing" with Linux for a while now, and I have a DNS and Postfix mailserver running on my LAN. The server has no contact with the outside world, which it shouldn't. It's just there for testing purposes. (I use Slackware 9.0)
My question is as follows:
All mail sent through the system is logged to /var/log/maillog
I did: grep to=< /var/log/maillog and this showed me basically what I wanted.
Is there a way to get the server to do the following:

Every night at 03:00 AM it runs a script or whatever it needs to.
This scripts retrieves all mail sent the previous day, from /var/log/maillog
It should only retreive the items from the day before. Otherwise it will be a lot of info in the end.

I have almost no experience in scripting or schedulig tasks on the server. If anyone can help point me in the right direction, I would really appreciate it.

Best regards
Geir Andersen
 
Old 09-29-2004, 12:00 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
To start with, you'll need to understand the script scheduler 'cron'.
man cron
You'll also need to learn bash scripting. For bash use:
http://freeos.com/guides/lsst/
http://www.icon.co.za/~psheer/book/index.html.gz which will also explain cron
http://dsl.org/cookbook/cookbook_toc.html another general guide
These should get you started. Come back if you have more specific qns.
 
Old 09-30-2004, 03:13 AM   #3
akilles
Member
 
Registered: Sep 2004
Location: Vestfold, Norway
Distribution: Slackware
Posts: 139

Original Poster
Rep: Reputation: 15
Thanks.
I found a tutorial yesterday, that explained cron, and that was ok.
After some searching, trying and failing, I got a script partially working.

#!/bin/bash

cat /var/log/messages | grep "to=<" > /tmp/send.file
sendmail -t -oi root@localhost < /tmp/send.file

This is very crude, but sends me info about all mail passing through the system.
Next I'll have to get bash to get yesterdays date, and filter out only those results as well.
Hopefully I'
ll get that working today.

Thanks for the links.

Geir O.
 
Old 09-30-2004, 03:52 AM   #4
kees-jan
Member
 
Registered: Sep 2004
Distribution: Debian, Ubuntu, BeatrIX, OpenWRT
Posts: 273

Rep: Reputation: 30
You probably also have logrotate installed. I think you should check it out. You can configure it to daily rename your /var/log/messages. That way you won't have to filter out yesterdays results any more.

Groetjes,

Kees-Jan
 
Old 09-30-2004, 04:00 AM   #5
akilles
Member
 
Registered: Sep 2004
Location: Vestfold, Norway
Distribution: Slackware
Posts: 139

Original Poster
Rep: Reputation: 15
Yes, I have logrotate installed.
Hadn't even thought about that . A new log for every day is so much simpler, allthough it will give a lot of files in the end. But, by the time the files are that many, I hope I've learned enough bash-scripting to write a script that deletes or compresses the files older than say, a couple of months.

Thanks.

-Geir O.
 
Old 09-30-2004, 04:21 AM   #6
kees-jan
Member
 
Registered: Sep 2004
Distribution: Debian, Ubuntu, BeatrIX, OpenWRT
Posts: 273

Rep: Reputation: 30
Don't despair. Logrotate can even do that for you.
On my system, /var/log/messages is rotated weekly, older files are compressed, and it keeps at most 4 files.

All you need to do is modify the configuration and, of course, plug in your script that fetches the info from the logfile, just before or after it is rotated.

Groetjes,

Kees-Jan
 
Old 09-30-2004, 04:52 AM   #7
akilles
Member
 
Registered: Sep 2004
Location: Vestfold, Norway
Distribution: Slackware
Posts: 139

Original Poster
Rep: Reputation: 15
I'll have to look more at the logrotate function then. Looks like it can do everything for me.


This little script also partially works:
#!/bin/bash

DAY=`date +%d`
MONTH=`date +%m`
YEAR=`date +%y`

YESTERDAY=$(($DAY - 1))
echo "Yesterday's date: " $YESTERDAY"."$MONTH"."$YEAR

This will however not work at the first of the month. Instead of returning 30 or 31, it would return 0. I'll sit down some time and see if I can write a script to handle first of the month, and return correctly 30 or 31, whatever the case may be.

Thanks again.

-Geir O.
 
Old 09-30-2004, 12:43 PM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
%j gives day of year, so start with that and substract 1 ?
 
Old 10-01-2004, 02:06 AM   #9
akilles
Member
 
Registered: Sep 2004
Location: Vestfold, Norway
Distribution: Slackware
Posts: 139

Original Poster
Rep: Reputation: 15
Well, that would give: 274 for yesterday, but I want the date: 30.09.(20)04. Not sure how it's written in /var/log/maillog
 
Old 10-01-2004, 11:21 AM   #10
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
How about getting logrotate to rotate mail logs every day, then yesterday's is always maillog.1 (or similar). Tell it to keep the last N logs eg 30 for 1 mth.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Need Help With Mailing! linuxnoob7 Linux - Software 14 08-15-2005 11:14 AM
mailing praveenv Linux - Newbie 2 09-10-2004 06:19 AM
e-mailing Linux~Powered Linux - Software 3 02-12-2004 09:07 PM
problem with mailing Red evil Linux - Networking 0 10-15-2003 10:17 AM
mailing client Chilipepa Linux - Newbie 1 09-28-2003 09:33 AM


All times are GMT -5. The time now is 02:10 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration