AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 992699
Accepted
umpirsky
umpirsky
Asked: 2018-01-06 10:10:51 +0800 CST2018-01-06 10:10:51 +0800 CST 2018-01-06 10:10:51 +0800 CST

空闲/活动时运行命令

  • 772

我需要在以下情况下运行自定义命令:

  1. 计算机空闲 X 分钟(未按下任何键)
  2. 按键

我实际上想要实现的是我的键盘的省电功能。

我想使用:

g413-led -a 000000
g413-led -a FFFFFF

使用https://github.com/MatMoul/g810-led打开/关闭键盘背光。

第一个相当容易使用以下之一:

  • 西德勒
  • xprintidle
  • 自动锁定

如果有更好,更优化的方法,请指教?

但是如何在按键或从空闲状态恢复时再次点亮它?我猜在每个按键上运行命令都过大了。是否有节电选项的事件?显示器节能是如何工作的?

在此处输入图像描述

我可以重复使用相同的事件/超时吗?

gnome
  • 2 2 个回答
  • 2488 Views

2 个回答

  • Voted
  1. Best Answer
    umpirsky
    2018-01-08T01:33:24+08:002018-01-08T01:33:24+08:00

    我想出的解决方案是 DBUS 事件。

    我首先运行dbus-monitor以查看当监视器从睡眠中进入/唤醒时会触发哪些事件:

    signal time=1515274499.244468 sender=org.freedesktop.DBus -> destination=:1.394 serial=2 path=/org/freedesktop/DBus; interface=org.freedesktop.DBus; member=NameAcquired
    string ":1.394"
    signal time=1515274499.244502 sender=org.freedesktop.DBus -> destination=:1.394 serial=4 path=/org/freedesktop/DBus; interface=org.freedesktop.DBus; member=NameLost
    string ":1.394"
    signal time=1515274559.337480 sender=:1.6 -> destination=:1.2 serial=29015 path=/org/gnome/Mutter/IdleMonitor/Core; interface=org.gnome.Mutter.IdleMonitor; member=WatchFired
    uint32 440
    signal time=1515274559.337878 sender=:1.2 -> destination=(null destination) serial=3288 path=/org/gnome/SessionManager/Presence; interface=org.gnome.SessionManager.Presence; member=StatusChanged
    uint32 3
    signal time=1515274559.338267 sender=:1.2 -> destination=(null destination) serial=3290 path=/org/gnome/SessionManager/Presence; interface=org.freedesktop.DBus.Properties; member=PropertiesChanged
    string "org.gnome.SessionManager.Presence"
    array [
    dict entry(
    string "status"
    variant uint32 3
    )
    ]
    array [
    ]
    signal time=1515274569.389778 sender=:1.6 -> destination=(null destination) serial=29018 path=/org/gnome/Shell; interface=org.gnome.Shell.Extensions; member=ExtensionStatusChanged
    string "[email protected]"
    int32 2
    string ""
    signal time=1515274569.398929 sender=:1.6 -> destination=(null destination) serial=29019 path=/org/gnome/Shell; interface=org.gnome.Shell.Extensions; member=ExtensionStatusChanged
    string "[email protected]"
    int32 2
    string ""
    signal time=1515274569.399633 sender=org.freedesktop.DBus -> destination=:1.6 serial=5195 path=/org/freedesktop/DBus; interface=org.freedesktop.DBus; member=NameLost
    string "org.kde.StatusNotifierWatcher"
    signal time=1515274569.399660 sender=org.freedesktop.DBus -> destination=(null destination) serial=850 path=/org/freedesktop/DBus; interface=org.freedesktop.DBus; member=NameOwnerChanged
    string "org.kde.StatusNotifierWatcher"
    string ":1.6"
    string ""
    signal time=1515274569.404274 sender=:1.6 -> destination=(null destination) serial=29022 path=/org/gnome/Shell; interface=org.gnome.Shell.Extensions; member=ExtensionStatusChanged
    string "[email protected]"
    int32 2
    string ""
    signal time=1515274569.442137 sender=:1.6 -> destination=(null destination) serial=29025 path=/org/gnome/Shell; interface=org.gnome.Shell.Extensions; member=ExtensionStatusChanged
    string "[email protected]"
    int32 2
    string ""
    signal time=1515274569.455035 sender=:1.6 -> destination=(null destination) serial=29027 path=/org/gnome/ScreenSaver; interface=org.gnome.ScreenSaver; member=ActiveChanged
    boolean true
    signal time=1515274569.657300 sender=:1.6 -> destination=:1.42 serial=29037 path=/org/gnome/Mutter/IdleMonitor/Core; interface=org.gnome.Mutter.IdleMonitor; member=WatchFired
    uint32 491
    signal time=1515274569.657404 sender=:1.6 -> destination=(null destination) serial=29038 path=/org/gnome/Mutter/DisplayConfig; interface=org.freedesktop.DBus.Properties; member=PropertiesChanged
    string "org.gnome.Mutter.DisplayConfig"
    array [
    dict entry(
    string "PowerSaveMode"
    variant int32 3
    )
    ]
    array [
    ]
    signal time=1515276608.243100 sender=:1.6 -> destination=:1.2 serial=29039 path=/org/gnome/Mutter/IdleMonitor/Core; interface=org.gnome.Mutter.IdleMonitor; member=WatchFired
    uint32 489
    signal time=1515276608.243573 sender=:1.2 -> destination=(null destination) serial=3291 path=/org/gnome/SessionManager/Presence; interface=org.gnome.SessionManager.Presence; member=StatusChanged
    uint32 0
    signal time=1515276608.243843 sender=:1.2 -> destination=(null destination) serial=3292 path=/org/gnome/SessionManager/Presence; interface=org.freedesktop.DBus.Properties; member=PropertiesChanged
    string "org.gnome.SessionManager.Presence"
    array [
    dict entry(
    string "status"
    variant uint32 0
    )
    ]
    array [
    ]
    signal time=1515276608.244032 sender=:1.6 -> destination=:1.42 serial=29040 path=/org/gnome/Mutter/IdleMonitor/Core; interface=org.gnome.Mutter.IdleMonitor; member=WatchFired
    uint32 494
    signal time=1515276608.270459 sender=:1.6 -> destination=(null destination) serial=29042 path=/org/gnome/Mutter/DisplayConfig; interface=org.freedesktop.DBus.Properties; member=PropertiesChanged
    string "org.gnome.Mutter.DisplayConfig"
    array [
    dict entry(
    string "PowerSaveMode"
    variant int32 0
    )
    ]
    array [
    ]
    signal time=1515276623.523917 sender=:1.6 -> destination=:1.42 serial=29043 path=/org/gnome/Mutter/IdleMonitor/Core; interface=org.gnome.Mutter.IdleMonitor; member=WatchFired
    uint32 491
    signal time=1515276623.554451 sender=:1.6 -> destination=(null destination) serial=29047 path=/org/gnome/Mutter/DisplayConfig; interface=org.freedesktop.DBus.Properties; member=PropertiesChanged
    string "org.gnome.Mutter.DisplayConfig"
    array [
    dict entry(
    string "PowerSaveMode"
    variant int32 3
    )
    ]
    array [
    ]
    signal time=1515276668.529836 sender=:1.6 -> destination=:1.2 serial=29048 path=/org/gnome/Mutter/IdleMonitor/Core; interface=org.gnome.Mutter.IdleMonitor; member=WatchFired
    uint32 440
    signal time=1515276668.530236 sender=:1.2 -> destination=(null destination) serial=3293 path=/org/gnome/SessionManager/Presence; interface=org.gnome.SessionManager.Presence; member=StatusChanged
    uint32 3
    signal time=1515276668.530565 sender=:1.2 -> destination=(null destination) serial=3295 path=/org/gnome/SessionManager/Presence; interface=org.freedesktop.DBus.Properties; member=PropertiesChanged
    string "org.gnome.SessionManager.Presence"
    array [
    dict entry(
    string "status"
    variant uint32 3
    )
    ]
    array [
    ]
    signal time=1515279067.394398 sender=:1.6 -> destination=:1.2 serial=29051 path=/org/gnome/Mutter/IdleMonitor/Core; interface=org.gnome.Mutter.IdleMonitor; member=WatchFired
    uint32 498
    signal time=1515279067.394982 sender=:1.2 -> destination=(null destination) serial=3296 path=/org/gnome/SessionManager/Presence; interface=org.gnome.SessionManager.Presence; member=StatusChanged
    uint32 0
    signal time=1515279067.395139 sender=:1.2 -> destination=(null destination) serial=3297 path=/org/gnome/SessionManager/Presence; interface=org.freedesktop.DBus.Properties; member=PropertiesChanged
    string "org.gnome.SessionManager.Presence"
    array [
    dict entry(
    string "status"
    variant uint32 0
    )
    ]
    array [
    ]
    signal time=1515279067.395662 sender=:1.6 -> destination=:1.42 serial=29052 path=/org/gnome/Mutter/IdleMonitor/Core; interface=org.gnome.Mutter.IdleMonitor; member=WatchFired
    uint32 496
    signal time=1515279067.414904 sender=:1.6 -> destination=(null destination) serial=29054 path=/org/gnome/Mutter/DisplayConfig; interface=org.freedesktop.DBus.Properties; member=PropertiesChanged
    string "org.gnome.Mutter.DisplayConfig"
    array [
    dict entry(
    string "PowerSaveMode"
    variant int32 0
    )
    ]
    array [
    ]
    signal time=1515279076.828255 sender=:1.6 -> destination=(null destination) serial=29063 path=/org/gnome/Shell; interface=org.gnome.Shell.Extensions; member=ExtensionStatusChanged
    string "[email protected]"
    int32 1
    string ""
    signal time=1515279076.829775 sender=org.freedesktop.DBus -> destination=(null destination) serial=851 path=/org/freedesktop/DBus; interface=org.freedesktop.DBus; member=NameOwnerChanged
    string "org.kde.StatusNotifierWatcher"
    string ""
    string ":1.6"
    signal time=1515279076.829812 sender=org.freedesktop.DBus -> destination=:1.6 serial=5215 path=/org/freedesktop/DBus; interface=org.freedesktop.DBus; member=NameAcquired
    string "org.kde.StatusNotifierWatcher"
    signal time=1515279076.829824 sender=:1.6 -> destination=(null destination) serial=29066 path=/org/gnome/Shell; interface=org.gnome.Shell.Extensions; member=ExtensionStatusChanged
    string "[email protected]"
    int32 1
    string ""
    signal time=1515279076.841923 sender=:1.6 -> destination=(null destination) serial=29067 path=/org/gnome/Shell; interface=org.gnome.Shell.Extensions; member=ExtensionStatusChanged
    string "[email protected]"
    int32 1
    string ""
    signal time=1515279076.844396 sender=:1.6 -> destination=(null destination) serial=29068 path=/org/gnome/Shell; interface=org.gnome.Shell.Extensions; member=ExtensionStatusChanged
    string "[email protected]"
    int32 1
    string ""
    signal time=1515279076.851323 sender=:1.6 -> destination=(null destination) serial=29069 path=/org/gnome/ScreenSaver; interface=org.gnome.ScreenSaver; member=ActiveChanged
    boolean false
    signal time=1515279076.896958 sender=:1.6 -> destination=(null destination) serial=29218 path=/StatusNotifierWatcher; interface=org.kde.StatusNotifierWatcher; member=StatusNotifierItemRegistered
    string "/"
    signal time=1515279076.921110 sender=:1.38 -> destination=(null destination) serial=2735 path=/org/gnome/evolution/dataserver/CalendarView/1887/93; interface=org.gnome.evolution.dataserver.CalendarView; member=complete
    array [
    string ""
    string ""
    ]
    signal time=1515279076.924727 sender=:1.34 -> destination=(null destination) serial=2701 path=/org/gnome/evolution/dataserver/CalendarView/1870/92; interface=org.gnome.evolution.dataserver.CalendarView; member=complete
    array [
    string ""
    string ""
    ]
    signal time=1515279077.686970 sender=:1.6 -> destination=(null destination) serial=30014 path=/StatusNotifierWatcher; interface=org.freedesktop.DBus.Properties; member=PropertiesChanged
    string "org.kde.StatusNotifierWatcher"
    array [
    dict entry(
    string "RegisteredStatusNotifierItems"
    variant array [
    string ":1.66/org/ayatana/NotificationItem/software_update_available"
    ]
    )
    ]
    array [
    

    我第一次尝试 for org.gnome.Mutter.IdleMonitor,但uint32值不断增加,不知道为什么?

    我选择了 StatusChanged,这是最终的脚本:

    #!/bin/bash
    # Turn keyboard backlight on/off using https://github.com/MatMoul/g810-led
    
    g413-led -a FFFFFF # always turn on on startup (it turns off for some reason on reboot)
    
    dbus-monitor --session "type=signal,interface=org.gnome.SessionManager.Presence,member=StatusChanged" |
      while read x; do
          case "$x" in
            *"uint32 3"*) g413-led -a 000000;;
            *"uint32 0"*) g413-led -a FFFFFF;;
          esac
      done
    

    屏幕保护程序事件也可以工作:

    g413-led -a FFFFFF # always turn on on startup (it turns off for some reason on reboot)
    
    dbus-monitor --session "type='signal',interface='org.gnome.ScreenSaver'" |
      while read x; do
          case "$x" in
            *"boolean true"*) g413-led -a 000000;;
            *"boolean false"*) g413-led -a FFFFFF;;
          esac
      done
    

    当然,你可以输入任何你想要的命令。与此相比,第一个脚本的优点是它在用户登录之前点亮键盘,这使得输入密码更容易。

    唯一的缺点是键盘在用户登录后亮起,而不是在按键时亮起,但我可以忍受。

    我正在尝试:

    dbus-monitor --session "type='signal',interface='org.freedesktop.DBus.Properties',path='/org/gnome/Mutter/DisplayConfig'" | grep 'variant int32' |
      while read x; do
          case "$x" in
            *"variant int32 3"*) g413-led -a 000000;;
            *"variant int32 0"*) g413-led -a FFFFFF;;
          esac
      done
    

    但由于某种原因它不起作用,可能是grep问题。

    • 4
  2. Jacob Vlijm
    2018-01-08T03:06:44+08:002018-01-08T03:06:44+08:00

    微小的后台进程

    对于一个简单的问题,您的设置似乎相当复杂。我要做的只是简单地运行一个小后台进程,每 2 秒左右检查一次空闲时间,如果空闲时间超过限制,则采取一项行动,如果切换到低于设定的空闲时间,则采取另一项行动。

    剧本:

    #!/usr/bin/env python3
    import time
    import subprocess
    
    # set idle time (seconds)
    t = 60
    
    # set commands
    on_idle = ["g413", "-led", "-a", "000000"]
    on_active = ["g413", "-led", "-a", "FFFFFF"]
    
    def set_state(cmd):
        subprocess.Popen(cmd)
    
    def get_idle():
        return int(subprocess.check_output("xprintidle").decode("utf-8").strip())/1000
    
    idle1 = 0
    
    while True:
        time.sleep(2)
        idle2 = get_idle()
        # if idle time exceeds (passes) the limit, run one command
        if all([idle2 >= t, idle1 < t]):
            set_state(on_idle)
        # if idle time switches to below (passes) the limit, run another command
        elif all([idle2 <= t, idle1 > t]):
            set_state(on_active)
        idle1 = idle2
    

    使用

    • 确保xprintidle已安装

      sudo apt install xprintidle
      
    • 将脚本复制到一个空文件中,另存为switch_light.py

    • 设置所需的空闲时间以在以下时间采取行动:

      # set idle time (seconds)
      t = 60
      
    • 通过以下命令运行它:

      python3 /path/to/switch_light.py
      
    • 如果一切正常,请将其添加到启动应用程序。

    笔记)

    • 由于我没有您的键盘,因此无法对其进行测试,但是使用其他命令进行了测试,它可以很好地完成工作。

    • 您可以将脚本与任何其他命令一起使用:

      # set commands
      on_idle = ["g413", "-led", "-a", "000000"]
      on_active = ["g413", "-led", "-a", "FFFFFF"]
      

      只要确保像示例中那样分开参数即可。

    • 如果您只想对超过空闲时间(或相反)采取行动,只需注释掉:

      # if idle time switches to below (passes) the limit, run another 
      elif all([idle2 <= t, idle1 > t]):
          set_state(on_active)
      
    • 2

相关问题

  • 如何安装 KDE?

  • 为什么我的时钟、指示器小程序和通知区域有时会在我重新启动时移动?我怎样才能防止这种情况?

  • 停止菜单图标闪烁

  • 是否有适用于 IMAP 邮件帐户的 Gnome 小程序?

  • 如果顶部面板中缺少会话小程序,如何注销?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve