Python does not get environment variable when using cron.

S

Stephen Cattaneo

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
 
A

Asun Friere

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.
 
J

John Nagle

Stephen said:
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
 
S

Stephen Cattaneo

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

<cron date / time> set; echo "-----------------"; echo "import os; print
os.environ" | python

Cheers,

S


- -----Original Message-----
- From: Asun Friere [mailto:[email protected]]
- Sent: Sunday, August 17, 2008 7:55 PM
- To: (e-mail address removed)
- Subject: Re: Python does not get environment variable when using cron.
-
- On Aug 18, 11:15 am, "Stephen Cattaneo"
- > 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.
 
A

Asun Friere

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

<cron date / time> set; 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,831
Latest member
HealthSmartketoReviews

Latest Threads

Top