2

I have this script which I run once a month, and I want to get the output sent to my email.

If I run it manually like so, then the standard output is sent to my email.

./monthly_mail.sh | mail -s "Mail purged" email@email.com

But when I added the following line to my crontab, I just get a blank email. Why isn't the stdoutput of the script piped to mail?

* * 2 * * /var/mail/monthly_mail.sh | /usr/bin/mail -s "Mail purged" email@example.com
  • When you run this from the command line (outside of cron) it works, right? – slm Nov 5 '13 at 9:34
2

Couple of things to try.

  1. Correct crontab incantation

    If you're putting this in the file, /etc/crontab you're required to specify what user to run the cron job as. So you're line should be like this:

    * * 2 * * root /var/mail/monthly_mail.sh | /usr/bin/mail -s "Mail purged" email@example.com
    

    Where root is the user that the command will run under. Change that as needed.

  2. Test mail outside of cron

    Make sure you can run the command as the user you intend to have running the cron from the file /etc/crontab, standalone from a shell first, before moving it into the crontab.

    $ /var/mail/monthly_mail.sh | /usr/bin/mail -s "Mail purged" email@example.com

  3. Check the output of MAILTO

    Cron typically has a default user that get's any output that is generated on the STDOUT or STDERR already. See if there are any error messages getting emailed to this user that might clue you into why your command isn't running. This email address is typically configured in cron like so:

    MAILTO=username@domain.com
    
-1

Another site suggested adding 2>&1 before the | as in:

* * 2 * * root /var/mail/monthly_mail.sh 2>&1 | /usr/bin/mail -s "Mail purged" email@example.com

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.