parse an environment file

Discussion in 'Python' started by Jason Friedman, Oct 1, 2012.

  1. $ 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?
    Jason Friedman, Oct 1, 2012
    #1
    1. Advertising

  2. 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
    Steven D'Aprano, Oct 1, 2012
    #2
    1. Advertising

  3. 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
    Ulrich Eckhardt, Oct 1, 2012
    #3
  4. Jason Friedman <> 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.
    Alain Ketterlin, Oct 1, 2012
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dave Angel

    Re: parse an environment file

    Dave Angel, Oct 1, 2012, in forum: Python
    Replies:
    0
    Views:
    198
    Dave Angel
    Oct 1, 2012
  2. Chris Angelico

    Re: parse an environment file

    Chris Angelico, Oct 1, 2012, in forum: Python
    Replies:
    0
    Views:
    247
    Chris Angelico
    Oct 1, 2012
  3. Jason Friedman

    Re: parse an environment file

    Jason Friedman, Oct 1, 2012, in forum: Python
    Replies:
    4
    Views:
    239
    Ramchandra Apte
    Oct 2, 2012
  4. Chris Angelico

    Re: parse an environment file

    Chris Angelico, Oct 1, 2012, in forum: Python
    Replies:
    0
    Views:
    245
    Chris Angelico
    Oct 1, 2012
  5. Chris Angelico

    Re: parse an environment file

    Chris Angelico, Oct 1, 2012, in forum: Python
    Replies:
    2
    Views:
    244
    88888 Dihedral
    Oct 1, 2012
Loading...

Share This Page