I would like to trigger a php script via cron. I'd very much like to know if a problem occurs in the script but would like to avoid a situation where I'm getting email every five minutes while on vacation or something. Also, I'm wondering how to manage any notifications for several platforms: my workstation, a dev/test server, and the production machine. I'm hoping folks can provide some input here. Some questions:
1) What should my script output, if anything?
According to
Unix and Linux System Administration Handbook, 4th Edition:
Quote:
Usually, any output produced by a cron command is mailed to the owner of the crontab*
*That is, the user after whom the crontab file is named. On most (but not all) systems, the actual owner of crontab files is root
|
I usually try outputting some text to indicate progress of any cron script and route stdout and stderr to an output file:
Code:
*/10 * * * * /usr/bin/php /path/to/my/script.php > /path/to/some/output/file.txt 2>&1
According to the handbook quote above, that should always result in an email being sent, which doesn't sound desirable. On the other hand, I almost never receive emails from this sort of thing, which suggests that a) the handbook is wrong or b) email is piling up somewhere in a local email account (which sounds undesirable). Such an output file seems helpful as it would contain the output and error from the last attempted run of a script.
How do folks most often deal with output?
2) What notifications should be sent?
It would be nice to know if my script is failing all the time, but I don't want to receive an email every five minutes -- or even sporadically all day if my server experiences some unexciting/typical problem. E.g., "couldn't connect to twitter to send tweets at 11:59". Does anyone have suggestions or anecodotal experience in dealing with cron job notifications? Is there some best-practice regarding such things? Or perhaps some kind of toggle method that sends an email when the service goes down and another when it does up? Keep in mind I have workstation/dev/live instances of my app that might all run simultaneously.
Question 2b: under what circumstances does cron send emails?
I've got plenty of scripts outputting text and throwing uncaught exceptions and no email seems to ever get sent. On one machine, however, I get a huge pile of emails for cron jobs. This appears to be because Postfix is considered to route all email off the local machine (no local delivery!) to actual email accounts. Must I be concerned about email piling up without bound in some local account, chewing up precious space on the file system?
3) How to deal with errors?
Suppose my PHP script throws a fatal or non-fatal error. Using the technique above, I can route this to output, but I risk sending myself a ton of email for trivial situations. I realize I can try/catch in my PHP scripts and try to catch exceptions, but how does one capture error conditions? I've checked the /var/log/syslog on my unbuntu workstation for this trivial script that just throws an exception:
Code:
<?php
throw new Exception("Just for kicks!");
as above, stderr and stdout are routed to a file, but I only get the last attempt. In /var/log/syslog, there is no detail about the error. All I get is:
Code:
Mar 8 12:43:01 sneakyimp-ubuntu-14 CRON[18881]: (sneakyimp) CMD (php /tmp/foo/foo.php > /tmp/foo/foo.txt 2>&1)
Any guidance would be much appreciated.