Friday, 12 July 2013

How to send mail from shell scirpt?


Hi All,

First of all we will create a sh file and write script see example below:

vim notificationFailedCron.sh

Write down the following code in the above file:

#!/bin/bash
dt=`date +%F`
echo $dt
passwd="pwd"
count=`mysql -uipay_read -p$passwd -h localhost -D database_name -B --skip-column-names -e "select count(*) from tablename where last_execution_status = 'notexecuted' AND date(created_at) = '$dt';"`
if [ $count -ge 5 ]; then

echo $count;
mail   -s "$count Payment notifications are getting failed."  abc@abc.com  <<< 'There are $count notifications are pending due to cron job failure'
fi


Now setting cronjob which will run every minute:

* * * * * /path/to/file/ipay4me_cronJob/notificationFailedCron.sh /usr/bin/php Asia/Calcutta >/dev/null 2>&1



Cheers!

No comments:

Post a Comment