7

Can I do the following to change where mail goes for one specific crontab job in root's crontab (don't sent to root for Do Something job, just send to porklover):

# Make a hambone appear
* * * * * /scripts/hambone.pl

# Eat Ham
* * * * * /scripts/manga_ham.py

OLDMAILTO=$MAILTO

MAILTO=porklover.cramble.edu

#Do something
* * * * * /scripts/chown_abrahams_niece.sh

MAILTO=$OLDMAILTO
4

In traditional cron implementations, you can't do it this way. Variable definitions in a crontab are global, and cron uses the same email address for all commands.

One workaround would be to use a local address and redirect based on the input.

Another method to send the output of a job to a different address is to do the sending manually. See How to pipe output from one process to another but only execute if the first has output? for various methods.


Some modern cron implementations, at least the one in systemd, parse the crontab differently (thanks geedoubleya, jarauh): only assignments above a job line apply to that job. So if you're on a modern Linux system, there's a chance you can do it. Check the documentation on your system (man 5 crontab).

You still can't do what you proposed in your question, because cron doesn't expand the right-hand side of variable assignments. MAILTO=$OLDMAILTO sets MAILTO to a value beginning with a dollar sign. You need to repeat the old address.

# Make a hambone appear. Email sent to the user running the crontab.
* * * * * /scripts/hambone.pl

MAILTO=abrahams@example.com
#Do something. Email sent to abrahams@example.com
* * * * * /scripts/chown_abrahams_niece.sh

MAILTO=
# Hunt rabbits. Doesn't send email.
* * * * * /scripts/hunt.pl --rabbits

MAILTO=bob
# Eat Ham. Email sent to bob on the local machine.
* * * * * /scripts/manga_ham.py

Also note that MAILTO=porklover.cramble.edu would send mail to the account porklover.cramble.edu on the local machine. That's probably not what you want.

  • In his answer, user @geedoubleya claims that it is possible to change the email address between commands. Might it depend on the cronjob version? – jarauh Feb 9 '18 at 7:48
  • 1
    @jarauh Thanks, it does seem to depend on the cron implementation. Given that people have patched cron in all kinds of ways without always changing the credits or the version number, I don't know what versions of cron do it this way. – Gilles Feb 9 '18 at 9:07
  • Thanks for the information. What makes it worse is that people also don't care to document their changes in the corresponding man pages. In particular, the format for the crontab is not documented in all details (e.g. the order of the lines does matter). – jarauh Feb 10 '18 at 7:45
1

While setting variables like this in a cron entry is not possible, what crontab will allow you to do is override the MAILTO variable for a single entry then set it back for subsequent entries.

Example:

# Eat Ham
* * * * * /scripts/manga_ham.py

MAILTO=porklover.cramble.edu

#Do something
* * * * * /scripts/chown_abrahams_niece.sh

MAILTO=root

As a down side to this, if the default MAILTO setting is changed (in /etc/crontab for example) it would need to be changed in this cron entry too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.