如何在交流电源不可用时自动关机

我的旧笔记本电脑有一个故障的电池,当接通交流电源时显示100%,但拔下插头后,它会在几秒钟内大幅下降到随机百分比,并使机器残酷地关闭。 我害怕破坏加载Ubuntu的SSD(和我的硬盘)。

我想在交流电源不可用时立即关闭电脑。 我在这里搜索并找到了这个 。 但我不明白这个问题的答案,或者至少它与我的情况有关。

请解释一下在拔下交流电源或电源故障时自动关闭笔记本电脑的方法。

介绍

评论中讨论过的脚本是用bash写的,不久前写的。 从那时起,我在Python中使用了几个使用dbus的实用程序函数,对它进行了不同的实现。 抛开所有技术乱码,下面的脚本基本上是那个Python脚本的修改版本。

该过程的关键部分都在main()函数中完成。 其他一系列代码都是实用函数,因此代码可能看起来有些令人生畏,但它实际上并不是,并且没有做任何壮观的事情。 还有一些额外的错误检查,以防万一。

它的工作原理很简单:

  1. 它会在您登录后立即启动。
  2. main中的第一个while循环等待,直到插入AC适配器(在这种特殊情况下有点多余,但以防万一仍然使用)。 如果插入AC适配器,我们转到下一步
  3. 第二个while循环等待直到AC适配器被移除。 一旦它被移除,我们将进入最后一步。
  4. shutdown_system()函数将尝试关闭您的计算机。 如果出现任何问题,您应该看到一个包含错误消息的弹出窗口。

设置脚本

首先,获取脚本源代码并将其保存为文件,最好保存在您的主文件夹中,准确地保存在~/bin中。 如果您的主目录中没有bin/文件夹,请创建一个。

将脚本保存为shutdown_monitor.py并通过在文件管理器中右键单击它或在终端中使用chmod +x ~/bin/shutdown_monitor.py命令确保它是可执行的。

最后,让我们在您登录时自动启动脚本。打开Unity Dash并找到Startup Applications. Add either full path to the command or Startup Applications. Add either full path to the command or python / home / USERNAME / bin / shutdown_monitor.py`作为新命令。

而已 !

脚本

也可以在GitHub上获得

 #!/usr/bin/env python """ Author: Serg Kolo <1047481448@qq.com> Date: Nov 29 , 2016 Purpose:Script for shutting down Ubuntu system if AC adapter is removed Written for:http://askubuntu.com/q/844193/295286 """ import dbus import time import subprocess def get_dbus_property(bus_type, obj, path, iface, prop): """ utility:reads properties defined on specific dbus interface""" if bus_type == "session": bus = dbus.SessionBus() if bus_type == "system": bus = dbus.SystemBus() proxy = bus.get_object(obj, path) aux = 'org.freedesktop.DBus.Properties' props_iface = dbus.Interface(proxy, aux) props = props_iface.Get(iface, prop) return props def get_dbus_method(bus_type, obj, path, interface, method, arg): """ utility: executes dbus method on specific interface""" if bus_type == "session": bus = dbus.SessionBus() if bus_type == "system": bus = dbus.SystemBus() proxy = bus.get_object(obj, path) method = proxy.get_dbus_method(method, interface) if arg: return method(arg) else: return method() def on_ac_power(): adapter = get_adapter_path() call = ['system','org.freedesktop.UPower',adapter, 'org.freedesktop.UPower.Device','Online' ] if get_dbus_property(*call): return True def get_adapter_path(): """ Finds dbus path of the ac adapter device """ call = ['system', 'org.freedesktop.UPower', '/org/freedesktop/UPower','org.freedesktop.UPower', 'EnumerateDevices',None ] devices = get_dbus_method(*call) for dev in devices: call = ['system','org.freedesktop.UPower',dev, 'org.freedesktop.UPower.Device','Type' ] if get_dbus_property(*call) == 1: return dev def shutdown_system(): call = ['session', 'com.canonical.Unity', '/com/canonical/Unity/Session', 'com.canonical.Unity.Session', 'Shutdown',None ] return get_dbus_method(*call) def main(): while not on_ac_power(): time.sleep(1) while on_ac_power(): time.sleep(1) try: shutdown_system() except Exception as e: error_msg = 'Ooops,' + __file__ + 'failed to shutdown your system.' error_msg = error_msg + 'Please show Serg this error so he can fix it:' subprocess.call(['zenity','--error', '--text', error_msg + repr(e) ]) if __name__ == "__main__": main() 

补充说明

如果您发现错误,请报告错误,最好在评论或github上

通过打开终端并使用以下命令在udev中创建新规则:

 gksu gedit /etc/udev/rules.d/50-ac-unplugged.rules 

放入以下行:

 SUBSYSTEM=="power_supply", ENV{POWER_SUPPLY_ONLINE}=="0", RUN+="/sbin/shutdown now" 

保存文件,然后使用以下命令重新启动udev服务:

 sudo udevadm control --reload-rules 

保存所有工作并拔掉电源。