首页 > 解决方案 > 我想清理一个脚本,它会在 X 分钟不活动后自动锁定并关闭我的屏幕

问题描述

我正在运行 Lubuntu 20.04 LTS。

目前,要手动关闭我的显示器并锁定我的屏幕,我运行这个 bash 脚本/home/y/Scripts/Script to lock the screen but keep the computer awake using i3lock......

i3lock -u -i /home/y/Pictures/02-Wallpaper.png &
sleep 4 &&
xtrlock &
sleep 2 &&
xset dpms force off

我希望该脚本在 X 分钟不活动后自动运行。

因此,在一段时间不活动后,我稍微修改了这个 Python 脚本锁屏,但不是在合上盖子时——我不完全理解——来创建......

import subprocess
import time

# set screen off after x seconds
off = 300

# lock screen after x seconds
lock = 3

# check idle every x seconds
res = 3

def get(cmd):
    return subprocess.check_output(cmd).decode("utf-8").strip()

def test(t, threshold):
    return int(t)/1000 < threshold

testoff1 = True
testlock1 = True

t1 = 0

while True:
    time.sleep(res)
    t2 = get("xprintidle")
    testoff2 = test(t2, off); testlock2 = test(t2, lock)
    if (testoff2, testoff1) == (False, True):
        subprocess.run(["/bin/bash", "/home/y/Scripts/Script to lock the screen but keep the computer awake using i3lock"])
    if (testlock2, testlock1) == (False, True):
        subprocess.Popen(["gnome-screensaver-command", "-l"])                
    testoff1 = testoff2; testlock1 = testlock2

当我简单地测试它时,它起作用了。也就是说,我的屏幕正确锁定。我想删除脚本的无关部分。例如,我认为我不需要subprocess.Popen(["gnome-screensaver-command", "-l"]). 另外,我可以删除代码的其他部分testoff1 = testoff2; testlock1 = testlock2吗?

标签: python-3.x

解决方案


推荐阅读