将在 Ubuntu 设定的时间后锁定屏幕的应用程序

Dmi*_*riy 10 lock-screen dbus gnome-screensaver 16.04

是否有适用于 Ubuntu 的应用程序,它会在规定的时间(例如 30 分钟)后锁定计算机。我需要它来限制孩子们看动画片的时间。例如,观看 30 分钟,然后 PC 锁定,需要父母解锁。

对于 Windows,我使用了 Eyes Relax 应用程序,它显示黑屏和绿色进度条(解锁 PC 的剩余时间)并且需要密码才能覆盖锁定。

Win*_*nix 15

2018 年 6 月 4 日更新

multi-timer已经创建了一个更高级的程序:一个可以同时设置不同警报的计时器

锁屏定时器

系统托盘.gif

创建您自己的屏幕锁定计时器而不是第 3 方应用程序

尽管有 3rd Party 应用程序可以执行此操作,但您可以创建自己的应用程序。步骤总结:

  • 使用 gedit 创建脚本 lock-screen-timer
  • 将此窗口中的代码复制并粘贴到 lock-screen-timer
  • 标记lock-screen-timer为可执行文件
  • 测试一下!
  • 配置 Nautilus 以执行 bash 脚本
  • 创建桌面快捷方式链接
  • 监控剩余时间

编辑 1(2016 年 11 月 19 日):最终版本。使用消息和声音在剩余 15、10、5、3、2 和 1 分钟时发出即将发生的锁定警告。使用 zenity 获取分钟数(默认为 30)。

编辑 2(2016 年 11 月 20 日):更改代码以终止之前的休眠脚本。

编辑 3(2017 年 1 月 20 日):修改杀掉之前的脚本代码。如果先前版本已在运行并终止,则添加信息消息。

编辑 4(2017 年 2 月 4 日):循环运行多个倒计时,而无需重新单击桌面图标。即洗衣之夜——16 分钟漂洗周期(织物柔软剂),13 分钟装入烘干机,58 分钟取出烘干机。

编辑 5(2017 年 2 月 11 日):将剩余分钟数写入 /tmp/lock-screen-timer-remaining. 这允许其他程序在屏幕锁定之前显示时间量。

编辑 6(2017 年 8 月 7 日):将/tmp/工作文件更改~/.为支持网络上的多个用户。

编辑:请注意,脚本依赖 ogg123 来播放提醒声音。该程序是 Universe 存储库中 vorbis-tool 包的一部分。此处提供的脚本也尝试使用此处找到hotplugtv 脚本。

编辑 8(2017 年 11 月 12 日):添加 WSL(适用于 Linux 的 Windows 10 子系统)支持。请注意,您需要添加VcXsrvubuntu-desktopxubuntu-desktop(首选)才能在 WSL 中使用 Linux GUI。Windows 10 系统托盘/通知区域剩余时间显示需要补充indicator-sysmonitorLinux 中的功能。未来的powershell.exe脚本计划用于 Windows 10 通知区域支持。

编辑 9(2018 年 2 月 19 日):修复动画缺少\旋转披萨“|,/,?,\”序列。

使用 gedit 创建脚本 lock-screen-timer

打开Terminal使用Ctrl+ Alt+T和类型:

gedit lock-screen-timer
Run Code Online (Sandbox Code Playgroud)

从下面的窗口复制并粘贴代码到 lock-screen-timer

切换回此屏幕并通过突出显示并按Ctrl+复制以下代码C

#!/bin/bash

# NAME: lock-screen-timer
# PATH: $HOME/bin
# DESC: Lock screen in x minutes
# CALL: Place on Desktop or call from Terminal with "lock-screen-timer 99"
# DATE: Created Nov 19, 2016. Last revision May 30, 2018.
# UPDT: Updated to support WSL (Windows Subsystem for Linux)
#       Remove hotplugtv. Replace ogg with paplay.
#       Cohesion with multi-timer. New sysmonitor indicator style.

# NOTE: Time defaults to 30 minutes.
#       If previous version is sleeping it is killed.
#       Zenity is used to pop up entry box to get number of minutes.
#       If zenity is closed with X or Cancel, no screen lock timer is launched.
#       Pending lock warning displayed on-screen at set intervals.
#       Write time remaining to ~/.lock-screen-timer-remaining

MINUTES="$1" # Optional parameter 1 when invoked from terminal.

# if no parameters set default MINUTES to 30
if [ $# == 0 ]; then
    MINUTES=30
fi

DEFAULT="$MINUTES" # When looping, minutes count down to zero. Save deafult for subsequent timers.

# Check if lock screen timer already running
pID=$(pgrep -f "${0##*/}") # All PIDs matching lock-screen-timer name
PREVIOUS=$(echo "$pID" | grep -v ^"$$") # Strip out this running copy ($$$)
if [ "$PREVIOUS" != "" ]; then
    kill "$PREVIOUS"
    rm ~/.lock-screen-timer-remaining
    zenity --info --title="Lock screen timer already running" --text="Previous lock screen timer has been terminated."
fi

# Running under WSL (Windows Subsystem for Linux)?
if cat /proc/version | grep Microsoft; then
    WSL_running=true
else
    WSL_running=false
fi


while true ; do # loop until cancel

    # Get number of minutes until lock from user
    MINUTES=$(zenity --entry --title="Lock screen timer" --text="Set number of minutes until lock" --entry-text="$DEFAULT")

    RESULT=$? # Zenity return code
    if [ $RESULT != 0 ]; then
        break ; # break out of timer lock screen loop and end this script.
    fi

    DEFAULT="$MINUTES" # Save deafult for subsequent timers.
    if [[ $MINUTES == 0 ]] || [[ $MINUTES == "" ]]; then
        break ; # zero minutes considered cancel.
    fi

    # Loop for X minutes, testing each minute for alert message.
    (( ++MINUTES )) 
    while (( --MINUTES > 0 )); do
        case $MINUTES in 1|2|3|5|10|15|30|45|60|120|480|960|1920)
            notify-send --urgency=critical --icon=/usr/share/icons/gnome/256x256/status/appointment-soon.png "Locking screen in ""$MINUTES"" minute(s)." ;
            if [[ $WSL_running == true ]]; then  
                powershell.exe -c '(New-Object Media.SoundPlayer "C:\Windows\Media\notify.wav").PlaySync();'
            else
               paplay /usr/share/sounds/freedesktop/stereo/complete.oga ;
            fi
           ;;
        esac;

        # Record number of minutes remaining to file other processes can read.
        echo "Lock screen in: $MINUTES Minutes" > ~/.lock-screen-timer-remaining

        sleep 60

    done

    rm ~/.lock-screen-timer-remaining # Remove work file others can see our progress with

    if [[ $WSL_running == true ]]; then  
        # Call lock screen for Windows 10
        rundll32.exe user32.dll,LockWorkStation
    else
        # Call screen saver lock for Ubuntu versions > 14.04.
        dbus-send --type=method_call --dest=org.gnome.ScreenSaver /org/gnome/ScreenSaver org.gnome.ScreenSaver.Lock
    fi

done # End of while loop getting minutes to next lock screen

exit 0 # Closed dialog box or "Cancel" selected.
Run Code Online (Sandbox Code Playgroud)

然后切换回空gedit窗口并使用Ctrl+粘贴代码V。保存文件并退出编辑器回到命令提示符。

标记lock-screen-timer为可执行文件

现在我们需要通过键入以下内容使脚本可执行:

chmod +x lock-screen-timer
Run Code Online (Sandbox Code Playgroud)

测试一下!

在从 GUI 调用脚本之前,我们将从终端调用它,以便我们可以查看是否显示任何错误消息:

~/lock-screen-timer
Run Code Online (Sandbox Code Playgroud)

系统会提示您输入分钟数:

锁屏定时器

设置所需的分钟数,然后单击“确定”以启动计时器。当还剩 15、10、5、3、2 和 1 分钟时,会听到系统声音并出现消息气泡,建议何时锁定屏幕。屏幕锁定后,您需要输入密码才能解锁屏幕。

配置 Nautilus 以执行 bash 脚本

Nautilus 定义了当我们双击一个可执行脚本时会发生什么,当它是文件显示窗口或桌面上的链接时。正常行为是使用gedit. 我们想改变这种行为,让它被执行。

启动 Nautilus 并导航到包含lock-screen-timer. 左键单击它一次以使其聚焦。将鼠标悬停在顶部菜单栏上,直到出现“文件编辑...”菜单,使用:

  1. 点击Edit下拉菜单
  2. 单击Properties选项
  3. 单击Behavior选项卡
  4. 观察下方的单选选项按钮 Executable Text Files
  5. 检查单选按钮 Run executable text files when they are opened

创建桌面快捷方式链接

从上一节lock-screen-timer仍然有重点。如果没有,导航到脚本并左键单击它一次以使其成为焦点。然后使用:

  • 右键单击文件,出现上下文菜单选项。
  • 从菜单中选择Make Link
  • 出现一个名为 的新图标Link to lock-screen-timer
  • 左键单击新图标并将其从 Nautilus 拖到桌面。

现在您可以双击桌面快捷方式链接并运行脚本。将出现一个对话框以获取分钟数。显示了两个按钮CancelOK。如果单击X关闭窗口,则与选择 相同Cancel

计时器运行后,您再次双击它,第一个正在运行的副本被“杀死”。您现在可以开始新的屏幕锁定倒计时或单击Cancel不倒计时。

在系统托盘/通知区域显示剩余时间

当锁屏计时器运行时,它会记录文件中剩余的分钟数~/.lock-screen-timer-remaining。您可以使用watch命令查看此文件,或将其显示在 Ubuntu 的系统托盘/应用程序指示器栏上,如本答案顶部所示。要在通知区域显示剩余时间,请按照此问答中的说明进行操作:(BASH 可以在系统托盘中显示为应用程序指示器吗?)。