为特定应用程序(例如游戏)设置时间限制

为特定应用程序(例如游戏)设置时间限制

我想在 Ubuntu 上为某些特定应用程序(例如游戏)设置时间限制。有各种 Windows 应用程序可以做到这一点,包括 HomeGuard Program Blocker,它可以将某些应用程序的使用限制在一天中的特定时间,或将应用程序的使用限制在特定的时间段。有没有适用于 Ubuntu 的类似软件?

答案1

为此,我编写了以下脚本。我将其命名为timelimit

#!/bin/bash

#timelimit - Set daily time limits for specific applications

#Licensed under the standard MIT license:
#Copyright 2013 Radu Rădeanu (https://askubuntu.com/users/147044/).
#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

if [ $# -ne 1 ];then
    echo "Usage: `basename $0` APP_NAME"
    exit 1
fi

export DISPLAY=:0

app_name=$@
time_to_play_daily="2:30:00"  # channge as you wish; the format is: H[:M[:S]]
file="$HOME/.count_time_$app_name"

if [ -a $file ]; then
    if [ "$(head -1 $file)" != "$(date +%D)" ]; then
        echo $(date +%D) > $file
        echo $time_to_play_daily >> $file
    fi
else 
    touch $file
    echo $(date +%D) >> $file
    echo $time_to_play_daily >> $file
fi

time_to_play_left=$(sed -n '2p' $file)

sec_left=$(echo $time_to_play_left | awk -F: '{ print ($1 * 3600) + ($2 * 60) + $3 }')

function countdown
{
    sec_left=$(echo $time_to_play_left | awk -F: '{ print ($1 * 3600) + ($2 * 60) + $3 }')
    local start=$(date +%s)
    local end=$((start + sec_left))
    local cur=$start

    while [[ $cur -lt $end ]]; do
        pid=$(pgrep -x $app_name)
        if [ "$pid" != "" ]; then
            cur=$(date +%s)
            sec_left=$((end-cur))
            time_to_play_left="$((sec_left/3600)):$(((sec_left/60)%60)):$((sec_left%60))"
            sed -i "2s/.*/$time_to_play_left/" $file
            # next line is useful only when you test from terminal
            printf "\rTime left to play with %s: %02d:%02d:%02d" $app_name $((sec_left/3600)) $(((sec_left/60)%60)) $((sec_left%60))
            sleep 1
        else
            break
        fi
    done
}

while : ; do
    pid=$(pgrep -x $app_name)
    sleep 1
    if [ "$pid" != "" ]; then
        if [ $sec_left -gt 0 ]; then
            notify-send -i $app_name "Time left to play with $app_name for today: $time_to_play_left"
        else
            notify-send -i "error" "Your time to play with $app_name has finished for today!"
        fi
        countdown $time_to_play_left
        pid=$(pgrep -x $app_name)
        if [ "$pid" != "" ]; then kill $pid; fi
    fi
done

不要忘记使其可执行:

chmod +x timelimit

句法:

时限应用程序名称

重要的:

  • 您可以在终端中测试此脚本,但它应该在系统启动时运行。要使其在启动时运行,请参阅如何在启动时运行脚本?
  • 如果您晚上不关闭计算机,则应在午夜重新启动此脚本。为此,您可以添加一个 cron 作业,例如:

    00 00 * * * 杀死`pgrep -x timelimit`&& 时间限制应用程序名称
    

    更多关于:https://help.ubuntu.com/community/CronHowto

答案2

每次启动都限制
简单且容易被破解。但使用它来控制自己
timeout 15 totem 将在 15 秒后退出程序。但可以再次启动。编辑并添加timeout <sec>到应用程序“.desktop”文件的exec 字段

相关内容