473,749 Members | 2,513 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Re: Python does not get environment variable when using cron.

On 17Aug2008 21:25, John Nagle <na***@animats. comwrote:
Stephen Cattaneo wrote:
>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?
Because $HOSTNAME is a bash specific variable, set by bash but NOT
EXPORTED! Like $0 and a bunch of other "private" variables, subprocesses
do not inherit this value. From "man bash":
Shell Variables
The following variables are set by the shell:
[...]
HOSTNAME
Automatically set to the name of the current host.

Note that "set" does not imply "exported". Only exported vairables are
seen by subprocesses.
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.
This statement is false.

Cron hands _all_ jobs to a shell. From "man 5 crontab":

The ``sixth'' field (the rest of the line) specifies the command to be
run. The entire command portion of the line, up to a newline or % char-
acter, will be executed by /bin/sh or by the shell specified in the SHELL
variable of the cronfile.

That's from Vixie cron, but all UNIX crons hand the command part of the
cron line to a shell, usually /bin/sh.

You're probably confused by the fact that cron does not invoke "login" shells
(with their associated initialisation from /etc/profile and $HOME/.profile).
If you have
a crontab line like
10 3 * * * /usr/bin/python someprogram.py
there's no shell.
Not so. A shell will be interpreting the string "/usr/bin/python
someprogram.py" , mch as happens when you type this on the command line.

You can try
10 3 * * * /bin/sh /usr/bin/python someprogram.py
which will load a shell, which in turn will load Python.
Even more not so. The command "/bin/sh /usr/bin/python someprogram.py"
will attempt to parse the file "/usr/bin/python" as though it were a
shell script. That's almost certainly not what you wanted.

To do what you describe requires the line:

/bin/sh -c '/usr/bin/python someprogram.py'

which hands the command string "/usr/bin/python someprogram.py" to
/bin/sh for interpretation. Were you to do this as a cron line, the
string: "/bin/sh -c '/usr/bin/python someprogram.py' " would be handed
to /bin/sh for interpretation. That interpreter in turn then hands
"/usr/bin/python someprogram.py" to /bin/sh for interpretation.

A parent-child nested process listing (with some fake quoting tossed in
to show the strings) would look like this:

/bin/sh -c "/bin/sh -c '/usr/bin/python someprogram.py' "
/bin/sh -c '/usr/bin/python someprogram.py'
/usr/bin/python someprogram.py

i.e. three process alive.
Or, in Python, you can use "socket.gethost name()", which will
get you the host name used for networking purposes.
Or, on a cron line (after the time fields, omitted here):

HOSTNAME=`hostn ame`; export HOSTNAME; python someprogram.py

Cheers,
--
Cameron Simpson <cs@zip.com.auD oD#743
http://www.cskk.ezoshosting.com/cs/

186,282 miles per second - Not just a good idea, It's the Law!
Aug 18 '08 #1
0 3474

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
2960
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, then explain what happened (again, in blindingly obvious terms). There's extra credit for giving a design insight as to why things are as they are.
14
2858
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" in the subject of any post to this newsgroup. Please ignore the old thread. I've also updated my webpage http://ece.arizona.edu/~edatools/Python Anyone with some good ideas for "Python 3" is welcome to contribute. I hope GvR won't sue me for...
19
5824
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 name in the Command Prompt. Has anyone used the Windows Task Scheduler to run .py scripts, and if so isthere some intracacy to it? Is there a more UNIX version of a cron program one can run on Windows? Has anyone written a simple cron...
4
4153
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 why; my code works fine when run manually from the command prompt. On final thing: What would be the best way to turn off the recording? Can someone help me?
4
3052
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 dns records at my dns provider, zoneedit. So basically, I've setup dynamic dns using python. Once the ip compare and/or update is complete, I log the results to a text file called update.log. When I run the program in a bash shell with the...
2
2165
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 script using: import os os.setegid os.setgid
4
3959
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 to create a system backup script that creates an image of the system, filters the output though gzip, and then uploads the data (via ftp) to a remote site.
4
7557
by: Stephen Cattaneo | last post by:
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...
0
200
by: Edwin.Madari | last post by:
source in or execute .profile (or .bash_profile which ever is applicable toyou) as a first thing in the cron to get environment variables. hope that helps. Edwin -----Original Message----- From: python-list-bounces+edwin.madari=verizonwireless.com@python.org On Behalf Of Eric Wertman Sent: Sunday, August 17, 2008 9:39 PM
0
8997
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
9335
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8257
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6801
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6079
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4709
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4881
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3320
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2218
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.