Join the Stack Overflow Community
Stack Overflow is a community of 6.5 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up
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)

share|improve this question
2  
Why are you calling whole scripts like this? Why not import the 'inner' script, or the parts of it you need, and run those? Like why not call a function in the 'inner' script? – davo36 May 9 at 4:50
    
    
You could test sys.argv[0] to prevent running yourself, which could cause a horrible fork bomb (continually spawning process). – cdarke May 9 at 5:42

You can just run your other script as a module. Let's say you have the folder:
dir
   |---main.py
   |---imported.py
And you want to run the second from the first. Just use import, like that:

import imported #the file name without the extension

It will run that file, and you will be able to use it's vars and functions in your main script.

share|improve this answer
    
It's not possible in my case because I have different options to give to the spawned script. – Dheeraj Chakravarthi May 9 at 7:05
    
Just insert your script into a function, and pass the arguments... – Yotam Salmon May 9 at 15:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.