-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpsutils.py
56 lines (47 loc) · 1.42 KB
/
psutils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import glob
import os
class PsUtils:
"""Process utilities, use Python builtin modules and functions, do not
use third-party libs.
"""
def __init__(self):
pass
@staticmethod
def get_process_id_by_name(name):
"""Get process id by name.
Args:
name: process name to check.
Returns:
A list containing process pid.
Raises:
None.
"""
pids = []
for cmdline in glob.glob('/proc/[0-9]*/cmdline'):
if os.path.exists(cmdline):
with open(cmdline) as out:
if name in out.read():
pids.append(int(cmdline.split('/')[2]))
return pids
@staticmethod
def process_is_running(name):
"""Check where given process 'name' is running.
Args:
name: process name to check.
Returns:
True if process is running, otherwise False.
Raises:
None.
"""
pathname = '/proc/[1-9]*/cmdline'
def cmdline_exists():
for _item in glob.glob(pathname):
if os.path.exists(_item):
with open(_item) as out:
if name in out.read():
yield True
else:
yield False
return any(cmdline_exists())