Link to home
Start Free TrialLog in
Avatar of Thomas
ThomasFlag for Malaysia

asked on

Stuck with small batch script

Hi,

I am trying to do a small script but are stuck because of my limited knowledge.
What I try to do is relative simple. I like to read the last line of the mailq command and than write it in a file. I plan to do this into the hourly cron and than after 24 hours have 24 numbers in the file and send them to me via email and then erase the content for another 24 hour.

I have the command

mailq | tail -n 1 | awk '{ print($5); }'

that shows me the number.

next thing I tryed was:
mailq | tail -n 1 | awk '{ print($5); }' /etc/mailq_log.txt

but this does not write the number into the file.

So the bottom line is that I simply want to write into a file the amount of messages in the postfix queue and then send this file every day to my email and then clean the file for the next day. My next phase would then be that if this number is over x amount at any hour it sends a "check mail queue" email to me as alert.

I would be very happy if any of you would be able to help me out to make this happen. It would be greatly appreciated.

Best wishes,
Thomas
ASKER CERTIFIED SOLUTION
Avatar of Dan Craciun
Dan Craciun
Flag of Romania image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Thomas

ASKER

Hi,

Thank you for the answers, I do appreciate it. However, I try to get the batch working the way I described.

Here is what I have by now:

(this would be in the hourly cron)

#!/bin/sh

MAX_SIZE=${1:-10}
CUR_SIZE=`mailq | tail -n 1`

if [ $CUR_SIZE -gt $MAX_SIZE ]; then
     mailq | tail -n 1 | awk '{ print $5 >> "/var/log/mailq.alert" }'
     mail -s Postfix Queue Alert email@email.com < /var/log/mailq.alert
fi
else
     echo $CUR_SIZE > /var/log/mailq.size
fi

exit 0

then I plan to put in the daily cron another script simply containing:

mail -s Postfix Queue Daily email@email.com < /var/log/mailq.size

so that once a day I get the mailq.size file that theoretically contains 24 numbers.
I also try to get a date and time stamp in front of each mailq count.
Anyway, all this does not work as expected.
Any assistance is welcome

Best wishes,
Thom
Avatar of Thomas

ASKER

Thanks for the input, I did now write a script that monitors the mail and sends a daily report or a alert if the queue goes over xx messages. Works great