473,396 Members | 1,707 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 7477
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Raymond Hettinger | last post by:
Here are four more mini-mysteries for your amusement and edification. In this episode, the program output is not shown. Your goal is to predict the output and, if anything mysterious occurs,...
14
by: David MacQuigg | last post by:
I am starting a new thread so we can avoid some of the non-productive argument following my earlier post "What is good about Prothon". At Mr. Hahn's request, I will avoid using the name "Prothon"...
19
by: Eric | last post by:
I'm trying to have some scripts run periodically on Windows XP and found the "Task Scheduler" did not execute my scripts. My scripts are of the form scriptName.py, and will run just by invoking that...
4
by: Carl | last post by:
Hi, I have this little code snippet that I use for recording audio streams. My problem is that I want to schedule my recordings with crontab. This does not work, however. I cannot figure out...
8
by: Harlin Seritt | last post by:
I have a remote linux server where I can only access it via ssh. I have a script that I need to have run all the time. I run like so: python script.py & It runs fine. When I log off ssh I...
4
by: vagrantbrad | last post by:
I'm using python 2.4 running on Fedora Core 4. I have written a python program called ipscan.py that checks the external ip address of my cable internet connection, and on change, will update the...
2
by: Henry Hollenberg | last post by:
Hello, I have written a script that uses environment variables set during a particular users login in ".bash_profile" and ".profile". I have changed to that users uid and gid in my python...
4
by: Aidan | last post by:
Hi, I'm having a bit of trouble with a python script I wrote, though I'm not sure if it's related directly to python, or one of the other software packages... The situation is that I'm trying...
0
by: Cameron Simpson | last post by:
On 17Aug2008 21:25, John Nagle <nagle@animats.comwrote: Because $HOSTNAME is a bash specific variable, set by bash but NOT EXPORTED! Like $0 and a bunch of other "private" variables, subprocesses...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.