parse an environment file

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sun Sep 30 21:17:15 EDT 2012


On Sun, 30 Sep 2012 18:11:09 -0600, Jason Friedman wrote:

> $ crontab -l
> * * * * * env
> 
> This produces mail with the following contents:
[snip]

Yes, env returns the environment variables of the current environment.


> On the other hand
> 
> $ env
> 
> produces about 100 entries, most of which are provided by my .bashrc;
> cron provides only a limited number of environment variables.

That's because it's a different environment.

 
> I want my python 3.2.2 script, called via cron, to know what those
> additional variables are.  How?

In general, you can't, because they may not even exist when your script 
runs. There's no guarantee that "your environment" (which one? you might 
have many, or none) exists at the time, and env certainly cannot guess 
which one that might be.

But specifically, if you know the ID of a process, you may be able to see 
that process' environment variables by reading the /proc/<PID>/environ 
virtual file, which is a NULL-delimited list of environment variables.

As usual, permissions apply. In general, you can read your own processes, 
but not those of other users.



-- 
Steven



More information about the Python-list mailing list