centos7.2 get pid by process name with python3.6

前端之家收集整理的这篇文章主要介绍了centos7.2 get pid by process name with python3.6前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

centos7.2 get pid by process name with python3.6


#-*- encoding:UTF-8 -*-
import os
import sys
import string
import psutil
import re

def get_pid(name):
process_list = psutil.pids()
pid = 0
for i in range(0,len(process_list)):
p = psutil.Process(process_list[i])
if len(p.cmdline())>=2:
process_info = str(p.cmdline()[1])
result = process_info.find(name)
if result != -1:
pid = process_list[i]
break
print(pid)
return pid

def main(argv):
name = argv[1]
#print(name)
get_pid(name)

if __name__ == "__main__":
main(sys.argv)


关于:psutil 5.2.2使用说明参见:

https://pypi.python.org/pypi/psutil/5.2.2


Example usages

cpu

>>> import psutil
>>> psutil.cpu_times()
scputimes(user=3961.46, nice169.729, system2150.659, idle16900.540, iowait629.59, irq0.0, softirq19.42, stealguest=0,112)">0.0)
>>>
>>> for x in range(3):
...     psutil.cpu_percent(interval1)
...
4.0
5.9
3.8
1, percpu=True)
...
[4.0, 6.9,112)">3.7,112)">9.2]
[7.0,112)">8.5,112)">2.4,112)">2.1]
[1.2,112)">9.0,112)">9.9,112)">7.2]
.cpu_times_percent(intervalFalse)
...
scputimes(user1.5,112)">0.5,112)">96.5, guest_nice0.0)
scputimes(user1.0,112)">99.0,112)">2.0,112)">98.0,102)">.cpu_count()
4
.cpu_count(logicalFalse)
2
.cpu_stats()
scpustats(ctx_switches20455687, interrupts6598984, soft_interrupts2134212, syscalls0)
.cpu_freq()
scpufreq(current931.42925, min800.0,32)">max3500.0)
>>>
Memory
.virtual_memory()
svmem(total10367352832, available6472179712, percent37.6, used8186245120, free2181107712, active4748992512, inactive2758115328, buffers790724608, cached3500347392, shared787554304)
.swap_memory()
sswap(total2097147904,112)">296128512,112)">1801019392,112)">14.1, sin304193536, sout677842944)
>>>
Disks
.disk_partitions()
[sdiskpart(device='/dev/sda1', mountpoint'/', fstype'ext4', opts'rw,nosuid'),
 sdiskpart(device'/dev/sda2',160)">'/home',160)">'ext,opts='rw')]
.disk_usage('/')
sdiskusage(total21378641920,112)">4809781248,112)">15482871808,112)">22.5)
.disk_io_counters(perdiskFalse)
sdiskio(read_count719566, write_count1082197, read_bytes18626220032, write_bytes24081764352, read_time5023392, write_time63199568, read_merged_count619166, write_merged_count812396, busy_time4523412)
>>>
Network
.net_io_counters(pernicTrue)
{'eth0': netio(bytes_sent485291293, bytes_recv6004858642, packets_sent3251564, packets_recv4787798, errinerroutdropindropout0),
 'lo': netio(bytes_sent2838627,112)">30567,112)">0)}
.net_connections()
[pconn(fd115, family=<AddressFamily.AF_INET: 2>,32)">type=<SocketType.SOCK_STREAM: 1laddr=('10.0.0.1', 48776), raddr'93.186.135.91',112)">80), status'ESTABLISHED', pid1254),
 pconn(fd117,112)">43761),160)">'72.14.234.100',160)">'CLOSING',112)">2987),102)">=-60759),160)">'72.14.234.104',32)">None),112)">51314),160)">'72.14.234.83',112)">443),160)">'SYN_SENT',32)">None)
 ...]
.net_if_addrs()
{'lo': [snic(familyaddress'127.0.0.1', netmask'255.0.0.0', broadcastptpsnic(family.AF_INET6: 10'::1',160)">'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff',32)">None,102)">.AF_LINK: 17'00:00:00:00:00:00',32)">None)],160)">'wlan0': [snic(family'192.168.1.3',160)">'255.255.255.0',160)">'192.168.1.255',
           snic(family'fe80::c685:8ff:fe45:641%wlan0',160)">'ffff:ffff:ffff:ffff::',160)">'c4:85:08:45:06:41',160)">'ff:ff:ff:ff:ff:ff',32)">None)]}
.net_if_stats()
{'eth0': snicstats(isupTrue, duplex=<NicDuplex.NIC_DUPLEX_FULL: speed100, mtu1500),160)">'lo': snicstats(isup.NIC_DUPLEX_UNKNOWN: 065536)}
>>>
Sensors
.sensors_temperatures()
{'acpitz': [shwtemp(label'', current47.0, high103.0, critical103.0)],160)">'asus': [shwtemp(label'coretemp': [shwtemp(label'Physical id 0',112)">52.0,112)">100.0,112)">100.0),
              shwtemp(label'Core 0',112)">45.0,160)">'Core 1',160)">'Core 2',160)">'Core 3',112)">100.0)]}
.sensors_fans()
{'asus': [sfan(label'cpu_fan',112)">3200)]}
.sensors_battery()
sbattery(percent93, secsleft16628, power_plugged>>>
Other system info
.users()
[user(name'giampaolo', terminal'pts/2', host'localhost', started1340737536.0),
 user(name'pts/3',112)">1340737792.0)]
.boot_time()
1365519115.0
>>>
Process management
.pids()
[2,112)">3,112)">4,112)">5,112)">6,112)">7,112)">46,112)">48,112)">50,112)">51,112)">178,112)">182,112)">222,112)">223,112)">224,112)">268,112)">1215,112)">1216,112)">1220,112)">1221,112)">1243,112)">1244,
 1301,112)">1601,112)">2237,112)">2355,112)">2637,112)">2774,112)">3932,112)">4176,112)">4177,112)">4185,112)">4187,112)">4189,112)">4225,112)">4243,112)">4245,112)">4263,112)">4282,112)">4306,112)">4311,112)">4312,112)">4313,112)">4314,112)">4337,112)">4339,112)">4357,112)">4358,112)">4363,112)">4383,112)">4395,112)">4408,112)">4433,112)">4443,112)">4445,112)">4446,112)">5167,112)">5234,112)">5235,112)">5252,112)">5318,112)">5424,112)">5644,112)">6987,112)">7054,112)">7055,112)">7071]
>>> p = psutil.Process(7055)
>>> p.name()
'python'
.exe()
'/usr/bin/python'
.cwd()
'/home/giampaolo'
.cmdline()
['/usr/bin/python', 'main.py']
.pid
7055
.ppid()
7054
.parent()
<psutil.Process(pidname'bash') at 140008329539408>
.children()
[8031,160)">'python') at 140208324519778044,112)">19229444921932>]
.status()
'running'
.username()
'giampaolo'
.create_time()
1267551141.5019531
.terminal()
'/dev/pts/0'
.uids()
puids(real1000, effectivesaved1000)
.gids()
pgids(real.cpu_times()
pcputimes(user1.02,112)">0.31, children_user0.32, children_system0.1)
1.0)
12.1
.cpu_affinity()
[3]
.cpu_affinity([1])  # set
.cpu_num()
1
.memory_info()
pmem(RSS10915840, vms67608576,112)">3313664, text2310144, libdata7262208, dirty.memory_full_info()  # "real" USS memory usage (Linux,OSX,Win only)
pfullmem(RSS10199040,112)">52133888,112)">3887104,112)">2867200,112)">5967872, uss6545408, pss6872064, swap.memory_percent()
0.7823
.memory_maps()
[pmmap_grouped(path'/lib/x8664-linux-gnu/libutil-2.15.so', RSS32768, size2125824, shared_cleanshared_dirtyprivate_clean20480, private_dirty12288, referencedanonymouspmmap_grouped(path'/lib/x8664-linux-gnu/libc-2.15.so',112)">3821568,112)">3842048,112)">3575808,160)">'/lib/x8664-linux-gnu/libcrypto.so.0.1',112)">34124,112)">2134016,112)">15360,112)">24576,112)">8192,160)">'[heap]',  RSS139264,160)">'[stack]',112)">2465792,112)">2494464,112)">2277376,102)">.io_counters()
pio(read_count478001,112)">59371,112)">700416,112)">69632, read_chars456232, write_chars517543)
.open_files()
[popenfile(path'/home/giampaolo/svn/psutil/setup.py', fdpositionmode'r', flags32768),
 popenfile(path'/var/log/monitd',112)">235542,160)">'a',112)">33793)]
.connections()
[pconn(fd'ESTABLISHED'),160)">'CLOSING'),112)">119,112)">123,160)">'SYN_SENT')]
.num_threads()
.num_fds()
8
.threads()
[pthread(iduser_time22.5, system_time9.2891),
 pthread(0.0),112)">5236,112)">5237,112)">0.0707,112)">1.1)]
.num_ctx_switches()
pctxsw(voluntary78, involuntary19)
.nice()
0
.nice(10)  .ionice(psutil.IOPRIO_CLASS_IDLE)  # IO priority (Win and Linux only)
.ionice()
pionice(ioclass=<IOPriority.IOPRIO_CLASS_IDLE: 3value.rlimit(psutil.RLIMIT_NOFILE, (5))  # set resource limits (Linux only)
.RLIMIT_NOFILE)
(5)
.environ()
{'LC_PAPER': 'it_IT.UTF-8',160)">'SHELL': '/bin/bash',160)">'GREP_OPTIONS': '--color=auto',
'XDG_CONFIG_DIRS': '/etc/xdg/xdg-ubuntu:/usr/share/upstart/xdg:/etc/xdg',160)">'COLORTERM': 'gnome-terminal',102)">...}
.as_dict()
{'status': 'running',160)">'num_ctx_switches': pctxsw(voluntary63,112)">1),160)">'pid': 5457, .is_running()
True
.suspend()
.resume()
.terminate()
.wait(timeout3)
.test()
USER         PID %cpu %MEM     VSZ     RSS TTY        START    TIME  COMMAND
root           1  0.0  0.0   24584    2240            Jun17   00:00  init
root           2  0.0       0       0            Jun17   00  kthreadd
root           3  05  ksoftirqd/...
giampaolo  31475  20760    3024 /dev/pts0 Jun19   00  python2.4
giampaolo  31721  2.2  773060  181896            04   10:30  chrome
root       31763  0            05   00  kworker0:>>>
Further process APIs
for p in psutil.process_iter():
...     print(p)
...
psutil'init')
psutil'kthreadd')
psutil'ksoftirqd/0')
...
def on_terminate(proc):
print("process {} terminated".format(proc))
>>> # waits for multiple processes to terminate
>>> gone, alive .wait_procs(procs_list, timeoutcallback=on_terminate)
>>>
Windows services
>>> list(psutil.win_service_iter())
[<WindowsService(name'AeLookupSvc', display_name'Application Experience') at 38850096'ALG',160)">'Application Layer Gateway Service') at 38850128'APNMCP',160)">'Ask Update Service') at 38850160'AppIDSvc',160)">'Application Identity') at 38850192>>> s .win_service_get('alg')
>>> s'binpath': 'C:\\WindowsSystem32alg.exe',160)">'description': 'Provides support for 3rd party protocol plug-ins for Internet Connection Sharing',160)">'display_name': 'Application Layer Gateway Service',160)">'name': 'alg',160)">'pid': 'start_type': 'manual',160)">'stopped',160)">'username': 'NT AUTHORITYLocalService'}

猜你在找的CentOS相关文章