LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-08-2017, 02:47 PM   #1
sneakyimp
Senior Member
 
Registered: Dec 2004
Posts: 1,056

Rep: Reputation: 78
cron jobs: handling failure and return values


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.

Last edited by sneakyimp; 03-08-2017 at 03:06 PM.
 
Old 03-08-2017, 07:22 PM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 21,505

Rep: Reputation: 4113Reputation: 4113Reputation: 4113Reputation: 4113Reputation: 4113Reputation: 4113Reputation: 4113Reputation: 4113Reputation: 4113Reputation: 4113Reputation: 4113
On most default systems which are configured for local mail as stated if the command produces output it will be mailed to the user. If you redirect the output to a file there isn't any output so no mail i.e.

Code:
* * * * * echo "Hello World"
cron will email the job was run with Hello World to the user but
Code:
* * * * * echo "Hello World > /tmp/test.file"
Does not.

To see your mail use the mail command in a terminal window. See the man pages for commands. The log (/var/log/syslog) shows the command was run but not the errors. What kind of errors do you expect? For expected errors can you test with conditionals and try/catch? With the output being redirected you can scan the file for errors and not worry about lots of mail if your running the script every 10 minutes.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Cron job starts 6 hours late, rest of cron jobs work fine pieterhouwen Linux - Newbie 33 10-05-2015 12:38 PM
Where are cron jobs located when not under /var/spool/cron/root? NotionCommotion Linux - Newbie 6 09-07-2012 07:33 AM
[SOLVED] chkconfig return values youngstorm Linux - General 6 04-10-2012 07:47 AM
cron hourly, daily, cron.d jobs don't execute eggsmartha Linux - General 3 09-17-2007 06:37 PM
system() and return values PatrickNew Programming 1 02-07-2007 04:35 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:49 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration