By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,608 Members | 3,801 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,608 IT Pros & Developers. It's quick & easy.

Python does not get environment variable when using cron.

P: n/a
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

Aug 18 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Aug 18, 11:15 am, "Stephen Cattaneo"
<Stephen.Catta...@u4eatech.comwrote:
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.
Aug 18 '08 #2

P: n/a
Stephen Cattaneo 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?
Cron doesn't normally use a shell at all. It just runs the
requested program in a subprocess. So there's no shell involved,
and you don't get a shell-type user environment. If you have
a crontab line like

10 3 * * * /usr/bin/python someprogram.py

there's no shell. You can try

10 3 * * * /bin/sh /usr/bin/python someprogram.py

which will load a shell, which in turn will load Python.

Or, in Python, you can use "socket.gethostname()", which will
get you the host name used for networking purposes.

John Nagle
Aug 18 '08 #3

P: n/a
- What did you run in the cronjob to get back all those variables?

<cron date / timeset; echo "-----------------"; echo "import os; print
os.environ" | python

Cheers,

S
- -----Original Message-----
- From: Asun Friere [mailto:af*****@yahoo.co.uk]
- Sent: Sunday, August 17, 2008 7:55 PM
- To: py*********@python.org
- Subject: Re: Python does not get environment variable when using cron.
-
- On Aug 18, 11:15 am, "Stephen Cattaneo"
- <Stephen.Catta...@u4eatech.comwrote:
- 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.

Aug 18 '08 #4

P: n/a
On Aug 19, 2:37 am, "Stephen Cattaneo" <Stephen.Catta...@u4eatech.com>
wrote:
- What did you run in the cronjob to get back all those variables?

<cron date / timeset; echo "-----------------"; echo "import os; print
os.environ" | python

Cheers,

S
As I should have noted from $BASH_EXECUTION_STRING. I'd be half
dangerous if I just ... paid ... ATTENTION!

OK, the difference between using bash's builtin 'set' and '/usr/bin/
env' is that 'env' looks at ENVIRONMENT variables, whereas 'set' looks
at SHELL variables (which in bash includes HOSTNAMES). Since shell
variables are private to the shell, 'os.environ' can only see
environment variables.

In other words if you want 'os.environ' to see a variable 'set' sees,
you have to export it from the shell to the environment, eg.
* * * * * export HOSTNAME;echo "import os;print
os.environ['HOSTNAME']" | /usr/bin/python

But note this solution depends on the particular predilections of the
bash shell. If you are solely concerned with finding the hostname,
the more generic solution would be the one using
'socket.gethostname' (as John Machin suggested above), which
presumably calls the C library routine of the same name.

cheers
Aug 19 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.