I'd like to set time limits for some specific applications (such as games) on Ubuntu. There are various Windows applications that can do this, including HomeGuard Program Blocker, which can limit the usage of certain applications to specific times of day, or restrict application use to certain periods of time. Is there any similar software for Ubuntu?
Asked
Active
Viewed 3,628 times
10
-
There's a program called Parental Control for Ubuntu, but I don't know whether it makes it possible to block all applications. – Anderson Green Dec 07 '12 at 17:40
-
So far, this is the only relevant discussion that I've found on the Ubuntu Forums: http://ubuntuforums.org/showthread.php?t=977836 Someone mentioned applications called "Cyborg" and "Cybercafe", and I'm still trying to find them. – Anderson Green Dec 07 '12 at 17:50
-
There's also a program called timekpr, but I don't know whether it can block access to specific applications. – Anderson Green Dec 07 '12 at 17:57
-
If I'm going to develop an application that does this, then I'll need to find a way to detect running processes: http://www.cyberciti.biz/faq/show-all-running-processes-in-linux/ – Anderson Green Dec 07 '12 at 18:45
-
It's also possible to restrict access to applications via the command line - this information may be useful to developers as well. http://askubuntu.com/questions/8149/how-can-i-restrict-program-access-to-other-users – Anderson Green Dec 07 '12 at 18:47
2 Answers
7
I just made the following script for this purpose. I named it 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
Don't forget to make it executable:
chmod +x timelimit
Syntax:
timelimit APP_NAME
Important:
- You can test this script in terminal, but it should be to run at system startup. To make it to run at startup, see How to run scripts on start up?
This script should be restarted at midnight if you don't turn off your computer at night. To do this you can add a cron job like:
00 00 * * * kill `pgrep -x timelimit` && timelimit APP_NAME
More about: https://help.ubuntu.com/community/CronHowto

Radu Rădeanu
- 169,590
-
Wauw! Nice! Would rebooting the computer bypass the timelimit? If so, would it be possible to make the timelimit persistent (in a read only file for instance)? – don.joey Aug 30 '13 at 10:05
-
@Private You can test it. The time limit is stored in a file, so, yes, the computer bypass the time limit at reboot. – Radu Rădeanu Aug 30 '13 at 10:19
-
I think this could be packaged and become part of the repositories. Beautiful bash skills! Thanks and I am glad to offer you this bounty. – don.joey Sep 02 '13 at 07:29
-
It is nice that it gives a little ubuntu message box to say that the time to play with a game is over for a day, but that message keeps on being displayed... for hours :) – don.joey Sep 02 '13 at 08:05
-
@Private With which app did you test it? That app would probably wants to open again and again. – Radu Rădeanu Sep 02 '13 at 08:24
-
-
@Private That's because there is another process
/usr/sbin/console-kit-daemon
that wants to run all the time. I modified the script by replacingpgrep
withpgrep -x
to match exactly with the app name. – Radu Rădeanu Sep 02 '13 at 14:54 -
Somehow this broke the script for me. I start it with
@reboot ~/timelimit.sh pokerth
in a cron job, for instance, it then creates the .count_time_pokerth file, but does not actually count down... – don.joey Sep 02 '13 at 15:38 -
@Private I just test it exactly how you said and I have no problem. – Radu Rădeanu Sep 02 '13 at 16:41
0
Restrict for each launch
Simple and easily hackable. But use it to control yourself
timeout 15 totem
will exit the program after 15 sec. But can be launched again. Edit and add timeout <sec>
to apps '.desktop' file's exec
field

timm
- 1