How do I get the results of a cron emailed to me?
I have a cron tab with the following that I have two jobs - 1 running at 1am UTC and the other at 2am UTC:
MAILTO=me@example.com
0 2 * * * /home/andy/.local/share/virtualenvs/hr_py-zmYY2Pz4/bin/python /home/andy/hr_py/hr_py/manage.py dbbackup --compress
0 1 * * * /home/andy/.local/share/virtualenvs/hr_py-zmYY2Pz4/bin/python /home/andy/hr_py/hr_py/manage.py email_touch_base
But I'm not getting the emails. The second cron tab normally prints stuff out to the console (when run manually) - and I would like this print out emailed to me.
In cPanel you could just put a setting of who to email cron results to - but I don't know how to do it with linode.
I'm running ubuntu 20ish with ngiunx and gunicorn.
I'm wondering if linode stops these kinds of emails? I had to get the lindoe team to turn on email sending back where I first set up my linode.
Thanks in advance!
4 Replies
✓ Best Answer
Like this:
MAILTO=me@example.com
33 3 * * * /path/to/some_job.sh 2>&1 | mail -s "some_job.sh output" $MAILTO
-- sw
Is the "some_job.sh output" the subject of the email?
MAILTO=support@horserecords.info
0 2 * * * /home/andy/.local/share/virtualenvs/hr_py-zmYY2Pz4/bin/python /home/andy/hr_py/hr_py/manage.py dbbackup --compress
0 1 * * * /home/andy/.local/share/virtualenvs/hr_py-zmYY2Pz4/bin/python /home/andy/hr_py/hr_py/manage.py email_touch_base 2>&1 | mail -s "Email Touch Base output" $MAILTO
I have the above but the emails still arent coming on the second job (I don't need emails for the first job).
What am I doing wrong?
Yes. You can set all kinds of other info from the command line too…Cc:, Bcc:, From:, etc. You can send the mail to more than one person if you put other addresses after $MAILTO.
See man mail.
-- sw
In cPanel you could just put a setting of who to email cron results to - but I don't know how to do it with linode.
Do you have an email server configured on your Linode to which to submit email and send it to support@horserecords.info? Such things are not automatic when you set up a Linode… If not, the mails are ending up in a local mailbox…probably root's.
Log in to your Linode and run
sudo mail
and see what shows up. I'll bet you'll see your mail there… If root doesn't have a local mailbox, your emails went to some unknown place that's prob distro-dependent. Another place to look would be the system logs. Typically, the mail log would be /var/log/mail.log. I don't know where logs would go if you don't have an email server set up.
-- sw