Paul
Paul

Reputation: 5974

check if script ran successfully?

I have code that runs commands in bash (each bash command runs a function python script), I wish to check if the script ran successfully or not, as in was there any error. Here is the code:

    for command in command_list:
        if subprocess_cmd(command):
            print "success"
        else:
            print "fail"

def subprocess_cmd(command):
    process = subprocess.Popen(command.split(), stdout=subprocess.PIPE)
    proc_stdout = process.communicate()[0].strip()  

this always prints fail, even though everything runs fine, is what I am doing making sense? How do I do this?

Edit: answer is the returncode attribute of popen.

Upvotes: 1

Views: 4101

Answers (1)

Ashwini Chaudhary
Ashwini Chaudhary

Reputation: 250931

That's because your function subprocess_cmm is not returning anything, so by default None is returned which is a Falsy value.

Default return value of a function is always None.

>>> def func():
...     pass
>>> print func()
None

Update:

import shlex
for command in command_list:
   try:
       out = subprocess.check_output(shlex.split(command))
       print out
   except CalledProcessError:
       print "fail"

Upvotes: 1

Related Questions