SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Wizard
    Join Date
    Jun 2005
    Posts
    1,441
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Email notification of cron job ?

    Can anyone please let me know how to get an email notification to say whether the cron job has run ok and emails to say when it hasn't run ok ?

    Any help appreciated.

    Dez.

  2. #2
    SitePoint Member
    Join Date
    Jun 2008
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello, Dez.
    Quote Originally Posted by Dez View Post
    Can anyone please let me know how to get an email notification to say whether the cron job has run ok and emails to say when it hasn't run ok ?
    Take an example #1:
    Code:
    MAILTO="postmaster@example.com"
    30 3 * * * find /home/*/Maildir/.Spam/{new,cur}/ -type f -mtime +6 -delete
    Crontab owner recieves non-dummy results by mail.
    You can change owner's e-mail address by environment string MAILTO.

    And the second:
    Code:
    30 3 * * * find /home/*/Maildir/.Spam/{new,cur}/ -type f -mtime +6 -delete| \
               mail -e -s "task #1 report" postmaster@example.com
    Where '-e' mean don't send zero sized messages.

    Both variants sends you stdout of the tasks.
    You can suppress stdout reports with redirection to /dev/null.
    Stderr reports you will recieve if something wrong were on task execution.
    It may be something like 'ls: asdasd: No such file or directory' or something else.

    Add '2>&1' before the ' >/dev/null' for stderr suppressing.
    Example:
    Code:
    30 3 * * * find /home/*/Maildir/.Spam/{new,cur}/ -type f -mtime +6 -delete  \
               2>&1 >/dev/null && echo The task was ended at `date` | \
               mail -e -s "task #1 report" postmaster@example.com
    It sends something like
    Code:
    From: yourlogin
    To: postmaster@example.com
    Subject: task #1 report
    
    The task was ended at Mon Jun 16 03:30:08 MSD 2008
    Good luck!

  3. #3
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,692
    Mentioned
    20 Post(s)
    Tagged
    3 Thread(s)
    жопа,

    First, WELCOME to SitePoint!

    Second, thanks for that excellent post!

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  4. #4
    SitePoint Wizard
    Join Date
    Jun 2005
    Posts
    1,441
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Xопа, Many thanks for taking the time to post such detail - it's appreciated.

    The terminology has me a little baffled, but that's because I've only been using crons for a very short time.

    One thing, is it possible to do all of that on a shared server ?

    Dez.

  5. #5
    SitePoint Member
    Join Date
    Jun 2008
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Dez,

    Quote Originally Posted by Dez View Post
    One thing, is it possible to do all of that on a shared server?
    Yes. All users can use ctontabs. Except of crontab-denyed users.
    Check your hosting rate (tarif?).

  6. #6
    SitePoint Member
    Join Date
    Jun 2008
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dklynn View Post
    thanks for that excellent post!
    :)
    Do you know, how will work apache on opensolaris?
    I want to make an installation on my home server.

  7. #7
    SitePoint Wizard
    Join Date
    Jun 2005
    Posts
    1,441
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up

    Quote Originally Posted by жопа View Post
    Hi Dez,


    Yes. All users can use ctontabs. Except of crontab-denyed users.
    Check your hosting rate (tarif?).
    Many thanks - will work on this some more.

  8. #8
    SitePoint Guru LinhGB's Avatar
    Join Date
    Apr 2004
    Location
    Melbourne, Australia
    Posts
    902
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    жопа, this is a trickier question than that.

    Linux always emails any output of the cronjob to the owner of the job. System crons email it to the root user (unless specified otherwise). User crontabs email the user. If you only care about the outputs, all you have to do is to set a working email address for that user. How you do that is up to the mail system you run.

    However, to get notifications only when the cronjob is successful, you need to do more scripting than this, because you need to determine exactly what's considered successful and what's not. This varies from case to case.

    Sysadmins typically integrate such scripts with their monitoring platform. I personally use Nagios and for example integrate my cron backup scripts with it so that I can monitor (and get notified) when the backups fail or succeed.

    What gets tricky is what I meant by the case by case basis earlier. Even within the same use, like backup, there are servers I know will get errors when running a rsync backup on them because their files get modified very regularly and it doesn't matter so much if I have a few errors, so overall the backup is still a success. Whereas if I'm backing up database dumps and get a file error on any dump file, that's a failure.

    If you can't be bothered scripting such things, just get the cron daemon to email you the whole output then you can manually shift through it and see for yourself.
    "I disapprove of what I say,
    but I will defend to the death my right to say it."


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •