简体   繁体   中英

Python not able to get iotop output

I am working on a python script which needs to read the output of iotop. But stdout appears to be blank. The goal is to have the output of iotop stored as a string which I can then later search and use. This is running on Ubuntu 16.04 w/ python 3.6. I am calling the script with "sudo python3.6 script.py" as iotop requires admin rights.

p = subprocess.Popen(['iotop','-b','-n','1','-a','-k'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
try:
    outs, errs = p.communicate(timeout=15)
except subprocess.TimeoutExpired:
    p.kill()
    outs, errs = p.communicate()
returncode=p.returncode
print(outs.decode('UTF-8'))

Running this command in the bash terminal gives me the expected output. What am I doing wrong?

sudo iotop -b -n 1 -a -k

"-n""1"应该是单独的列表元素

删除shell=True使代码按照我想要的方式工作。

p = subprocess.Popen(['iotop','-b','-n','1','-a','-k'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM