Solved

Stuck with small batch script

Posted on 2013-11-17
5
320 Views
Last Modified: 2013-11-19
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
0
Comment
Question by:Thomanji
  • 2
  • 2
5 Comments
 
LVL 34

Accepted Solution

by:
Dan Craciun earned 300 total points
ID: 39655539
For the awk command you need:
mailq | tail -n 1 | awk '{ print $5 >> "/etc/mailq_log.txt" }'

Here's an article for redirection with awk: http://www.chemie.fu-berlin.de/chemnet/use/info/gawk/gawk_7.html#SEC65

HTH,
Dan
0
 
LVL 34

Assisted Solution

by:Dan Craciun
Dan Craciun earned 300 total points
ID: 39655549
You can run your hourly cron with the ">>" redirection (which appends) and have a daily cron with the ">" redirection, which empties the file prior to writing in it.

HTH,
Dan
0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 200 total points
ID: 39655553
mailq | tail -n 1 | awk '{ print($5); }' > /etc/mailq_log.txt
0
 

Author Comment

by:Thomanji
ID: 39655647
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
0
 

Author Closing Comment

by:Thomanji
ID: 39661429
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
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question