Find the answer to your Linux question:
Results 1 to 5 of 5
Hi ,I am running a script from crontab and would like a crontab to mail me only if there is an error. Here is what I have in crontab: Code: ...
  1. #1
    Just Joined! feisar's Avatar
    Join Date
    Jun 2010
    Posts
    1

    Question crontab stderr only

    Hi ,I am running a script from crontab and would like a crontab to mail me only if there is an error.

    Here is what I have in crontab:
    Code:
    MAILTO="myemailaddress"
    0 1 * * * /home/admin/scripts/backup.sh >/dev/null
    The trouble is that I still seem to get mailed part of the successful output even with > /dev/null in place.

    Without > /dev/null:
    Code:
    Stopping slapd: [  OK  ]
    Checking configuration files for slapd:  config file testing succeeded
    [  OK  ]
    Starting slapd: [  OK  ]
    With > /dev/null:

    Code:
    config file testing succeeded
    Be great if someone could shed some light on this for me.

    Thanks

  2. #2
    Just Joined!
    Join Date
    Mar 2006
    Location
    Manila, Philippines
    Posts
    3

    RE: crontab stderr only

    Hi feisar, you can try using 2> whi'ch redirects error output instead of using >. which redirect standard output. But it seems that by changing > to 2> the logic of the script will also change.

  3. #3
    Just Joined!
    Join Date
    Jul 2008
    Posts
    81
    Your script "backup.sh" seems to be producing output to stderr whether or not it is successful. Like the message "config file testing succeeded".

    Cron can't tell the difference between a success and a failure message since they are both being sent to stderr. The message output is mailed to you. If you don't want this, change the output of "backup.sh" to say nothing if it succeeds. This is the Unix way of doing things.

  4. #4
    Just Joined!
    Join Date
    Mar 2010
    Posts
    1

    RE: crontab stderr only

    clowenstein is correct. Your script is putting out to stderr a non error message "config file testing succeeded". You need to fix this before the logic you want to use will work.

  5. #5
    Just Joined!
    Join Date
    Jun 2007
    Posts
    16
    maybe something like::::

    #!/bin/bash
    service start <something> > /tmp/somefile 2>&1
    if [ $? != 0 ]
    then
    mail -s "<some kind of subject" somebody@<somedomain> < /tmp/somefile
    fi
    exit

Posting Permissions

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