Summary
Last week, I found myself setting up some jobs in a Unix environment (Centos 7) for which I wanted to get error email notifications and I ran into some troubles setting up the proper configuration, so I would like to take the chance and share the actions I took in case anyone finds him/herself in the same situation.
Please note that we are going to use Postfix as mail service here!
Initial Crontab setup
This was the setup I was facing, some jobs scheduled that might fail sometimes and one that will always fail as it was badly written:
$crontab -l
30 15 * * * java -jar /path_to_your_jar_app/job.jar
45 13 * * * /path_to_some_script/script.sh
45 19 * * * obviously_wrong_command
I wanted to be notified by email of any failure (and as I was using Cron, the expected behavior was to get
an email notification when either process returns a non-zero exit code).
This is an example of an email that should be generated for the above setup:
Message 1:
From cron_user@homepc Sun Oct 23 19:45:01 2016
Delivered-To: john.doe.the.sysadmin@mailservice.com
From: "(Cron Daemon)"
Subject: Cron obviously_wrong_command
Content-Type: text/plain; charset=UTF-8
Auto-Submitted: auto-generated
Precedence: bulk
X-Cron-Env: XDG_SESSION_ID=3289
X-Cron-Env: XDG_RUNTIME_DIR=/run/user/3001
X-Cron-Env: LANG=en_US.UTF-8
X-Cron-Env: MAIL_TO=admin_user_alias
X-Cron-Env: SHELL=/bin/sh
X-Cron-Env: HOME=/home/cron_user
X-Cron-Env: PATH=/usr/bin:/bin
X-Cron-Env: LOGNAME=cron_user
X-Cron-Env: USER=cron_user
Date: Sun, 23 Oct 2016 19:45:01 +0200 (CEST)
Status: RO
/bin/sh: obviously_wrong_command: command not found
Keep reading if you want to get the same setup!