You are not logged in.
Hi,
I am having a hard time setting up cronie for mailing output. Jump to the last sentence for the actual question.
From man 8 cron, I read that the -m option "allows you to specify a shell command to use for sending Cron mail output instead of using sendmail(8). This command must accept a fully formatted mail message (with headers) on standard input and send it as a mail message to the recipients specified in the mail headers". I do have msmtp installed. I can do this and receive email successfully:
$ msmtp -t << eof
> To: <my address>@gmail.com
>
> test
> eof
Thus I know that msmtp -t is the command I must provide with the -m option.
Then, I followed these instructions and created /etc/systemd/system/cronie.service.d/mailing.conf which contains:
[Service]
ExecStart=
ExecStart=/usr/bin/crond -n -m 'msmtp -t'
Note: the first, empty ExecStart cancels the former one from the original .service file.
After starting cronie.service, this is the result of systemctl status cronie.service:
cronie.service - Periodic Command Scheduler
Loaded: loaded (/usr/lib/systemd/system/cronie.service; enabled)
Drop-In: /etc/systemd/system/cronie.service.d
└─mailing.conf
Active: active (running) since Mon 2013-06-24 22:58:24 EDT; 2s ago
Main PID: 1457 (crond)
CGroup: name=systemd:/system/cronie.service
└─1457 /usr/bin/crond -n -m msmtp -t
Jun 24 22:58:24 electron systemd[1]: Started Periodic Command Scheduler.
Jun 24 22:58:24 electron crond[1457]: (CRON) INFO (Syslog will be used instead of sendmail.)
Jun 24 22:58:24 electron crond[1457]: (CRON) INFO (running with inotify support)
Jun 24 22:58:24 electron crond[1457]: (CRON) INFO (@reboot jobs will be run at computer's startup.)
It seems that systemd drops the single quotes and takes -t as it's own parameter instead of passing it to msmtp. You can also see that crond logged "Syslog will be used instead of sendmail". I can actually see the output of my commands in /var/log/crond.log. How should I quote the space between msmtp and -t in the ExecStart line of my .service file?
Last edited by a_neutrino (2013-06-30 21:54:53)
Offline
Hi,
I cannot advise you how to add the "-t", but I think I can help you a bit.
Cron uses sendmail, so the easiest method to use msmtp instead of sendmail would be to create a symbolic link to msmtp:
sudo ln -s /usr/bin/msmtp /usr/bin/sendmail
Unfortunately this does not work as msmtp cannot figure out which e-mail address(es) to use instead of the user. You have to create a file /etc/aliases that holds the addresses where e-mail notifications will be sent to when a user's cronjob creates an output. For example:
root: myadmin@mydomain.com
user1: user1@domain1.com
default: another_address@domain2.com user2@his_domain.com
Note: Contrary to Wiki, there are no commas separating e-mail addresses if there are a few in a line.
You have to declare /etc/aliases for msmtp to find it. Best put it in /etc/msmtprc:
aliases /etc/aliases
There is a special script distributed with Arch that is able to scan /etc/aliases for a valid domain address and then calls msmtp. Here is its path
/usr/share/doc/msmtp/find_alias/find_alias_for_msmtp.sh
Copy it to /usr/bin/ and create a simlink:
sudo cp /usr/share/doc/msmtp/find_alias/find_alias_for_msmtp.sh /usr/bin/find_alias_for_msmtp.sh
sudo ln -s /usr/bin/find_alias_for_msmtp.sh /usr/bin/sendmail
/usr/local/bin/ could be a nice place for the script, too.
And that's is. When a cronjob generates an output, cron will run sendmail (linked to the script), and the script will find an e-mail address and run msmtp.
Remember to create separate .mailrc and .msmtprc files for root in his home directory.
Here is a cronjob command I used to test it:
echo $(/home/wm/bin/myip.sh) | mail -s info myaddress@mydomain.pl; echo HAHA
Output of myip.sh is e-mailed by mail (mailx). This not generates any output for cron to e-mail. But "echo HAHA" generates the output that is e-mailed by cron through sendmail. As a result, I receive two e-mails. The recipients may be different or the same, depending on the settings in /etc/aliases.
Hope this helps.
Offline
Hi,
Thanks a lot for your help. The issue is [solved].
You have to create a file /etc/aliases that holds the addresses where e-mail notifications will be sent to when a user's cronjob creates an output.
It seems unnecessary. The MAILTO variable set in the crontab accepts a complete email address.
Contrary to Wiki, there are no commas separating e-mail addresses if there are a few in a line.
The msmtp man page also says to put commas.
There is a special script distributed with Arch that is able to scan /etc/aliases for a valid domain address and then calls msmtp. Here is its path
/usr/share/doc/msmtp/find_alias/find_alias_for_msmtp.sh
Copy it to /usr/bin/ and create a simlink:
sudo cp /usr/share/doc/msmtp/find_alias/find_alias_for_msmtp.sh /usr/bin/find_alias_for_msmtp.sh sudo ln -s /usr/bin/find_alias_for_msmtp.sh /usr/bin/sendmail
Why didn't you just :
chmod +x /usr/share/doc/msmtp/find_alias/find_alias_for_msmtp.sh
ln -s /usr/share/doc/msmtp/find_alias/find_alias_for_msmtp.sh /usr/bin/sendmail
Also, I had to modify the find_alias_for_msmtp.sh script a little because cronie calls sendmail without any parameters:
diff --git a/scripts/find_alias/find_alias_for_msmtp.sh b/scripts/find_alias/find_alias_for_msmtp.sh
index fb485dd..346f313 100644
--- a/scripts/find_alias/find_alias_for_msmtp.sh
+++ b/scripts/find_alias/find_alias_for_msmtp.sh
@@ -16,15 +16,18 @@ MSMTP=`which msmtp || echo "/usr/local/bin/msmtp"`
#
if [ "$1" = '-i' ] ; then
v_recipient=$2 # mailx calls sendmail with -i as the 1st param and the recipient as the second param
-elif [ "$1" = '-t' ] ; then # most other programs call sendmail with a -t option
+elif [ "$1" = '-t' -o $# = 0 ] ; then
+ # Most other programs call sendmail with a -t option.
+ # cronie calls sendmail without parameters and
+ # expects sendmail to find the recipient in the mail headers.
v_to=`echo "$v_msg" | grep -m 1 'To: '`
v_recipient=${v_to:4:50}
else
- v_recipient=$1 # no parameter, sendmail was called with the recipient as parameter
+ v_recipient=$1 # sendmail was called with the recipient as parameter
fi
#
-if [ $v_recipient != 0 ] ; then
+if [ "$v_recipient" != 0 ] ; then
# trim spaces from recipient email address
v_recipient="${v_recipient// /}"
Or maybe I missed something. I submitted the patch upstream to see if they think it's relevant.
Hope this helps.
Yes, it did! Thank you so much.
Offline
Hi,
(1) MAILTO seems very simple and nice, but I wanted to keep crontab just for my cronjobs.
How about a few addresses in one "MAILTO". Have you tried it? I mean normally you do not need multiple addresses, but it is nice to have such an option.
(2) Commas
That is a strange thing. Everywhere I read about it I saw the commas, but when I put them in the /etc/aliases no mail came to me from cron.
When I deleted them, e-mail started to come.
(3) The location /usr/share/doc/msmtp/find_alias/find_alias_for_msmtp.sh
Again your approach is simpler and nicer, but I wanted an executable script in /usr/bin/.
I am beginning to think of moving it to /usr/local/bin/ for my backup solution.
(4) Script modification
I have yet to read it. I am sure I learn from that. Thank you.
By the way, if upstream accepts your patch, your cron notifications will stop working because of (3). It is not a problem, just "chmod +x", but sometimes it is hard to find what has happened.
Last edited by dif (2013-06-28 09:13:20)
Offline
This short script below (sm.sh) will tell you how cron calls sendmail and what the structure of cron's e-mail is.
echo -e "The sendmail call from cron:\n\t$0 $@" > /tmp/sm
MSG=$(cat)
echo -------e-mail_structure------- >> /tmp/sm
echo -e "$MSG" >> /tmp/sm
echo -------end_of_email----------- >> /tmp/sm
To use the script replace the sendmail link with the script
sudo rm /usr/bin/sendmail
sudo ln -s /path/to/sm.sh /usr/bin/sendmail
and set this cronjob command to run
echo $(date); echo cronjob:; cat /var/spool/cron/$USER; echo /etc/aliases:; cat /etc/aliases
Running this job cron will call sm.sh, and you will be able to see the parameters cron sends to sendmail in /tmp/sm. Note that the first parameter is "-FCronDaemon" and the last is the value of $USER.
Interestingly "-t" is also passed to sendmail.
The above information will help you modify find_alias_for_msmtp.sh.
P.S. Cron does not work with MAILTO="<USER1> <USER2>"
Last edited by dif (2013-06-30 15:36:54)
Offline
Hi dif,
Your sm.sh script proved to be useful for debugging.
If cronie is started with the -m option (for example: -m /usr/local/bin/sm.sh), then it does not pass any parameters. Otherwise, it does call /usr/bin/sendmail with the parameters as you described. Therefore, I shall retract the patch I sent for find_alias_for_msmtp.sh.
Also, your script allowed me to find the answer to my #1 question! If I create this file /etc/systemd/system/cronie.service.d/mailing.conf:
[Service]
ExecStart=
ExecStart=/usr/bin/crond -n -m 'msmtp -t'
then the -t parameter is indeed passed to msmtp, even though the quotes are dropped in the reports from systemd. It did not work at first because I didn't have an /etc/aliases file or a MAILTO=complete@address.com line in my crontab. dcron, the former default scheduler, had a different way of getting the mailing address. From what I see in my system backups, I used to pass the -m parameter with the email address like so '-m email@address.com'.
Now I know and I summed up this knowledge in the wiki and marked this thread as solved.
Arch Wiki > Cron > Example with msmtp (created the section)
Arch Wiki > Msmtp > Configuring cron for msmtp (updated)
Offline