I need to run my php code each hour, and my knowledge about crontab/cronjob is pretty bad. I want the script to run each hour and send email for 2 case: success and failure. I wrote this code but I don't know if it's good for crontab (I think it's not working):
0 * * * * php <full-path-script>
if [ “$?” = “0” ]; then
echo “Backup Process was Successful. A new log file <filename>.txt has been
created” | mail -s “Backup Status Successful” <email> -A <path>
<filename>.txt
else
echo “Backup Process Failed. Please contact System Administrator. A new log
file <filename>.txt has been created” | mail -s “Backup Status Failed”
<email> -A <path><filename>.txt
if you think I should do it different please explain and show me how. BTW - I'm working with postfix.
OK, here is what I've done to solve my problem:
First - Be sure to write all your shell scripts on Linux (otherwise you script will get messed up and you won't even be able to see it), you can setup your own Linux using vmware or other program.
This is my shell script:
`#!/bin/bash
/usr/bin/php <full-path-script>
if [ “$?” = “0” ]
then
echo “Backup Process was Successful. A new log file `date "+\%Y-\%m-\%d-
\%H"`.txt has been created” | mail -s “Backup Status Successful” <email-
address> -a <path>`date "+\%Y-\%m-\%d-\%H"`.txt
else
echo “Backup Process Failed. Please contact System Administrator” | mail -s
“Backup Status Failed” <email-address> -a <path>`date "+\%Y-\%m-\%d-\%H"`.txt
fi`
In order to make sure your script will send error into $? you should avoid using die(); on errors, In fact you should use exit(1); (it'll put 1 in $? so we'll know the script failed to run).
Don't forget to make cronjob that will task this script for you and send you notifications to your email address :)
I hope this help anyone!