Search code examples
pythonlsf

Python redirecting output from an LSF command


I am trying to run an LSF command , 'bjobs' inside a python code using subprocess and I am unable to get the output into a variable

Ways I have already tried and failed are as follows:

proc = subprocess.Popen(['bjobs'],stdout=subprocess.PIPE)
print proc.stdout.read() ## Not working

stdout,stderr = subprocess.Popen(['bjobs'],stdout=subprocess.PIPE).communicate()

print stdout # prints empty line

I do not want to redirect that to a physical file.

So please help me to find a way to directly capture them to a variable


Solution

  • As pointed out by a comment above, the "No unfinished job found" message is printed to stderr:

    [~]$ bjobs > /dev/null
    No unfinished job found
    [~]$ bjobs >& /dev/null
    [~]$
    

    If you want all bjobs output you should redirect subprocess stderr to stdout:

    proc = subprocess.Popen(["bjobs"],stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
    print proc.stdout.read()