用python监控电脑的电量,小于设定值后,并通过飞书机器人通知

  1. 使用pip3安装pustil
    pip3 install pustil
  2. 每隔10分钟检查电脑电量和充电状态
    def check_power_electric():
    
        while True:
            time.sleep(600)  # 每隔10分钟检查一次
            battery = psutil.sensors_battery()
            plugged = battery.power_plugged
            percent = str(battery.percent)
            if plugged == False:
                # 未接入电源线,没有在充电
                plugged = "Not Plugged In"
            else:
                plugged = "Plugged In"
            if int(percent) <= 40 and plugged == "Not Plugged In":
                print(recent)
  3. 使用飞书机器人发送通知
    def send_notify(percent):
        webhook = "替换你的飞书机器人webhook地址"
        headers = {
            "Content-Type": "application/json",
            "Charset": "UTF-8"
        }
        message = {
            "msg_type": "post",
            "content": {
                "post": {
                    "zh_cn": {
                        "title": "测试服务器电量剩余{0}%,请及时充电!!!".format(percent),
                        "content": [
                            [
                                {
                                    "tag": "text",
                                    "text": ""
                                },
                            ]
                        ]
                    }
                }
            }
        }
        requests.post(url=webhook, json=message, headers=headers)
  4. 当电量低于40%时且不在充电时,发送通知。修改第2步的代码
     if int(percent) <= 40 and plugged == "Not Plugged In":
           send_notify(percent)
  5. 启动监控进程
    if __name__ == '__main__':
        t = Thread(target=check_power_electric)
        t.start()
  6. 完整代码如下:
    import psutil
    import time
    import requests
    from threading import Thread
    
    def check_power_electric():
    
        while True:
            time.sleep(600)  # 每隔10分钟检查一次
            battery = psutil.sensors_battery()
            plugged = battery.power_plugged
            percent = str(battery.percent)
            if plugged == False:
                # 未接入电源线
                plugged = "Not Plugged In"
            else:
                plugged = "Plugged In"
            if int(percent) <= 40 and plugged == "Not Plugged In":
                send_notify(percent)
    
    def send_notify(percent):
        webhook = "替换你的飞书机器人webhook地址"
        headers = {
            "Content-Type": "application/json",
            "Charset": "UTF-8"
        }
        message = {
            "msg_type": "post",
            "content": {
                "post": {
                    "zh_cn": {
                        "title": "测试服务器电量剩余{0}%,请及时充电!!!".format(percent),
                        "content": [
                            [
                                {
                                    "tag": "text",
                                    "text": ""
                                },
                            ]
                        ]
                    }
                }
            }
        }
        requests.post(url=webhook, json=message, headers=headers)
    
    
    if __name__ == '__main__':
        t = Thread(target=check_power_electric)
        t.start()
  7. 使用nohup和&启动
    #在iterm窗口执行(在check_power.py文件同一目录下执行)
    #启动
    nohup python3 check_power.py &
    
    #查看是否成功
    ps -ef|grep check_power.py
    
    #kill进程,退出监控
    kill -9 PID
    

      

   至此结束,希望对你有点帮助

   每天进步一点点~

    

posted @ 2022-07-21 15:56  二十一笔画  阅读(5)  评论(0)    收藏  举报