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!

Thursday 4 July 2013

How to create cron in symfony?


Hi All,

First of all create task using the below command:

 
php symfony generate:task [your task] 



for example:

php symfony generate:task notificationFailed

After executing above command you will see that following file has been created.

Creating "/var/www/archive/ipay4me_zen/lib/task /notificationFailedTask.class.php" task file


Now,

change the following variable in above created file:

$this->namespace        = 'project';
$this->name             = '[name-for-your-task]';
$this->briefDescription = '[some short explanation of what your task does]';


You will see another method called execute() given below:

protected function execute($arguments = array(), $options = array())
{
 // initialize the database connection
 $databaseManager = new sfDatabaseManager($this->configuration);
 $connection = $databaseManager->getDatabase($options['connection'] ? $options['connection'] : null)->getConnection();

 // add your code here

 echo "I just did nothing at all, but at least I did it successfully!\n\n";
}


Let’s give it a test run. Open your command line and enter

php symfony project:[your task]


if everything is fine then you will see "I just did nothing at all, but at least I did it successfully!" otherwise correct error and run again.

If you see the expected output you’re then ready to go to the next step.


Note: if you want to access your app.yml variable here then you have to write following line:

$this->createConfiguration('frontend', 'prod')

or

$this->createConfiguration('frontend', 'dev')

Cron Setup:

* * * * * cd [YOUR SF APP DIR] && /usr/bin/symfony project:[YOUR TASK] >>[YOUR SF APP DIR]/log/crontab.log
e.g,

* * * * * cd /var/www/archive/ipay4me_new && /usr/bin/php symfony project:notificationFailed >>/var/www/archive/ipay4me_new/log/crontab.log


Cheers!!!