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

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

如何编写脚本来执行此操作?

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

剧本

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

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

  • 如果一切正常,请将其添加到Startup Applications Dash> Startup Applications> Add。 添加命令:

     python3 /path/to/lock_screen.py 30 

下面的脚本在用户登录时开始,并在锁定屏幕之前等待一定的时间。 必须记住两个警告:脚本必须是启动应用程序的一部分,并且必须是可执行的。

根据/bin/sleep使用情况,可以通过更改TIME变量在脚本本身内配置每个会话的TIME 。 从man sleep

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

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

简单的设置

  1. 在个人HOME文件夹中创建名为bin文件夹。
  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 

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