import time
import psutil
import os
import signal
import webbrowser
def runtask():
free = str(round(psutil.virtual_memory().free / (1024.0 * 1024.0 * 1024.0), 2))
total = str(round(psutil.virtual_memory().total / (1024.0 * 1024.0 * 1024.0), 2))
percent = int(psutil.virtual_memory().total - psutil.virtual_memory().free) / float(psutil.virtual_memory().total)
net = psutil.net_io_counters()
bytes_sent = '{0:.2f} Mb'.format(net.bytes_recv / 1024 / 1024)
bytes_rcvd = '{0:.2f} Mb'.format(net.bytes_sent / 1024 / 1024)
time_now = int(time.time()) - 15 # 获取当前系统时间(一个小时之前的时间)
msg = '''
-------------------------- Host Msg-------------------------
Total_Memory:{0}G,
Left_Memory:{1}G,
Memory_Percent:{2}%,
Net Receive:{3},
Net Send:{4}'''.format(total, free, int(percent * 100), bytes_sent, bytes_rcvd)
print(msg)
pids = psutil.pids()
for pid in pids:
p = psutil.Process(pid)
pid_name = p.name()
if 'chrome.exe' == pid_name:
# cmdline = p.cmdline()
# percent = p.cpu_percent()
pid_stats = p.status()
time_stamp = p.create_time()
dt = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time_stamp))
t_num = p.num_threads()
info = '''
-------------------------- Process Msg --------------------------
P_Name:{0},
P_ID:{1}
P_Status:{2},
P_CreateTime:{3},
P_Percent:{4}
P_Thread_num:{5}
'''.format(pid_name, pid, pid_stats, dt, percent, t_num)
print(info)
is_running = p.is_running()
if is_running is not True:
os.kill(pid, signal.SIGINT)
if time_now > time_stamp:
os.kill(pid, signal.SIGINT)
print('杀死老进程', dt, time_stamp, time_now, time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time_now)))
if __name__ == '__main__':
runtask()
webbrowser.open('file:///D:/Users/BlackHole/Desktop/hello.html')