Grep and Email

Hello All,

I want to make a script which reads a files after every one hour and grep a pariticular word on it and if it finds it than email me on my email address. Please tell me how i start and if there is some example
available then please tell me the link.

One thing more that for the emailing purpose, do i have to configure mail server or mail client on my machine..

Please explain in detail..

Regards,
Waqas Ahmed

#/bin/ksh
# this is myscript.sh
/usr/bin/grep -q 'particular word'  /path/to/somefile
if [ $? -eq 0 ]
    echo "particular word found in /path/to/somefile " | /usr/bin/mailx -s 'found it' \
        me@mymailaccount.com
fi

Make an entry in crontab to run 10 minutes after the hour every hour

10 * * * * /path/to/myscript.sh

Yes, you do need mail up and running.
[/code]

It is possible, though unlikely, that ksh is not installed.

There is nothing in the script that requires ksh., so it is better to use sh:

#!/bin/sh

On many systems grep is in /bin, not /usr/bin and your script will fail. There is no need to give the full path for standard commands.

On some systems, the command may be mail rather than mailx. (And the comment about using the full path applies here, too.)

I want to use the mail command to send email from the command line like so:

mail -s "subject of email" emailaddress@whatever.com<textfile.txt

Can you please tell me do i have to turn on Sendmail for this.

My machine is connected to internet but below mail command is not working.

mail -s "subject of email" emailaddress@whatever.com<textfile.txt

i have to do just send email to my account. I dont want to receive anything, only sending email is my requirement.

Regards,
Waqas Ahmed

[quote=wakhan;302237487]
My machine is connected to internet but below mail command is not working.

mail -s "subject of email" emailaddress@whatever.com<textfile.txt

What error do you get? Did you check logs?

check /var/spool/mqueue

See if the message is sitting in there. There should be two files for each failed send attempt.

Final-Recipient: RFC822; khiomc@telecard.com.pk

Action: failed

Diagnostic code :SMTP; Domain of the sender address [email]root@localhost.localdomain does not exsist

The workaround is to use a correct email address to send from. Your local mailx may have some option to control that, or (better) you should configure the system defaults so they actually make sense. If the box is not set up to receive mail then you probably want to configure it to use an address somewhere else (ideally, one which you own and which you read regularly).