Linux - ServerThis forum is for the discussion of Linux Software used in a server related context.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I want to create a default MAILTO for my cron jobs to use (this is a private system), without having to set MAILTO= in every file in /etc/cron.d. I had hoped I could set MAILTO in /etc/crontab (since variables like SHELL and PATH work there), but it doesn't appear to be possible. Is there a way to make all cron jobs use a default mailto address?
When the 'test' job runs, it sends an empty email to root, rather than to cron-alias. This is of course just a test to simulate any stdout from an actual cron script. If I add the MAILTO to the cron.d/test file, only then does it mail to cron-alias.
Just tested this with vixie-cron-4.1-72 adding the MAILTO and a '*/1 * * * * root /bin/env' test line to /etc/crontab. Sending e-mail to an address on another machine works (/var/log/maillog shows Sendmail sending it). There are three workarounds I can think of: catching stdin and stdout and piping that through your mailer, making a local account and having a procmail recipe forward it to the remote address, or making a local account and having an /etc/aliases entry send it off to the remote address, but those all go against your idea of configuring one mailto in a central location. You've shown (by adding your MAILTO to "/etc/cron.d/test") the generic mechanism works (so it's not a mail problem or a username lookup problem or something like that) so this could be a bug in your version. Checking your distributions bug tracker may shed a light on that.
Now that I've learned to read, I found that the cron manpage actually does shed some light on this issue:
"...Additionally, cron reads the files in /etc/cron.d: it treats the files in /etc/cron.d as in the same way as the /etc/crontab file (they follow the special format of that file, i.e. they include the user field). However, they are independent of /etc/crontab: they do not, for example, inherit environment variable settings from it."
Alas, it does not give any clues as to how to actually feed environment variables into it, nor where the variables these jobs have actually come from. Despite being run as root, the env vars do not match the root user's nor the master crontab's:
Since /etc/cron.d/ cronjobs submit their full commandline to '/bin/sh' you can use any form of '/bin/env MAILTO=some@addr.ess /some/command' or 'export MAILTO=some@addr.ess; /some/command' to feed envvars into it. Of course running a script instead (and sourcing MAILTO from a central resource in /etc ?) is easier on the eyes instead of finding the full commandline in your cronjob e-mail subject, and this again goes against your idea of having one central place to set the MAILTO...
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.