获取用户应用进程信息,以方便跟踪程序进程的运行状态
查看应用程序的进程名字
[root@bogon python_auto]# vim popen.py
!/usr/bin/env python
import psutil
from subprocess import PIPE
p = psutil.Popen(["/usr/bin/python","-c","print('hello fuchao')"],stdout=PIPE)
print p.name()
测试查看:
[root@bogon python_auto]# python popen.py
python
查看应用进程的用户名
!/usr/bin/env python
import psutil
from subprocess import PIPE
p = psutil.Popen(["/usr/bin/python","-c","print('hello fuchao')"],stdout=PIPE)
print p.name()
print p.username()
测试:
[root@bogon python_auto]# python popen.py
root
查看应用进程的输出信息
[root@bogon python_auto]# vim popen.py
!/usr/bin/env python
import psutil
from subprocess import PIPE
p = psutil.Popen(["/usr/bin/python","-c","print('hello fuchao')"],stdout=PIPE)
print p.name()
print p.username()
print p.communicate()
测试:
[root@bogon python_auto]# python popen.py
('hello fuchao\n', None)
查看应用进程的cpu运行时间
[root@bogon python_auto]# vim popen.py
!/usr/bin/env python
import psutil
from subprocess import PIPE
p = psutil.Popen(["/usr/bin/python","-c","print('hello fuchao')"],stdout=PIPE)
print p.name()
print p.username()
print p.communicate()
print p.cpu_times()
测试:
[root@bogon python_auto]# python popen.py
pcputimes(user=0.0, system=0.0)
网友评论