Getting return value in os.system rsh call

Erik Max Francis max at alcyone.com
Wed Jan 29 21:56:38 EST 2003


Andrew wrote:

> I'm writing a test script which kicks off tests on remote machines,
> and I need to get the return code from the remote test in my main
> program.
> 
> the relevent bit is
> 
> os.system("rsh remote test.py")
> 
> How can I get the return value of test.py?

You can't directly (this is a problem with any of these kinds of
remotely executing things), what you get is the result of the rsh (or
ssh, if that were what you were using, and as an aside it would be
advisable to switch), not the command which rsh executed remotely.

Instead you need to resport to using os.popen (or one the
popen2.popen... functions), and actually having your remote script print
its result code.  Since you're using os.system, you obviously don't care
about the output, so try something like this (untested):

	result = None
	pipe = os.popen(r"rsh remote test.py \; echo $?")
	try:
	    output = pipe.read().strip() # strip off the newline
	    result = int(output)
	finally:
	    pipe.close()
	# now result should contain the exit code

This will only work if test.py doesn't generate any output on its own;
if it does, then you'll either want to do pipe.readlines()[-1].strip()
(to get the last line) or, better yet, just toss its output altogether
(you obviously don't care about that, since you were using os.system
before); change the popen command to something like:

	r"rsh remote test.py \> /dev/null 2\> /dev/null \; echo $?"

Note the backslashes are important, since

	r"rsh remote test.py > /dev/null 2> /dev/null ; echo $?"

would mean something quite different (and exactly what you're not
wanting).

-- 
 Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
 __ San Jose, CA, USA / 37 20 N 121 53 W / &tSftDotIotE
/  \ The work will teach you how to do it.
\__/ (an Estonian proverb)
    Python chess module / http://www.alcyone.com/pyos/chess/
 A chess game adjudicator in Python.




More information about the Python-list mailing list