linux - Only email on cron errors for jobs in cron.daily, cron.hourly, etc

15
2014-01
  • jrdioko

    I have several cron jobs that run (in /etc/cron.daily, /etc/cron.hourly, /etc/cron.weekly, etc.) and email root@localhost with the results. I'd like to stop those emails if the jobs are succeeding, and only email on error (which I understand can be done by redirecting stdout to /dev/null). I understand how to do that for individual cron jobs, but the scripts in those special directories are run using run-parts. What is the best way to suppress success emails for those scripts?

  • Answers
  • pavium

    You should send successful email notifications to /dev/null so they disappear.

    But you want to see unsuccessful email notifications.

    This means you need to first direct stdout to /dev/null and then direct /dev/stderr to stdout

    try changing the redirection part of your cronjobs to

    >/dev/null 2>&1
    

    See this link


  • Related Question

    Linux cron job error
  • Questioner

    I have setup a cron job to run a PHP file every 30 minutes:

    lynx -source public_html/scripts/file.php

    The result comes through to an email but seems to get this error:

    Can't Access `file://localhost/home/username/public_html/scripts/file.php' Alert!: Unable to access document.

    lynx: Can't access startfile


  • Related Answers
  • leonbloy

    Several things are wrong here.

    First, before putting a command in crontab, check that it works by running it yourself.

    Second, if you want the html code generated by a (dynamic) php webpage, you must access trough the web server, not reading the php from from the filesystem. In the case of a php webpage, an alternative (not necesarily equivalent) would be to just invoke the php interpreter from the CLI:

    php public_html/scripts/file.php
    

    Third, no need to you use lynx (a text browser) for that, go wget:

    wget http://localhost/scripts/file.php  
    

    Perhaps you must change the url, that depends on your webserver configuration. Load it first in your web browser to check.