472,127 Members | 1,640 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,127 software developers and data experts.

Python does not get environment variable when using cron.

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
4 7264
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
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
- 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
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.

Similar topics

12 posts views Thread by Raymond Hettinger | last post: by
14 posts views Thread by David MacQuigg | last post: by
19 posts views Thread by Eric | last post: by
8 posts views Thread by Harlin Seritt | last post: by
4 posts views Thread by vagrantbrad | last post: by
4 posts views Thread by Aidan | last post: by
reply views Thread by Cameron Simpson | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.