我正在使用Ubuntu14.04LTS。我的电池不工作,即它提供了大约5分钟的备份。现在,当我下载东西时,有时需要5-6个小时。我不能在笔记本上呆这么久。所以我想做一个代码,这将检查电池是否每5分钟充电一次,如果没有,它将关闭系统。
发布于 2016-05-03 14:39:35
下面的脚本使用两个对dbus
的调用和一个while循环来轮询百分比。非常简单有效的设置。当您想在笔记本电脑充电后关机时,请运行此命令。
#!/bin/bash
get_percentage()
{
qdbus org.gnome.SettingsDaemon.Power \
/org/gnome/SettingsDaemon/Power \
org.gnome.SettingsDaemon.Power.Percentage
}
shutdown_system()
{
qdbus com.canonical.Unity \
/com/canonical/Unity/Session \
com.canonical.Unity.Session.Shutdown
}
# Basically loop that waits till
# battery reaches 100%. When 100%
# reached , loop exits, and next command
# is executed, which is shutdown
while [ $(get_percentage) -ne 100 ] ;
do
sleep 0.25
done
# Add delay or a warning message here if necessary
shutdown_system
发布于 2016-05-03 12:20:39
试试这个python脚本。它从电池低时自动节省工作借来
#!/usr/bin/env python
import subprocess
import dbus
sys_bus = dbus.SystemBus()
ck_srv = sys_bus.get_object('org.freedesktop.ConsoleKit',
'/org/freedesktop/ConsoleKit/Manager')
ck_iface = dbus.Interface(ck_srv, 'org.freedesktop.ConsoleKit.Manager')
stop_method = ck_iface.get_dbus_method("Stop")
battery_limit = 90 # in percent
def get_battery_percentage():
percentage, err = subprocess.Popen([r'upower -i $(upower -e | grep BAT) | grep --color=never -E percentage | xargs | cut -d ' ' -f2 | sed s/%//
'], shell=True, stdout=subprocess.PIPE).communicate()
return(int(percentage))
while True:
if get_battery_percentage() <= battery_limit:
stop_method()
https://askubuntu.com/questions/766843
复制相似问题