在启动时运行 Python 脚本以记录 Linux 中的鼠标和键盘活动



我很有信心很多人在问这个问题或至少是一个类似的问题之前。我的问题有点问题,但很简单。

我尝试在启动时使用cron和在 Linux 中编辑 etc/rc.local 文件来运行 Python 脚本,但这两种方式都无法运行我的 Python 脚本。

我的脚本使用键盘和鼠标的侦听器对象记录键盘和鼠标的活动。我使用一个名为pynput的第三方软件包,这取决于Xlib。我的脚本编辑需要sudo访问权限的文件,因此我必须使用sudo运行脚本。

您需要查看脚本,以便了解:

#!/usr/bin/env python
#backlight.py
from pynput.keyboard import Listener  as KeyboardListener
from pynput.mouse    import Listener  as MouseListener
import time
STATUS = ""                # Keyboard backlight ON/OFF status
turnOffSecs = 6           # Turn off keyboard backlight for x seconds of inactivity
# Keyboard brightness control file (change to your directory)
file_pth = "/sys/devices/platform/asus-nb-wmi/leds/asus::kbd_backlight/brightness"
def get_LEVEL():
"""return the current level of brightness of keyboard backlight"""
with open(file_pth, "r") as f:
brightness_level = f.read()[0]
return brightness_level

class Sec_timer:
"""
Sec_timer(until=None) 
Create a timer that counts x number of seconds starting from 0
until arg can be used for reseting the timer:
*Example:
timer = Sec_imer(20)
while timer.elapsed < timer.until:                  
timer.count()                # count a second
else:
timer.reset_timer()          # reset timer on exit
"""
def __init__(self, until=None):
self.until = until
self.elapsed = 0 
def count_sec(self): 
"Count one second per-call"
time.sleep(1)
self.elapsed += 1
def reset_timer(self):
self.elapsed = 0 

timer = Sec_timer(turnOffSecs)           # Sec_timer(x) turn off keyboard backlight for x seconds of inactivity
# General event handler to reset timer
def reset_timer(*args):
global STATUS
timer.reset_timer()
if STATUS == "OFF":
# print(STATUS)
with open(file_pth, "w") as f:
f.write(current_brightnessLevel)
f.close()
STATUS="ON"

keyboard_listener = KeyboardListener(on_press=reset_timer, 
on_release=(lambda *args: None))
mouse_listener    = MouseListener(on_click=reset_timer, 
on_scroll=reset_timer, 
on_move=reset_timer)

keyboard_listener.start()
mouse_listener.start()

while True:
timer.count_sec()
if timer.elapsed > timer.until:
# print "current brightness:" +  get_LEVEL()
if  get_LEVEL() != "0":
with open(file_pth, "w") as f: 
current_brightnessLevel = get_LEVEL()
f.write("0")
STATUS = "OFF"

此脚本记录键盘和鼠标的活动,任何键盘或鼠标活动都将重置计时器。当 x 秒过去并且没有发生键盘或鼠标活动时,通过在亮度文件夹中写"0"来关闭键盘的背光。如果在脚本关闭背光after发生任何鼠标或键盘事件,则根据背光的先前亮度级别打开键盘亮度。

使用终端运行脚本,它工作得很好。但是,自动启动此脚本很棘手;这是我到目前为止尝试过的:

*请注意,我已经使脚本文件可执行,并且 backlight.py 驻留在/home/user

1) 终端内:

$ xhost +
$ sudo ./backlight.py

工作正常!

2)使用'etc/rc.local我添加了:

xhost + 
cd /home/user/
./backlight.py

脚本在重新启动后未运行

3) 使用etc/rc.local

$ sudo crontab -e

@reboot xhost +
@reboot /home/user/backlight.py

重新启动后未运行

由于Xlib的问题,我不得不执行xhost +.使用最后两种方法不起作用。我猜这是一个编程问题,也许它与Xlib有关?!

我知道这个脚本在与键盘 LED 接口方面很疯狂,如果你愿意,可以称之为"坏脚本"。虽然我只是想使用文件输入/输出解决方案解决驱动程序问题,因为我没有兴趣深入研究 Linux 驱动程序细节,至少现在没有!

好吧,如果它需要root并且必须在启动时运行,为什么不创建自定义服务呢?

假设您创建了一个自定义服务,systemd 可以为您启动它。 然后,您可以使用sudo systemctl start yourservice启动服务 您可以确保它在启动时(以 root 身份)使用sudo systemctl enable yourservice.无论用户是谁,它都将启动。

我以前没有亲自做过这件事,但这应该不会太难。它基本上只是一个包含描述和执行命令的文件,在目录/etc/systemd/system 中创建。快速的谷歌搜索给了我这个指南:https://blog.sdbarker.com/post/adding-custom-units-services-to-systemd-on-arch-linux/

希望有帮助。

最新更新