4

I am setting up a new Linux computer. I installed s-nail (sudo apt install s-nail) to send emails from command line (and my scripts) to any email addresses over the Internet. After adding the necessary lines to /etc/s-nail.rc with my gmail credentials for gmail SMTP server, things are now working properly.

There are no other packages installed on this computer (mail, mailx, sendmail, postfix, ssmtp, etc. are all absent).

But right now, I cannot get crontab to email me (at my gmail address) when cron job fails. I don't really understand how things exactly work, but I guess s-nail does not need a local MTA while cron requires one?

Is there any way to get cron to email me, without installing any other packages? Can I create symbolic links to mail, mailx and sendmail (all pointing to s-nail) in order to "fool" crontab?

Thanks.

Shang Zhang
  • 141
  • 2

1 Answers1

3

1. Make cron use s-nail as mail

cron has an internal mechanism to send job output to e-mail recipients. That mechanism expects an external program with the interface of sendmail (see) or, on Ubuntu, also apparently an interface of mail (see). Probably, cron chooses the first of sendmail and mail, like other programs do (see).

What does not work is to symlink sendmail to s-nail. The trouble is, s-nail is not compatible. cron will complain about an unrecognized custom header option -S, a missing mail body, or some such. Rather, the author's webpage says:

It is intended to provide the functionality of the POSIX mailx(1) command

The interface of mailx is however similar to mail! And indeed, a workaround seems to be this:

You can make a symbolic link from /usr/bin/mail to /usr/bin/s-nail. [… M]ake sure that /usr/sbin/sendmail is not installed as it [will] first try to use sendmail [source]

These instructions are written for how to let the unattended-upgrades script use s-nail to send e-mails, but cron behaves the same way, so give it a try. (I did not try myself yet.)

2. Or use s-nail in individual cronjobs

If solution 1 from above does not work out and you really want to use s-nail, you can integrate the mailing of output into each of the commands that you want cron to execute. A crontab entry could look like this:

# every minute, execute your_command and send sterr by e-mail (but not stdout)
* * * * * { ./your_command > /dev/null ; } 2>&1  | s-nail -s 'A subject' target@example.com

Inspiration: a similar technique seen used with mail (see, see).

3. Or use msmtp or ssmtp instead of sendmail

If you are not yet sold on using s-nail, this alternative is a rather elegant and simple solution. For this, you let cron rely on the sendmail interface for mailing output, and install one of the modern sendmail alternatives that integrating most of the conveniences that s-nail provides (such as sending via external SMTP servers directly, without the need for a local mailserver).

ssmtp and msmtp are such programs. See here on Ask Ubuntu for good instructions to use cron with ssmtp. When installing ssmtp, it will already install a symlink from sendmail to its binary.

But apparently, ssmtp is unmaintained now, and msmtp is its proposed successor (migration guide).

tanius
  • 6,303
  • 1
  • 39
  • 49