I have a server in which there are about a 100 Cron jobs that run various PHP scripts. The task at hand is to generate an alert any time an error occurs in the execution of a PHP script.  The Crons are set as follows:  30 08 * * * /usr/bin/php /var/www/html/phpscirpt1.php > /var/www/html/phpscript1.log 2>&1What I have tried is placing an && in the end but this generates the Alert Email either way  30 08 * * * /usr/bin/php /var/www/html/phpscript1.php > /var/www/html/phpscript1.log 2>&1 && <Generate Mail>The ideal case that should work would be  /bin/bash /home/myUser/testfile.sh > /home/myUser/testfile.log 2>&1 ; [$? == 1] && /bin/bash script.shThe following is just a sample for testing purposes, when/bin/bash /home/myUser/testfile.sh > /home/myUser/testfile.log 2>&1is executed testfile.sh only creates a directory. When the above command is executed the first time echo $? gives the output 0, but running the command again return 1, because the scripts returns and logs an error  mkdir: cannot create directory `/home/myUser/testdir': File existsAnd Basically this is what is required that is whenever the script in a cron fails it should generate an Alert in the form of an Email. In the above example script.sh contains a mail -s command to send the Email.But when the complete command is executed an error is returned which is as follows/bin/bash /home/myUser/testfile.sh > /home/myUser/testfile.log 2>&1 ; [$? == 1] && /bin/bash script.sh-bash: [1: command not foundI would greatly appreciate any guidance that could be provided in resolving this error. Thank you
Using an If Statement in Crontab To Generate an Alert
linux;shell script;cron
Basically your solution is good. You have just made simple bash syntax error.You have to put spaces around '[' and ']' characters:[ $? == 1 ]I've tested it on my box and it works. I would also suggest to test error code as not equal 0 ([ $? -ne 0 ]), unless you are sure that you want to react only on error code 1.