parse an environment file

J

Jason Friedman

$ 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?
 
S

Steven D'Aprano

$ 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.
 
U

Ulrich Eckhardt

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
 
A

Alain Ketterlin

[...]
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top