For this purposes you system should be able to send emails. So you could install and configure postfix
:
sudo apt install postfix
For General type of mail configuration choose Internet Site, if you want to do more detailed configuration use the command:
sudo dpkg-reconfigure postfix
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 client mutt
to read your user's email box via the command line in a convenient way:
sudo apt install mutt
- Note
mutt
installation process will involve installation and configuration of postfix
if it isn't done before.
You can change the default destination mailbox by changing the value of the envvar MAILTO
within crontab
, 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 the rm
command) and must set proper value of MAILTO
. So your crontab
should look as this:
MAILTO="example.email@gmail.com"
* * * * * find /nfs/rpiggott/complete -mtime +45 -exec rm -v {} \;
Another approach is to create a script (which will be executed via crontab
) that includes your command and uses mail
, mutt
, ssmtp
or sendmail
to send emails. See the bottom of the references for mor details.
References and further reading:
find
also recognize the+
sign instead of\;
, i.e.find … -exec rm -v {} +
(instead offind … -exec rm -v {} \;
. Depending on the number of files this will greatly improve performance: with\;
therm
command is run once per file (i.e. 1000 times for 1000 files) whereas the+
variant will runrm
with as many filenames as parameter as fit into the command line (i.e. it will callrm
only twice or so, once with 800 files and a second time with 200 files). – PerlDuck Apr 01 '18 at 16:19rm [800 filenames]: failed
whereas the\;
would help to better identify the error because just a single filename is given. – PerlDuck Apr 01 '18 at 16:22\;
or+
is engaged while-exec rm -v {}
is used, the output is identical so probably+
is a better choice. @PerlDuck – pa4080 Apr 01 '18 at 16:27