I have the following cron job command running once daily on my NAS device:
find /nfs/rpiggott/complete -mtime +45 -exec rm {} \;
I am wondering if there is a way to pipe a list that will result in the cron sending an e-mail to me showing what file it delete and / or any errors experienced.
For this purposes you system should be able to send emails. So you could install and configure
postfix
:For General type of mail configuration choose Internet Site, if you want to do more detailed configuration use the command:
At this stage Cron will start to send emails. Everything that usually will be outputted to the STDOUT (if you are execute a command in the command line), including all error messages, will be sent to the local mailbox of the user that runs the Cronjob.
The default location of local user's mail boxes is
/var/mail/
. You can install the command-line email clientmutt
to read your user's email box via the command line in a convenient way:mutt
installation process will involve installation and configuration ofpostfix
if it isn't done before.You can change the default destination mailbox by changing the value of the envvar
MAILTO
withincrontab
, before the definition of the Cronjob.Please note: unless you haven't enabled SSL/TLS certificate within you send mail configuration, most of the public mail servers will ignore your emails in some way. For example
mail.google.com
will put them into the spam. If this is a server instance and you already have SSL/TLS certificate for your primary domain follow this nice manual to attach it to Postfix.Once your system is able to send emails you must make your Cronjob more verbose (for e.g. add
-v
to therm
command) and must set proper value ofMAILTO
. So yourcrontab
should look as this:Another approach is to create a script (which will be executed via
crontab
) that includes your command and usesmail
,mutt
,ssmtp
orsendmail
to send emails. See the bottom of the references for mor details.References and further reading:
Issues getting crontab to work
How To Install and Configure Postfix on Ubuntu 16.04
Postfix TLS Support
How to send mail from the command line?
5 Ways to Send Email From Linux Command Line
I used a pretty simple method to get
cron
to send emails: Backup Linux configuration, scripts and documents to Gmail.Simplest way to automate sending email
From Send email alerts using ssmtp we find the simplest way of sending email automated from terminal or script. The installation steps are straight forward:
There is one step not mentioned; Google will send you an email confirming you want to allow a "less secure" application to send mail with your account:
There is an annoyance of getting too much mail. For example every time you mistype your
sudo
password you will get an email: Stop emailing me bad sudo password attempts