2

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

Krishna
  • 131
  • 2
  • 11

1 Answers1

1

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()
Squirrel
  • 2,262
  • 2
  • 18
  • 29