解锁30分钟后如何锁屏

aam*_*aam 8 command-line scripts lock-screen schedule

我希望我的孩子只使用电脑 30 分钟,然后我希望屏幕被锁定。那时,如果我选择再次解锁屏幕,我希望再过 30 分钟屏幕再次锁定。

我怎样才能写一个脚本来做到这一点?

Jac*_*ijm 4

在后台运行以下脚本,它将在任意分钟后锁定屏幕:

剧本

#!/usr/bin/env python3
import subprocess
import time
import sys

t = 0; max_t = int(sys.argv[1])

while True:
    # check runs once per minute
    time.sleep(60)
    # check the lock status, add 1 to current time if not locked, else t = 0
    try:
        subprocess.check_output(["pgrep", "-cf", "lockscreen-mode"]).decode("utf-8").strip()
        t = 0
    except subprocess.CalledProcessError:
        t += 1
    # if unlocked status time exceeds set time (in minutes), lock screen
    if t >= max_t:
        subprocess.Popen(["gnome-screensaver-command",  "-l"])
        t = 0
Run Code Online (Sandbox Code Playgroud)

如何使用

  • 将脚本复制到一个空文件中,另存为lock_screen.py
  • 测试 - 从终端运行它,并将锁定时间作为参数(分钟)

    python3 /path/to/lock_screen.py 30
    
    Run Code Online (Sandbox Code Playgroud)

    (虽然为了测试,我会花更短的时间)

  • 如果一切正常,请将其添加到启动应用程序 Dash > 启动应用程序 > 添加。添加命令:

    python3 /path/to/lock_screen.py 30
    
    Run Code Online (Sandbox Code Playgroud)


aam*_*aam -2

感谢您的帮助。我决定将你的部分答案与我在网上找到的其他内容结合起来,并在 python 2.x 中提出了这个解决方案:

import gobject, dbus, time, subprocess
from dbus.mainloop.glib import DBusGMainLoop  

time.sleep(30*60)
subprocess.Popen(["gnome-screensaver-command", "-l"])

def lock_status(bus, message):

    if message.get_member() != "EventEmitted": 
        return

    args = message.get_args_list()

    if args[0] == "desktop-unlock":  
        time.sleep(30*60)
        subprocess.Popen(["gnome-screensaver-command", "-l"])

DBusGMainLoop(set_as_default=True)
bus = dbus.SessionBus()
bus.add_match_string("type='signal',interface='com.ubuntu.Upstart0_6'")
bus.add_message_filter(lock_status)
gobject.MainLoop().run()
Run Code Online (Sandbox Code Playgroud)