newbie script

Eric Brunel eric.brunel at
Wed Sep 24 18:05:04 CEST 2003

Gerhard Häring wrote:
> ataraxia2500 wrote:
>> I wanna do a script that launch a programm xyz and relaunch xyz if it
>> crashes. what kind of module do I need for such a task?
> Something like this:
> import os
> while 1:
>     os.system("j:/UT2003/System/UT2003.exe")
> os.system executes a program and waits, until the program is terminated. 
> But there's no way to know wether the program was terminated 
> deliberately or wether it crashed.

There is a way. Quoting the documentation for the os module for os.system:

The return value 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.

And for os.wait:

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.

According to my experience, it is also better to test for None in the return 
value, since it seems to happen when everything is OK. So, doing:

while 1:
   exitStatus = os.system("command...")
   if exitStatus is None: break
   if exitStatus & 0xFF == 0: break

should do the trick.

- Eric Brunel <eric dot brunel at pragmadev dot com> -
PragmaDev : Real Time Software Development Tools -

More information about the Python-list mailing list