Keeping os context possible? Re: Python a good thing for sysadmins ?

Magnus Lycka magnus at thinkware.se
Wed Sep 26 01:59:41 CEST 2001


Alex Martelli wrote:
> 
> "Magnus Lycka" <magnus at thinkware.se> wrote in message
> news:3BA14452.B72EFE1A at thinkware.se...
>     ...
> > A unix shell script or an NT cmd file would live in the os
> > context. It wouldn't spawn a new process for every command,
> 
> Unix shell scripts always spawn a new process for every
> command, where the "command" is an external program (or
> other script) rather than a shell builtin.  So does CMD.EXE,
> actually, it just has stuff built-in that most Unix shells
> keep out, such as file-copying.  So I'm not sure what that
> 'db2' command (program?) is doing that lets it keep state
> from one call to the other under a shell but not when run
> from Python...!

Sorry, you're right about the spawned process of course.
In general...although you can source shell files to avoid
spawning a new shell, right?

Anyway, I didn't really check, but I guess that the db2
command sets an environment variable in the environment of
the shell where the command is invoked. (However that is
done, but I guess there is a C call for that.)

So if you run 
$ db2 login bla bla bla

there will probably be something like below which wasn't there before

$ echo $DB2_SESSION_ID (just guessing here)
123123123121

And when you continue with

$ db2 sql select '*' from foo

db2 will know your context concerning user identity, that you're
logged in and which database you are connected to by reading that
variable.

with os.system() from within Python, this is lost. I guess I could
sovle this (in an ugly way) by doing a os.popen('db2 login bla bla
bla ; set'), read all the variables and make a loooong string for
setting them all, and prepending that to the next call. But if I
need to get some environment variable in a system call where I also
need some real data, like sql select output...then things get more
complex. I'll have to use temporary files I guess...

Then it seems much simpler to give up and use bash or ksh...

/Magnus

-- 
Magnus Lyckå | Älvans väg 99 | magnus at thinkware.se | tel: 070-582 80 65
Thinkware AB | 907 50  UMEÅ  | www.thinkware.se    | fax: 070-612 80 65



More information about the Python-list mailing list