def run_cmd(self, cmd):
print cmd
status = os.system(cmd)
print 'Command ran with Status: %s' % status
if status:
s = 'Command Failed: Status: %s for %s' % (status, cmd)
print s
sys.exit(status)
else:
s = 'Command Success: %s' % cmd
return status
I am using this function to run a python script from another script.
Ex:
command_to_exec = 'python script_b.py'
run_cmd(command_to_exec)
Now if script_b.py fails script_a.py exits with the status.
That's okay.
Case 2:
command_to_exec = 'python script_a.py --options'
rum_cmd(command_to_exec)
This case is script_a.py running 'python script_a.py' from itself. In this case, if newly spawned script_a.py fails, the outer script_a.py is still a success, because it wasn't able to catch the failure of inner script_a.py (because of sys.exit(status))
Now, how do I handle this situation? (If inner script_a fails, outer script_a should exit)
sys.argv[0]
to prevent running yourself, which could cause a horrible fork bomb (continually spawning process). – cdarke May 9 at 5:42