简易网页版进程管理器(支持手机管理电脑进程)

简易网页版进程管理器(支持手机管理电脑进程)大家好 我是小小明 今天要带大家做一款简易的网页版进程管理器 最终效果如下 目标只要求能查看内存使用何 cpu 使用率即可 基础模块技术测试读取进程信息首先 我们可以使用 psutil 读取服务端的进程使用情况 包括内存和 CPU importpsutil psutil cpu count infos forprocinpsu process iter attrs memory info name pid info proc in

大家好,我是小小明,今天要带大家做一款简易的网页版进程管理器,最终效果如下:

image-20210710003049714

目标只要求能查看内存使用何cpu使用率即可。

基础模块技术测试

读取进程信息

首先,我们可以使用psutil读取服务端的进程使用情况(包括内存和CPU):

import psutil n = psutil.cpu_count() infos = [] for proc in psutil.process_iter(attrs=['memory_info', 'name', 'pid']): info = proc.info memory_info = info['memory_info'].rss / 1024 info['memory_info'] = memory_info cpu_percent = proc.cpu_percent(0) / n info['cpu_percent'] = f"{ 
     cpu_percent:.2f}%" infos.append(info) 

然后我们可以按照内存使用量对数据进行降序排序:

infos.sort(key=lambda info: info['memory_info'], reverse=True) 

然后可以对内存数据进行格式化(当然这步也可以交给游览器js来处理):

for info in infos: memory_info = info['memory_info'] if memory_info < 1024: memory_info = f"{ 
     memory_info :.2f}KB" elif memory_info < 1024 * 1024: memory_info = f"{ 
     memory_info / 1024:.2f}MB" else: memory_info = f"{ 
     memory_info / 1024 / 1024:.2f}GB" info['memory_info'] = memory_info return infos 

杀掉某个进程

为了干掉某个进程,我们使用如下方法:

# 杀掉进程树 def kill_proc_tree(pid, sig=signal.SIGTERM, include_parent=True, timeout=None, on_terminate=None): if pid == os.getpid(): raise RuntimeError("I refuse to kill myself") parent = psutil.Process(pid) children = parent.children(recursive=True) if include_parent: children.append(parent) for p in children: p.send_signal(sig) gone, alive = psutil.wait_procs(children, timeout=timeout, callback=on_terminate) return (gone, alive) 

也可以调用系统命令:

def execute_cmd_command(PID): os.system("ntsd -c q -p PID".format(PID)) 

网页开发

基础前端模板

这次我们计划使用flask来开发一个小网站,所以使用jinja2模板的语法。

先做一个简单的前端页面(文件名show.html):

 
    DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>进程列表 
      title> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css">  
       head> <body> <div class="container"> <h1>进程列表 
        h1> <table class="table"> <thead> <tr> <th scope="col">排名 
         th> <th scope="col">pid 
          th> <th scope="col">name 
           th> <th scope="col">memory_info 
            th> <th scope="col">CPU使用率 
             th> <th scope="col">结束 
              th>  
               tr>  
                thead> <tbody> {% for row in data %} <tr> <td>{ 
              { loop.index }} 
                 td> <td>{ 
               { row['pid'] }} 
                  td> <td>{ 
                { row['name'] }} 
                   td> <td>{ 
                 { row['memory_info'] }} 
                    td> <td>{ 
                  { row['cpu_percent'] }} 
                     td> <td><a href='/kill_proL/{ 
                     { row['pid'] }}'>结束进程 
                      a> 
                       td>  
                        tr> {% endfor %}  
                         tbody>  
                          table>  
                           div>  
                            body>  
                             html> 

jinja2模板语法的规则可以百度。

完善后端代码:

""" 小小明的代码 CSDN主页:https://blog.csdn.net/as """ __author__ = '小小明' __time__ = '2021/7/9 22:21' import psutil from flask import Flask, redirect, render_template import os import signal app = Flask(__name__) # 获取系统进程列表 def get_procs(): infos = [] n = psutil.cpu_count() for proc in psutil.process_iter(attrs=['memory_info', 'name', 'pid']): info = proc.info memory_info = info['memory_info'].rss / 1024 info['memory_info'] = memory_info cpu_percent = proc.cpu_percent(0) / n info['cpu_percent'] = f"{ 
     cpu_percent:.2f}%" infos.append(info) infos.sort(key=lambda info: info['memory_info'], reverse=True) for info in infos: memory_info = info['memory_info'] if memory_info < 1024: memory_info = f"{ 
     memory_info :.2f}KB" elif memory_info < 1024 * 1024: memory_info = f"{ 
     memory_info / 1024:.2f}MB" else: memory_info = f"{ 
     memory_info / 1024 / 1024:.2f}GB" info['memory_info'] = memory_info return infos # 杀掉进程树 def kill_proc_tree(pid, sig=signal.SIGTERM, include_parent=True, timeout=None, on_terminate=None): if pid == os.getpid(): raise RuntimeError("I refuse to kill myself") parent = psutil.Process(pid) children = parent.children(recursive=True) if include_parent: children.append(parent) for p in children: p.send_signal(sig) gone, alive = psutil.wait_procs(children, timeout=timeout, callback=on_terminate) return (gone, alive) def execute_cmd_command(PID): os.system("ntsd -c q -p PID".format(PID)) @app.route('/kill_proL/ 
   
     ' 
   , methods=['GET']) def kill_proL(pid): try: kill_proc_tree(int(pid)) # execute_cmd_command(pid) finally: return redirect("/") # 重新加载页面 @app.route('/') def Show_html(): data = get_procs() return render_template('show.html', data=data) if __name__ == "__main__": app.run(host="0.0.0.0", port=8888) 

注意:show.html需要方法上述py脚本的同级templates目录下。

咱们运行看看效果:

image-20210710005140057

比较粗糙但是可以用。

升级版前端开发

这次我们计划给表头增加排序和筛选的功能。

经过一番开发,编译出了仿Excel的筛选功能的JavaScript脚本和css样式表:

image-20210710005401112

对于index.html模板的代码为:

<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>服务端进程列表</title> <link href="../static/css/bootstrap.min.css" rel="stylesheet"> <link rel="stylesheet" type="text/css" href="../static/css/demo.css"> <link rel="stylesheet" href="../static/dist/excel-bootstrap-table-filter-style.css"/> </head> <body> <div class="container"> <div class="row"> <div class="col-md-12"> <h2>进程列表</h2> <table id="table" class="table table-bordered table-intel"> <thead> <tr> <th class="filter">排名</th> <th class="filter">pid</th> <th class="filter">name</th> <th class="filter">memory_info</th> <th class="no-filter">CPU使用率</th> <th class="no-sort no-filter">结束</th> </tr> </thead> <tbody> { 
   % for row in data %} <tr> <td>{ 
   { 
    loop.index }}</td> <td>{ 
   { 
    row['pid'] }}</td> <td>{ 
   { 
    row['name'] }}</td> <td>{ 
   { 
    row['memory_info'] }}</td> <td>{ 
   { 
    row['cpu_percent'] }}</td> <td><a href='/kill_proL/{ 
   { row['pid'] }}'>结束进程</a></td> </tr> { 
   % endfor %} </tbody> </table> </div> </div> </div> <script type="text/javascript" src="../static/js/jquery-1.11.0.min.js"></script> <script type="text/javascript" src="../static/dist/excel-bootstrap-table-filter-bundle.js"></script> <script type="text/javascript"> $(function () { 
    $('#table').excelTableFilter({ 
    'captions': { 
   a_to_z: '升序排列', z_to_a: '降序排列', search: '搜索', select_all: '全部选择'} }); $('#table2').excelTableFilter({ 
    'captions': { 
   a_to_z: '升序排列', z_to_a: '降序排列', search: '搜索', select_all: '全部选择'} }); $('#table3').excelTableFilter({ 
    'captions': { 
   a_to_z: '升序排列', z_to_a: '降序排列', search: '搜索', select_all: '全部选择'} }); }); </script> </body> </html> 

于是就可以在前端按CPU使用率来排序了:

image-20210710005728811

还可以筛选筛选包含指定名称的进程:

image-20210710005828082

测试结束进程可以顺利的关闭服务器上面的金山词霸、钉钉等非系统进程。

至此我们就成功的完成了网页版进程管理器的开发。

前端代码获取方式

本文评论数过20并且阅读量达到1000以上,或者将本文转发到自己的朋友圈,可向作者索要本文全套代码的下载地址。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/211429.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月18日 下午10:27
下一篇 2026年3月18日 下午10:27


相关推荐

  • 访问数据库使用redis作为mysql的缓存(redis和mysql结合)

    访问数据库使用redis作为mysql的缓存(redis和mysql结合)首先声明一下,我是在一个SSM项目的基础上进行优化的,所以就不进行基础的介绍了。下面我也补充一些知识点:redis:内存型数据库,有持久化功能,具备分布式特性,可靠性高,适用于对读写效率要求都很高,数据处理业务复杂和对安全性要求较高的系统(如新浪微博的计数和微博发布部分系统,对数据安全性、读写要求都很高)。缓存机制说明:所有的查询结果都放进了缓存,也就是把MySQL查询的结果放…

    2022年6月17日
    38
  • Mysql锁死解决「建议收藏」

    Mysql锁死解决「建议收藏」经常遇到mysql锁死,如alterxx语句就经常锁死数据表怎么解决?1.showprocesslist;可以看到有Waitingfortablemetadatalock字眼;如果有其他类似字眼可以解决掉冲突的进程命令:killpidpid就是图片第一列的id,如果还是无法解决且看第二条2.select*frominformation_schema.innodb_trx;此条即查到未结束的事务,可以酌情杀死冲突事务一般都可解决锁死。如未解决可谨慎

    2022年5月18日
    55
  • python进阶(21)typing模块–类型提示支持[通俗易懂]

    python进阶(21)typing模块–类型提示支持[通俗易懂]typing介绍Python是一门弱类型的语言,很多时候我们可能不清楚函数参数的类型或者返回值的类型,这样会导致我们在写完代码一段时间后回过头再看代码,忘记了自己写的函数需要传什么类型的参数,返回什

    2022年7月31日
    8
  • IaaS、PaaS和SaaS的区别

    IaaS、PaaS和SaaS的区别IaaS PaaS 和 SaaS 到底是什么 本文就用最通俗的语言来说透 这些高大上的概念到底是什么 云服务 现在已经快成了一个家喻户晓的词了 如果你不知道 PaaS IaaS 和 SaaS 的区别 那么也没啥 因为很多人确实不知道 云 其实是互联网的一个隐喻 云计算 其实就是使用互联网来接入存储或者运行在远程服务器端的应用 数据 或者服务 任何一个使用基于互联网的方法任何一个使用基于互

    2026年3月19日
    2
  • gridbagconstraints什么意思_java rectangle

    gridbagconstraints什么意思_java rectangle说明:GridBagLayout只有一个无参的构造器,要使用它就必须用setConstraints(Componentcomp,GridBagConstraintsconstraints)将它和GridBagConstraints关联起来!当GridBagLayout与无参的GridBagConstraints关联时,此时它就相当于一个GridLayout,只不过,用GridLayout布局的

    2025年10月15日
    5
  • MOS开关电路_场效应管开关电路 实例

    MOS开关电路_场效应管开关电路 实例1mos管和三极管是构成芯片的基础元件,分立和mos,三极管在我们平常的电路中也是用的最多的。mos和电压控制型元件,而三极管是电流控制型元件。分别在mos的GS和三极管的IB给到一定的电压和电流,在mos的DS和三极管的CE就会有相应的电压变化。根据这个变化可以做成放大电路和开关电路,开关电路即放大电路的状态达到饱和状态。今天分享MOS的两个开关电路2.电平转换电路这个电路是双向电平转换电路分析:当uc_io为低电平时,M1导通,V2的电流流过R2,经过M1的DS到uc_io的低电平,从而实

    2026年1月28日
    8

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号