please help me understand os.system() result

Eric Brunel eric.brunel at pragmadev.com
Tue May 20 09:12:23 EDT 2003


Roman Suzi wrote:
> Under Linux I have very strange results for exit status info:
> 
> 
>>>>os.system("exit 7")
>>>
> 1792
> 
>>>>os.system("python -c 'import sys;sys.exit(1)'")
>>>
> 256
> 
>>>>os.system("python -c 'import sys;sys.exit(2)'")
>>>
> 512
> 
> True for my RH 7.3 and 6.2, for Pythons 1.5.2, 2.1, 2.2.2.
> 
> Any ideas?
> 
> Sincerely yours, Roman A.Suzi

According to the library reference ( 
http://www.python.org/doc/current/lib/os-process.html#l2h-1226 ):

"The return value [of os.system] is the exit status of the process encoded in 
the format specified for wait(), except on Windows 95 and 98, where it is always 0."

So let's go two paragraphs further for the doc of the wait function:

"[...] return a tuple containing its pid and exit status indication: a 16-bit 
number, whose low byte is the signal number that killed the process, and whose 
high byte is the exit status (if the signal number is zero); the high bit of the 
low byte is set if a core file was produced."

So it enables you to know if the command you ran through system exited normaly 
(status % 256 == 0); if so, its exit code (status / 256); if not, the killing 
signal (status % 256) and if a core file was produced (status / 256 == 1).

Please note that wait, system and the like have quite a tendency to return None 
if the command ran to completion without problem, so you're unlikely to get 0 as 
a result.

HTH
-- 
- Eric Brunel <eric.brunel at pragmadev.com> -
PragmaDev : Real Time Software Development Tools - http://www.pragmadev.com





More information about the Python-list mailing list