-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import psutil | ||
|
||
""" | ||
프로세스 실행 상태 감시 | ||
""" | ||
def checkIfProcessRunning(processName): | ||
for proc in psutil.process_iter(): | ||
try: | ||
if processName.lower() in proc.name().lower(): | ||
return True | ||
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess): | ||
pass | ||
return False; | ||
|
||
""" | ||
프로세스 이름으로 PID 찾기 | ||
""" | ||
def findProcessIdByName(processName): | ||
listOfProcessObjects = [] | ||
for proc in psutil.process_iter(): | ||
try: | ||
pinfo = proc.as_dict(attrs=['pid', 'name', 'username']) | ||
if processName.lower() in pinfo['name'].lower(): | ||
listOfProcessObjects.append(pinfo) | ||
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess): | ||
pass | ||
return listOfProcessObjects; | ||
|
||
""" | ||
감시 대상 프로세스 이름 | ||
""" | ||
process = "java" | ||
|
||
""" | ||
프로세스 상태 확인 | ||
""" | ||
if checkIfProcessRunning(process): | ||
print(f"{process} is running.") | ||
else: | ||
print(f"{process} was down.") | ||
|
||
""" | ||
프로세스 PID, 프로세스 이름, 사용자 | ||
""" | ||
listOfprocessIds = findProcessIdByName(process) | ||
|
||
if len(listOfprocessIds) > 0: | ||
print('Process Exists | PID and other detail are') | ||
for elem in listOfprocessIds: | ||
processID = elem['pid'] | ||
processName = elem['name'] | ||
processUsername = elem['username'] | ||
print(processID, processName, processUsername) | ||
else: | ||
print('No Running Process found with given text') |