Python does not get environment variable when using cron.

Asun Friere afriere at yahoo.co.uk
Sun Aug 17 22:55:00 EDT 2008


On Aug 18, 11:15 am, "Stephen Cattaneo"
<Stephen.Catta... at u4eatech.com> wrote:
> Hello all,
>
> I am attempting to execute an automated test (written in Python) via
> cron.  I have to check the HOSTNAME variable as part of the test, oddly
> under cron the HOSTNAME environment variable is not in the os.environ
> dictionary.  I know that cron runs in a subshell that does not have all
> of the normally set environment variables.  HOSTNAME is not one of those
> variables, it is set even in cron's subshell.  Why doesn't python get
> this variable?  Is this a bug in python2.4?
>
> >From a cronjob to check environment variables in cron's shell vs
>
> python's os.environ (please excuse my lack of creativity in naming
> convention)-
>
> BASH=/bin/bash
> BASH_ARGC=()
> BASH_ARGV=()
> BASH_EXECUTION_STRING='set; echo "-----------------"; echo "import os;
> print os.environ" | python'
> BASH_LINENO=()
> BASH_SOURCE=()
> BASH_VERSINFO=([0]="3" [1]="1" [2]="17" [3]="1" [4]="release"
> [5]="i686-redhat-linux-gnu")
> BASH_VERSION='3.1.17(1)-release'
> DIRSTACK=()
> EUID=501
> GROUPS=()
> HOME=/home/regression
> HOSTNAME=regress5
> HOSTTYPE=i686
> IFS=$' \t\n'
> LOGNAME=regression
> MACHTYPE=i686-redhat-linux-gnu
> OPTERR=1
> OPTIND=1
> OSTYPE=linux-gnu
> PATH=/usr/local/bin:/bin:/usr/bin:/home/regression/bin
> PPID=819
> PS4='+ '
> PWD=/home/regression
> PYTHONPATH=/home/regression/lib
> SHELL=/bin/bash
> SHELLOPTS=braceexpand:hashall:interactive-comments
> SHLVL=1
> TERM=xterm
> UID=501
> USER=regression
> _=/bin/bash
> -----------------
> {'TERM': 'xterm', 'SHELL': '/bin/bash', 'SHLVL': '1', 'PYTHONPATH':
> '/home/regression/lib', 'PWD': '/home/regression', 'LOGNAME':
> 'regression', 'USER': 'regression', 'HOME': '/home/regression', 'PATH':
> '/usr/local/bin:/bin:/usr/bin:/home/regression/bin', '_':
> '/usr/bin/python'}
>
> Thanks,
>
> Steve

What did you run in the cronjob to get back all those variables?

I just ran /usr/bin/env (on a 2.6.x linux box) as a cronjob and only
got back SHELL, USER, PATH, PWD, SHLVL, HOME, LOGNAME.  This is the
same set of variables os.environ (python2.4.4) gives me when run as a
cronjob.  I tried the same thing on a bladeserver running SunOS5.9 and
got a smaller set of variables, again identical between /usr/bin/env
and os.environ.



More information about the Python-list mailing list