1. Create script

2. Create cronlog directory at: /var/log/cronlog

About script:

This script will runs cron and maintain proper out put and error logs. Every cron will make 2 file: error & out put. This script will append results in error log file and output log file, as mentioned in script.

Below is script, named: cront. Sh

#!/bin/bash

if [ $1 ] ;then

filename=`echo $1 | awk ‘BEGIN { FS = “/” } ; { print $NF }’`

logpath=/var/log/cronlog

echo -n “running $1″

echo -e “————————————————————————” >> $logpath/$filename-output

echo -e “————————————————————————” >> $logpath/$filename-error

echo “Starting Script $1 at : `date`” >> $logpath/$filename-output

echo “Starting Script $1 at : `date`” >> $logpath/$filename-error

echo -e “————————————————————————” >> $logpath/$filename-output

echo -e “————————————————————————” >> $logpath/$filename-error

/usr/bin/php5 $1 2>> $logpath/$filename-error 1>> $logpath/$filename-output

if [ $? -gt 0 ] ; then

echo “script execution failed” >> $logpath/$filename-output

echo “script execution failed” >> $logpath/$filename-error

echo -e “=========================================” >> $logpath/$filename-output

echo -e “=========================================” >> $logpath/$filename-error

exit 0;

fi

echo -e ” ”

echo -e “————————————————————————” >> $logpath/$filename-output

echo -e ” ”

echo -e “————————————————————————” >> $logpath/$filename-error

#cat $logpath/$filename-output | mail -s “Cron is Running” farhan.umer@nxb.com.pk

#cat $logpath/$filename-error | mail -s “Cron is Running” farhan.umer@nxb.com.pk

echo “Script stopped at : `date`” >> $logpath/$filename-output

echo “Script stopped at : `date`” >> $logpath/$filename-error

echo -e “=======================================” >> $logpath/$filename-output

echo -e “=======================================” >> $logpath/$filename-error

echo -e >> $logpath/$filename-output

echo -e >> $logpath/$filename-output

else

echo “Please provide job to handle”

fi

3. Now setup cronjob in crontab, as mentioned below:

* * * * * /root/script/cron.sh /var/www/development/crons/articleDistribution/articleAutomation4.php

  • where

/root/script/cron.sh      ← path of script else is command to execute

Advertisements