psutil执行exe文件并检查启动进程
#!/usr/bin/python# --*-- coding:utf-8 --*--"""@File:fileprocess.py@Author: xianqc@Date:2020/11/17 2020/11/17"""import psutilimport subprocessimport errnoimport osimport timedef startprocess(cmdline):t
·
#!/usr/bin/python
# --*-- coding:utf-8 --*--
"""
@File:fileprocess.py
@Author: xianqc
@Date:2020/11/17 2020/11/17
"""
import psutil
import subprocess
import errno
import os
import time
def startprocess(cmdline):
try:
ps = subprocess.Popen(cmdline, shell=False)
time.sleep(2)
return ps.pid
#except WindowsError as err:
except:
return None
return None
def pid_exists(pid):
"""Check whether pid exists in the current process table.
UNIX only.
"""
if pid < 0:
return False
if pid == 0:
# According to "man 2 kill" PID 0 refers to every process
# in the process group of the calling process.
# On certain systems 0 is a valid PID but we have no way
# to know that in a portable fashion.
raise ValueError('invalid PID 0')
try:
os.kill(pid, 0)
except OSError as err:
if err.errno == errno.ESRCH:
# ESRCH == No such process
return False
elif err.errno == errno.EPERM:
# EPERM clearly means there's a process to deny access to
return True
else:
# According to "man 2 kill" possible error values are
# (EINVAL, EPERM, ESRCH)
raise
else:
return True
def checkprocess(processname):
pl = psutil.pids()
for pid in pl:
try:
if psutil.Process(pid).name() == processname:
return True
except:
continue
return False
def getprocess(processname):
pl = psutil.pids()
for pid in pl:
try:
if psutil.Process(pid).name() == processname:
return pid
except:
continue
return None
def checkfile(filepath):
if not os.path.isfile(filepath):
return False
if os.path.exists(filepath):
return True
return False
if __name__ == '__main__':
print(startprocess('E:\\state\\tools\\blacklist\\UltraISO.exe'))
print(checkprocess('UltraISO.exe'))
print(checkfile('E:\\state\\tools\\blacklist\\UltraISO.exe'))
print(startprocess('E:\\state\\tools\\wormvir\\serotonin.exe'))
print(checkprocess('serotonin.exe'))
print(checkfile('E:\\state\\tools\\wormvir\\serotonin.exe'))
更多推荐
所有评论(0)