防止机器进入睡眠状态

斯科特遣散费

我最近从14.04 Trusty升级到16.04 Xenial。升级之前,我使用caffeine-plus指示器图标告诉笔记本电脑何时可以进入睡眠状态。我通常使用的模式是启用咖啡因,因此计算机只有在机盖合上后才能进入睡眠/暂停状态。但是,有时候我希望能够让空闲计时器发挥预期的作用。

自升级以来,咖啡因似乎不再有任何作用。我可以让计算机长时间运行,故意打开机盖,只好回来找它睡觉,并且过程没有完成。

如何恢复以前的行为?请注意,我要的是切换,而不是永久的更改。作为切换,它应该在视觉上指示其是否已启用。指示器图标会很棒。

笔记

在提出此问题之前,我进行了以下搜索:a)关于如何使用咖啡因的过时(过时)帖子,或b)永久禁用睡眠以解决各种硬件错误。我的问题只是关于恢复我在14.04中拥有的功能,而这个主题我没有找到解决。

斯科特遣散费

编辑

经过一些工作,我写了一个比下面更完整,更易于使用的解决方案。您可以在GitHub上下载该程序您还需要安装依赖项:

sudo apt install xdotool xprintidle

原始答案

在Jacob Vlijm向我指出了部分解决方案之后,我将他的脚本的修改版本与部分Caffeine和我自己的一些代码组合在一起,并提出了Caffeine的替代品。

指示

  1. 确保安装了必要的软件包。注意:caffeine-plus仅用于图标。如果您不关心适当的图标,则不需要它。

    sudo apt install caffeine-plus xprintidle xdotool
    
  2. 将以下脚本保存在某个地方,使其可执行。

    #!/usr/bin/python3
    # coding=utf-8
    #
    # Copyright © 2016 Scott Severance
    # Code mixed in from Caffeine Plus and Jacob Vlijm
    #
    # This program is free software: you can redistribute it and/or modify
    # it under the terms of the GNU General Public License as published by
    # the Free Software Foundation, either version 3 of the License, or
    # (at your option) any later version.
    #
    # This program is distributed in the hope that it will be useful,
    # but WITHOUT ANY WARRANTY; without even the implied warranty of
    # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    # GNU General Public License for more details.
    #
    # You should have received a copy of the GNU General Public License
    # along with this program.  If not, see <http://www.gnu.org/licenses/>.
    
    import argparse
    import os
    import signal
    import time
    from subprocess import Popen, check_output, call
    import gi
    gi.require_version('Gtk', '3.0')
    gi.require_version('AppIndicator3', '0.1')
    from gi.repository import GObject, Gtk, AppIndicator3
    
    class SleepInhibit(GObject.GObject):
    
        def __init__(self):
            GObject.GObject.__init__(self)
            self.inhibited = False
            self._add_indicator()
            self.inhibit_proc = None
    
        def _add_indicator(self):
            self.AppInd = AppIndicator3.Indicator.new("sleep-inhibit-off",
                                                      "sleep-inhibit",
                                                      AppIndicator3.IndicatorCategory.APPLICATION_STATUS)
            self.AppInd.set_status(AppIndicator3.IndicatorStatus.ACTIVE)
            self.AppInd.set_icon ("caffeine-cup-empty")
    
            self._build_indicator_menu(self.AppInd)
    
        def _build_indicator_menu(self, indicator):
            menu = Gtk.Menu()
    
            menu_item = Gtk.MenuItem("Toggle Sleep Inhibition")
            menu.append(menu_item)
            menu_item.connect("activate", self.on_toggle)
            menu_item.show()
    
            menu_item = Gtk.MenuItem("Quit")
            menu.append(menu_item)
            menu_item.connect("activate", self.on_quit)
            menu_item.show()
    
            indicator.set_menu(menu)
    
        def on_toggle(self, menuitem):
            self.inhibited = not self.inhibited
            self.flip_switch()
    
        def on_quit(self, menuitem):
            if self.inhibit_proc:
                self.kill_inhibit_proc()
            exit(0)
    
        def _set_icon_disabled(self):
            self.AppInd.set_icon('caffeine-cup-empty')
    
        def _set_icon_enabled(self):
            self.AppInd.set_icon('caffeine-cup-full')
    
        def flip_switch(self):
            if self.inhibited:
                self._set_icon_enabled()
                self.inhibit_proc = Popen([__file__, "--mode=inhibit-process"])
            else:
                self.kill_inhibit_proc()
                self._set_icon_disabled()
    
        def kill_inhibit_proc(self):
            self.inhibit_proc.terminate()
            self.inhibit_proc.wait()
            self.inhibit_proc = None
    
    def inhibit():
        seconds = 120 # number of seconds to start preventing blank screen / suspend
        while True:
            try:
                curr_idle = check_output(["xprintidle"]).decode("utf-8").strip()
                if int(curr_idle) > seconds*1000:
                    call(["xdotool", "key", "Control_L"])
                time.sleep(10)
            except FileNotFoundError:
                exit('ERROR: This program depends on xprintidle and xdotool being installed.')
            except KeyboardInterrupt:
                exit(0)
    
    def parse_args():
        parser = argparse.ArgumentParser(description='''Indicator to prevent
            computer from sleeping. It depends on the commands xprintidle and
            xdotool being properly installed on your system. If they aren't
            installed already, please install them. Also, the icons are taken from
            caffeine-plus, so if it isn't installed, you will probably see a broken
            icon.''')
        mode = '''The mode can be either indicator (default) or inhibit-process. If
            mode is indicator, then an indicator icon is created. inhibit-process is
            to be called by the indicator. When sleep is inhibited, it runs,
            preventing sleep.'''
        parser.add_argument('--mode', type=str, default='indicator', help=mode)
        return parser.parse_args()
    
    def main():
        args = parse_args()
        if args.mode == 'indicator':
            signal.signal(signal.SIGINT, signal.SIG_DFL)
            GObject.threads_init()
            SleepInhibit()
            Gtk.main()
        elif args.mode == 'inhibit-process':
            inhibit()
        else:
            exit('ERROR: Invalid value for --mode!')
    
    if __name__ == '__main__':
        main()
    
  3. 运行脚本。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何防止硬盘进入睡眠状态?

无法通过pm-suspend使机器进入睡眠状态

如何防止Android设备以编程方式进入睡眠状态?

如何防止Azure网站进入睡眠状态?

如何防止Ubuntu使显示器进入睡眠状态?

关于让inotify线程进入睡眠状态

使主线程进入睡眠状态

Ubuntu的日志进入睡眠状态并醒来?

当heroku使我的应用进入睡眠状态时,如何防止ClearDB(MySQL)掉线

我可以修改液晶显示器以防止其进入睡眠状态吗?

如何防止Android设备从Qt应用程序进入睡眠状态

如何防止Android设备进入睡眠状态(通过adb命令外壳)

如何防止计算机自动进入睡眠和/或休眠状态?

当我的C ++应用程序运行时,如何防止Windows进入睡眠状态?

Microsoft Bot Framework-Bot进入睡眠状态。有办法防止吗?

MoUsoCoreWorker.exe是否有防止Windows 10进入睡眠状态的修复程序?

键盘可防止PC进入睡眠状态;如何调试/解决?

防止计算机在程序运行时进入睡眠/待机/休眠状态

当VMWare Fusion中运行VM时如何防止OSX使计算机进入睡眠状态

我关闭了Latop的盖子后,Ubuntu随机进入睡眠状态(使其进入睡眠状态)

VLC是否保持计算机处于活动状态,并防止其进入睡眠状态或显示屏幕保护程序?

防止系统进入睡眠/暂停状态-Xviewer / VLC如何做到这一点

避免检查后进入睡眠状态

主线程进入睡眠状态会引发InterruptedException

如何使当前线程进入睡眠状态?

活动中的NFC阅读进入睡眠状态?

x秒后使Windows 7进入睡眠状态

使Windows进入睡眠状态的热键是什么?

java thread.sleep也使swing ui进入睡眠状态