LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 07-07-2008, 01:44 AM   #1
CyberDog3K
LQ Newbie
 
Registered: Jun 2007
Distribution: Ubuntu, Gentoo
Posts: 10

Rep: Reputation: 0
setting a global $mailto


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?

cron 3.0pl1-100ubuntu2 - Ubuntu Hardy
 
Old 07-07-2008, 07:10 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Quote:
Originally Posted by CyberDog3K View Post
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.
It is not only possible it also is the place to configure it. Maybe elaborate, post an example?
 
Old 07-07-2008, 08:24 PM   #3
CyberDog3K
LQ Newbie
 
Registered: Jun 2007
Distribution: Ubuntu, Gentoo
Posts: 10

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by unSpawn View Post
It is not only possible it also is the place to configure it. Maybe elaborate, post an example?
in /etc/crontab:
...
MAILTO=cron-alias@somewhere.com
...

in /etc/cron.d/test:
*/5 * * * * root echo "hi $MAILTO"


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.
 
Old 07-08-2008, 12:27 PM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
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.
 
Old 07-08-2008, 06:13 PM   #5
CyberDog3K
LQ Newbie
 
Registered: Jun 2007
Distribution: Ubuntu, Gentoo
Posts: 10

Original Poster
Rep: Reputation: 0
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:

LANGUAGE=en
HOME=/root
LOGNAME=root
PATH=/usr/bin:/bin
LANG=en_US.UTF-8
SHELL=/bin/sh
PWD=/root
 
Old 07-08-2008, 07:43 PM   #6
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
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...
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Global proxy setting petestaar Linux - Networking 4 11-13-2011 12:35 PM
Setting global varible Corrado Linux - Software 3 03-05-2007 02:24 PM
Setting Global Konqueror Background Neruocomp Linux - Software 1 09-11-2005 09:27 PM
Setting global system variables AzidStar Linux - Newbie 5 03-11-2005 09:03 AM
Setting global variables from a crontab job davee Programming 2 10-06-2003 07:05 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 09:39 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration