I did an exercise to find out in which cases will crontab send email notifications.
The objective of this exercise was to get an email notification when the job runs AND fails(errors, etc).
Another objective was to see if we get an email if the cron job fails to start due to incorrect command/script.
This was tested on a FreeBSD box with bash.
The Observations:

By default we receive mails only when the job(command) is executed and it either, fails or succeeds. Now depending upon how we redirect sterr and stdout, any of these can be suppressed.

An example where failure messages are sent:

crontab -l

MAILTO=user@email.com
 @hourly ls /home/another_user_   1>/home/user/crontab_log.txt

Will send an email if there are errors when the ls command executes, for example due to permissions on /home/another_user_ dir. This works because we have not redirected stderr(2) and cron gets content to forward over email.

An example when the command is not found:

crontab -l

MAILTO=user@email.com
 @hourly lss /home/user 1>/home/user/crontab_log.txt

Will send an email as the stderr(2) (lss not found)is not redirected and is instead sent over email.

Lastly, if the job is not executed at all due to various reasons(crond dead), then we get no notification.

To redirect both stdout(1) and stderr(2):

<command_to_run> 1>/path/to/save/or/dump  2>&1
Advertisements