Quantcast
Viewing latest article 9
Browse Latest Browse All 10

cron issues: not being able to get a real ‘from’ address

And yet another issue with cron.

I want to get an e-mail when something goes wrong on our servers. Especially if it’s a scheduled script that I normally don’t check that often any-more. (as you know from my previous post, some of scripts run every 10s, so even if I would check it by hand every hour, a serious backlog could already be generated). This worked very well by just creating a ‘forward’ file in the home directory of the user. ( ~/forward) which contains one e-mail address. The problem is that only the ‘from’ address will be something like hostname@domain [without any tld or something]. And our exchange server didn’t want to except e-mails coming from such a source.

After some googling and trying things I decided to give up and write my own very small script that takes care of this all.

#!/bin/bash
stuff=""
#put all output in one string
while read data; do
    stuff="$stuff n $data"
done
#if string is not empty, send e-mail with contents
if [[ -n "$stuff" ]]
then
  datetime=$(date "+%Y-%m-%d %H:%M:%S.%N")
  echo -e "Date: $datetime \n Trace: $stuff" | mail -s 'MESSAGE FROM CRON'  -- -f
fi

This is how the mailer is called inside the crontab system:

*/10 * * * * /path/to/script 2>&1 &>/dev/null | /path/to/cronmailer

ps: if someone knows a better solution, like, how to set a ‘from’ address in cron, please tell me.. but for now this will work just fine..

[edit]
For some reason, another options just started to work. Not sure what I did wrong the first time I tried it Image may be NSFW.
Clik here to view.
:)

Anyway, you can also set the ‘MAILFROM’ value when editing the crontab.
Just add the following to the top of your crontab:

MAILFROM=your@email.tld

Thanks Patrick for letting it me try again. But this option is only available on newer machines. (I can’t find it on rhel 5/6)


Viewing latest article 9
Browse Latest Browse All 10

Trending Articles