Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > parse an environment file

Reply
Thread Tools

parse an environment file

 
 
Jason Friedman
Guest
Posts: n/a
 
      10-01-2012
$ crontab -l
* * * * * env

This produces mail with the following contents:

HOME=/home/spjsf
LOGNAME=spjsf
PATH=/usr/bin:/bin
PWD=/home/spjsf
SHELL=/bin/sh
SHLVL=1
USER=spjsf
_=/usr/bin/env

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.

I want my python 3.2.2 script, called via cron, to know what those
additional variables are. How?
 
Reply With Quote
 
 
 
 
Steven D'Aprano
Guest
Posts: n/a
 
      10-01-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
 
Reply With Quote
 
 
 
 
Ulrich Eckhardt
Guest
Posts: n/a
 
      10-01-2012
Am 01.10.2012 02:11, schrieb Jason Friedman:
> $ crontab -l
> * * * * * env
>
> This produces mail with the following contents:
>

[...]
> SHELL=/bin/sh

^^^^^^^
[...]
>
> On the other hand
>
> $ env
>
> produces about 100 entries, most of which are provided by my .bashrc;


bash != sh

Instead of running a script in default POSIX shell, you might be able to
run it in bash, which will then read your ~/.bashrc (verify that from
the docs, I'm not 100% sure). Maybe it is as easy as changing the first
line to '#!/bin/bash'.

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


To be honest, I would reconsider the approach. You could patch the cron
invokation, but that still won't fix any other invokations like starting
it from a non-bash shell, filemanager, atd etc. You could instead set
these variables in a different place that is considered by more
applications. I wonder if maybe ~/.profile would be such a place.

Alternatively, assuming these environment variables are just for your
Python program, you could store these settings in a separate
configuration file instead. Environment variables are always a bit like
using globals instead of function parameters.


Good luck!

Uli

 
Reply With Quote
 
Alain Ketterlin
Guest
Posts: n/a
 
      10-01-2012
Jason Friedman <(E-Mail Removed)> writes:

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


This is not a python question. Have a look at the crontab(5) man page,
it's all explained there.

-- Alain.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: parse an environment file Jason Friedman Python 4 10-02-2012 04:16 PM
Re: parse an environment file Chris Angelico Python 2 10-01-2012 03:29 PM
Re: parse an environment file Chris Angelico Python 0 10-01-2012 02:21 PM
Re: parse an environment file Chris Angelico Python 0 10-01-2012 04:20 AM
Re: parse an environment file Dave Angel Python 0 10-01-2012 12:58 AM



Advertisments