python监控主机信息,重启浏览器

该脚本用于实时监控主机内存使用情况和网络收发,并列出所有进程中与'chrome.exe'相关的信息。当进程运行时间超过一小时或未运行时,脚本将发送信号关闭这些进程。此外,它还会打开一个本地网页。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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')

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值