newbie script

Eric Brunel eric.brunel at pragmadev.N0SP4M.com
Wed Sep 24 12:05:04 EDT 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.

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





More information about the Python-list mailing list