python status,output = commands.getstatusoutput("cmd") 诡异现象

python status,output = commands.getstatusoutput("cmd") 诡异现象


test.py

#!/usr/bin/python

import commands
import sys


def do_cmd(cmd='pwd'):
  status,output = commands.getstatusoutput(cmd);
  print cmd
  print 'status code: ', status
  print 'output: ', output



if __name__ == '__main__':
  if len(sys.argv) != 2 :
    print "usage: %s cmd" % sys.argv[0]
    sys.exit(1);

  cmd = sys.argv[1]
  do_cmd(cmd)
  print
  cmd = "( " + cmd + " &)"
  do_cmd(cmd)


执行命令行: 

python test.py "ls -l xxxxxx"

ls -l xxx
status code:  512
output:  ls: xxx: 没有那个文件或目录


( ls -l xxx &)
status code:  0
output:  ls: xxx: 没有那个文件或目录


虽然命令命令没有成功执行,第二种方式仍然返回状态码: 0 ; 表示成功;


诡异的bug,调了半天;

记录一下!



你可能感兴趣的:(Linux,python)