我的旧笔记本有一个错误的电池,显示100%时,在交流电源,但当拔下它急剧下降到随机百分比在几秒钟内,使机器关闭残酷。我害怕损坏我的SSD (和我的硬盘),Ubuntu是在上面的。
我想在没有交流电源的情况下立即关掉电脑。我在这里搜索,发现了这。但我不明白这个问题的答案,或者至少它与我的情况有关。
请解释一种方法,以自动关闭我的笔记本电脑,一旦电源断开,或当有电源故障。
发布于 2016-11-29 01:00:23
通过打开终端并使用以下方法在udev中创建新规则:
gksu gedit /etc/udev/rules.d/50-ac-unplugged.rules(如果您使用的是Ubuntu18.04或更新版本,默认情况下gksu将不可用。在这种情况下,请参考这个问题或使用上面的命令作为sudo -H 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节省你所有的工作,并拔掉电源。
发布于 2016-11-29 07:29:24
注释中讨论过的脚本是在bash和一段时间前编写的。从那时起,我就用Python和使用dbus的几个实用函数实现了它。撇开所有技术上的废话不说,下面的脚本基本上是那个Python脚本的修改版本。
流程的关键部分都是在main()函数中完成的。所有其他代码行都是实用程序函数,因此代码看起来可能有点吓人,但它实际上没有,也没有做任何引人注目的事情。还有一些额外的错误检查,以防万一。
它的工作原理很简单:
首先,获取脚本源代码并将其保存为一个文件,最好保存在您的主文件夹中,确切地说是在~/bin中。如果您的主目录中没有bin/文件夹,请创建一个。
将脚本保存为shutdown_monitor.py,并通过在文件管理器中右键单击它或在终端中使用chmod +x ~/bin/shutdown_monitor.py命令来确保它是可执行的。
最后,让我们让脚本在您登录时自动启动。打开Unity,并找到Startup Applications. Add either full path to the command orpython /home/USERNAME/bin/Shuwatenonor.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
https://askubuntu.com/questions/854570
复制相似问题