Python does not get environment variable when using cron.
nagle at animats.com
Mon Aug 18 06:25:11 CEST 2008
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.
More information about the Python-list