3

For a long time, I have been used to thinking that any environment I wanted in cron, I basically needed to set in the crontab itself -- see the "Different Environment", likely top answer here: Reasons why crontab does not work

but... on my recent Ubuntu 14.04 install, I have a crontab that looks like this:

*/1 * * * * env > /tmp/cron.env

an /etc/environment file that looks like this:

PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games"
RACK_ENV=production
LC_ALL=en_US.UTF-8
FROM_ETC_ENVIRONMENT=testing

and the output cron.env file looks like this:

FROM_ETC_ENVIRONMENT=testing
HOME=/home/devops
LOGNAME=devops
RACK_ENV=production
PATH=/usr/bin:/bin
LANG=en_US.UTF-8
SHELL=/bin/sh
LC_ALL=en_US.UTF-8
PWD=/home/devops

the FROM_ETC_ENVIRONMENT is a variable I created with that unique name to ensure it couldn't possibly be coming from anywhere else.

I've looked around for documentation on when this might have changed / etc... I guess my question is really this:

Is this a behavior I can rely on from Ubuntu 14.04 going forward? (Anyone know when it was introduced? Or has it always been this way, and I just didn't realize it -- the top answer in the Reasons why crontab does not work link says that cron doesn't read from /etc/environment at all, but that seems to be wrong based on my tests.

Kem Mason
  • 183

1 Answers1

6

That answer is outdated (at least, that section is, which is from 2011). If you inspect /etc/pam.d/cron:

$ grep pam_env /etc/pam.d/cron
# Read environment variables from pam_env's default files, /etc/environment
# and /etc/security/pam_env.conf.
session       required   pam_env.so
session       required   pam_env.so envfile=/etc/default/locale

pam_env is the PAM module that loads /etc/environment, and as you can see, it is used by cron, at least as of 14.04. And I checked a 12.04 system, and it too had pam_env loaded for cron.

muru
  • 197,895
  • 55
  • 485
  • 740