超时后暂停盖子关闭
我使用的是Ubuntu 16.04笔记本电脑,Gnome作为桌面环境,LightDM作为登录管理器。
我想要做的是在盖子关闭30秒后暂停电脑。 这可能吗?
盖子关闭后切换到暂停30秒
禁用默认的近盖操作
要设置特定时间,在计算机切换到暂停之前,我们需要禁用关闭盖子的默认操作。 这可以通过以下命令完成:
gsettings set org.gnome.settings-daemon.plugins.power lid-close-ac-action "nothing"
和
gsettings set org.gnome.settings-daemon.plugins.power lid-close-battery-action "nothing"
关闭盖子的自定义动作
随后,我们需要设置关闭盖子的替代操作,包括在暂停前30秒的中断。 我们可以将它们与现有事件挂钩。
问题是,这样做的好处是否会影响所需的更复杂的设置。 像下面这样的脚本版本在处理器和内存中几乎都消耗零,即使你运行了其中的10个。
脚本选项的优点是它易于恢复; 只是不再运行它并恢复原始(或不同)设置。
剧本
#!/usr/bin/env python3 import time import subprocess # set delay time below (seconds) delay = 30 # set path to lid status file f = "/proc/acpi/button/lid/LID/state" # --- set close command below close_command = ["systemctl", "suspend"] def get_state(): return "open" in open(f).read() state1 = get_state() while True: time.sleep(3) state2 = get_state() if state2 != state1: t = 0 while not get_state(): time.sleep(1); t = t+1 if t > delay: subprocess.Popen(close_command) break state1 = state2
如何使用
-
如上所述,首先禁用默认的lid-close-actions:
gsettings set org.gnome.settings-daemon.plugins.power lid-close-ac-action "nothing"
和
gsettings set org.gnome.settings-daemon.plugins.power lid-close-battery-action "nothing"
-
将脚本复制到空文件中,将其另存为
delay_lidsuspend.py
-
在脚本的head部分中,设置了一些默认值和路径。 最有可能的是,您不需要更改任何内容:
# set delay time below (seconds) delay = 30 # set path to lid status file f = "/proc/acpi/button/lid/LID/state" # --- set close command below close_command = ["systemctl", "suspend"]
我不确定你的盖子状态文件(“/ proc / acpi / button / lid / LID / state”)的路径在每台笔记本电脑上都是一样的。 使用命令测试:
cat /proc/acpi/button/lid/LID/state
-
通过从终端运行来测试脚本:
python3 /path/to/delay_lidsuspend.py
并关闭盖子,看它是否按预期工作(这里完美地完成了工作)。
-
如果一切正常,请将其添加到启动应用程序。