5 Replies - 1431 Views - Last Post: 22 May 2012 - 12:06 AM

#1 Duckington  Icon User is offline

  • D.I.C Addict

Reputation: 170
  • View blog
  • Posts: 608
  • Joined: 12-October 09

emailing cron output - centos

Posted 14 May 2012 - 03:14 AM

Hi,

I'd like to get the output of our crons to be emailed to me, as we've been having some issues recently which would have been much easier to resolve if they'd been emailed.

I've googled it and it suggested adding: | mail -s "subject" address@address.com

So what I've got now is:

*/5 * * * * /usr/bin/php/ /root/to/file/test.php | mail -s "test output" me@domain.com



However it's not sending anything.

I've been into the cron log and it's outputting this:

May 14 11:10:01 moodledev crond[16793]: (root) CMD (/usr/bin/php /root/to/file/test.php | mail -s "test output" me@domain.com^M)



The only thing that looks wrong to me is the ^M at the end, which i'm not sure where it has appeared from.

Any clues on where i'm going wrong?

Cheers.

Is This A Good Question/Topic? 0
  • +

Replies To: emailing cron output - centos

#2 no2pencil  Icon User is online

  • Admiral Fancy Pants
  • member icon

Reputation: 5382
  • View blog
  • Posts: 27,350
  • Joined: 10-May 07

Re: emailing cron output - centos

Posted 15 May 2012 - 06:18 PM

Rather than forcing all code into a single line of the crontab, why not just put it into a script, & then execute the script in the crontab?

Or since you are using php, just use mail().
Was This Post Helpful? 0
  • +
  • -

#3 Duckington  Icon User is offline

  • D.I.C Addict

Reputation: 170
  • View blog
  • Posts: 608
  • Joined: 12-October 09

Re: emailing cron output - centos

Posted 16 May 2012 - 01:16 AM

Well the point is that if there is an error somewhere before it reaches the end, I still want the output (i.e. error messages and other output) to be emailed to me. if I just use mail() alone (which I already have ni various places) I only get an email if it gets that far and doesn't encounter an error.

The scripts work when I run them manually, but not when I run them via cron, so I would like the output of it all, error messages included, so I can see what is actually going wrong.
Was This Post Helpful? 0
  • +
  • -

#4 no2pencil  Icon User is online

  • Admiral Fancy Pants
  • member icon

Reputation: 5382
  • View blog
  • Posts: 27,350
  • Joined: 10-May 07

Re: emailing cron output - centos

Posted 16 May 2012 - 01:21 AM

Run echo statements throughout the php & log them to a file. Then you can see where the execution stops. output variables as well.

I believe the pipe, in the way you are using it, will only pass upon a successful return. If the php is failing, the mail will not launch. That's why I suggest a script over multi-line items in your crontab.
Was This Post Helpful? 0
  • +
  • -

#5 Duckington  Icon User is offline

  • D.I.C Addict

Reputation: 170
  • View blog
  • Posts: 608
  • Joined: 12-October 09

Re: emailing cron output - centos

Posted 16 May 2012 - 02:38 AM

That seems like an unnecessary amount of work. This should be fairly simple, it's not like it's a crazy new thing I'm trying to do... I've got it to work a few years ago on a different site & host, and I'd like to get it working here at work as well.

So could anyone suggest where I may be going wrong with what I have in the original post?
Was This Post Helpful? 0
  • +
  • -

#6 wordswords  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 76
  • View blog
  • Posts: 272
  • Joined: 17-December 11

Re: emailing cron output - centos

Posted 22 May 2012 - 12:06 AM

View PostDuckington, on 16 May 2012 - 02:38 AM, said:

That seems like an unnecessary amount of work. This should be fairly simple, it's not like it's a crazy new thing I'm trying to do... I've got it to work a few years ago on a different site & host, and I'd like to get it working here at work as well.

So could anyone suggest where I may be going wrong with what I have in the original post?


Try this:

Quote

*/5 * * * * "/usr/bin/php/ /root/to/file/test.php | /usr/bin/mail -s 'test output' me@domain.com"

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1