簡體   English   中英

在Startup上運行Python腳本以在Linux中記錄鼠標和鍵盤活動

[英]Running Python Script at Startup to log mouse and keyboard activity in Linux

在問這個問題或至少是一個類似的問題之前,我對很多人很有信心。 我的問題有點問題,但很簡單。

我試圖在啟動時使用cron運行Python腳本,並在Linux中編輯etc / rc.local文件,但兩種方式都無法運行我的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/

希望有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM