Google Answers Logo
View Question
Q: Cron job to check for specific incoming email ( No Answer,   1 Comment )
Subject: Cron job to check for specific incoming email
Category: Computers > Algorithms
Asked by: cluelessone-ga
List Price: $5.00
Posted: 05 Mar 2006 15:30 PST
Expires: 04 Apr 2006 16:30 PDT
Question ID: 703977
I need a unix script that will grep a mailbox file every hour, if a
string is found, then send an email notifying me to an external email
address. I can set up a cron job on the server but I need the
There is no answer at this time.

Subject: Re: Cron job to check for specific incoming email
From: internettester-ga on 06 Mar 2006 06:39 PST
I would say the easiest way to do something like this is to simply
forward the incoming messages to your external e-mail address. To do
this, simply create a file named .forward in your home directory that
contains your external e-mail address (for some UNIX/mail
configurations the .forward file has to be placed in your mail
directory). But this way you'll get ALL incoming mail forwarded.

Now getting back to your request... let's split it in two parts:

1. The "script" to send the mail if a string is found in a file is
(assuming you're familiar with UNIX' shell):
[ -n "`cat FILE | grep STRING`" ] && echo "MESSAGE" | mail -s "SUBJECT" ADDRESS
where: FILE is the file to be parsed for the occurence of the string
STRING and MESSAGE is the message to be send to the e-mail address
ADDRESS, having the subject SUBJECT.

2. The cron job syntax:
<min> <hour> <day/month> <month> <day/week> <command>
where <min> = minute (0-59), <hour> = hour (0-23), <day/month> = day
of the month (1-31), <month> = month of the year (1-12), <day/week> =
day of the week (0-6 with 0=Sunday) and <command> is the command to be

E.g. 0 * * * * <command>  -->  will execute <command> every hour.
If you wish to execute it only during the weekdays write it as:  0 * *
* 1-5 <command>

So, putting it all together, here is what you have to add to your
crontab (crontab -e):
0 * * * * [ -n "`cat FILE | grep STRING`" ] && echo "MESSAGE" | mail

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  

Google Home - Answers FAQ - Terms of Service - Privacy Policy