如何在解锁 30 分钟后锁定屏幕

如何在解锁 30 分钟后锁定屏幕

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

我怎样才能编写脚本来执行此操作?

答案1

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

剧本

#!/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

如何使用

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

    python3 /path/to/lock_screen.py 30
    

    (尽管对于测试来说,我会花更短的时间)

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

    python3 /path/to/lock_screen.py 30
    

答案2

下面显示的脚本在用户登录时启动,并等待一段时间后锁定屏幕。必须记住两个注意事项:脚本必须是启动应用程序的一部分,并且必须是可执行的。

TIME根据用途,可以通过更改变量在脚本中配置每个会话的时间/bin/sleep。来自man sleep

暂停 NUMBER 秒。SUFFIX 可以是“s”(表示秒)(默认值)、“m”(表示分钟)、“h”(表示小时)或“d”(表示天)。与大多数要求 NUMBER 为整数的实现不同,此处 NUMBER 可以是任意浮点数。

该脚本可以手动启动,也可以作为启动应用程序的一部分在每次 GUI 登录时调用。请参阅 如何在登录时自动启动应用程序?为了那个原因。

简单设置

  1. bin在您的个人HOME文件夹中创建名为的文件夹。
  2. 在文件夹中bin创建名为 的文件sessionLocker.sh。将源代码复制到该文件中
  3. 右键单击文件,授予其可执行权限,转到“属性”->“权限”选项卡,选中“允许将文件作为程序执行”选项。或者,chmod +x $HOME/bin/sessionLocker.sh在终端中使用
  4. 跑步启动应用程序. 将完整路径添加到脚本作为启动应用程序之一。例如:/home/MYUSERNAME/bin/sessionLocker.sh
  5. 重新启动 Unity 会话进行测试。

该脚本也发布在我的个人github上。用于git clone https://github.com/SergKolo/sergrep.git下载源代码。

脚本源

#!/bin/bash
##################################################
# AUTHOR: Serg Kolo 
# Date: Jan 2nd 2016
# Description: A script that locks session every x
#       minutes. 
# TESTED ON: 14.04.3 LTS, Trusty Tahr
# WRITTEN FOR: https://askubuntu.com/q/715721/295286
# Depends: qbus, dbus, Unity desktop
###################################################

# Copyright (c) 2016 Serg Kolo
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal in 
# the Software without restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 
# the Software, and to permit persons to whom the Software is furnished to do so, 
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all 
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 
# PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


##############
# INTRODUCTION
##############

# This script locks user session every x minutes, and repeats this task
# upon user re-logging in. Useful for creating session for children, study session
# for college students, pomodoro sessions for self-learners ,etc.
#
# This can be started manually or as part of Startup Applications 

###########
# VARIABLES
###########
TIME="30m"

##########
# MAIN
##########
while [ 1 ]; 
do
  # Wait the time defined in VARIABLES part and lock the session
  /bin/sleep $TIME &&  qdbus  com.canonical.Unity /com/canonical/Unity/Session com.canonical.Unity.Session.Lock

  # Continuously query dbus every 0.25 seconds test whether session is locked
  # Once this sub-loop breaks, the main one can resume the wait and lock cycle.
  while [ $(qdbus  com.canonical.Unity /com/canonical/Unity/Session com.canonical.Unity.Session.IsLocked) == "true" ];
  do
    /bin/sleep 0.25 && continue
  done
done

答案3

谢谢你的帮助。我决定将你的答案的部分内容与我在网上找到的其他内容结合起来,并在 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()

相关内容