Other Stuff:

How to Limit Daily Desktop Usage in Ubuntu

I have been looking for a way to limit the amount of time my kids spend on the computer. After toying with a couple of different things like timeoutd and pam_time, I decided to hack together something to meet my needs:

My kids share a PC with their Mom using fast-user-switching, so I needed it to keep track of multiple sessions.

I needed different times for different users ( Mom wouldn’t be too thrilled with me forcing her to log off after an hour or two )

I wanted everyone to get ample warning so they could save their work and logout cleanly.

What timekpr does is watch for instances of the session manager, keep a running tally of the time they are running, and logs kills it when time is up. Several libnotify alerts let the user know when thier time is running out.

It’s pretty rough right now ( just a couple of hours of research and bash hacking ) but it just kicked my son of the PC after his alloted hour and a half, so it’s functional – YMMV – I need to allow for different time limits on the weekends, and it uses a less than elegant method for shutting down gnome-session, so it’s very much a work in progress.

Comments (23)

Hi, I was looking for a program that does exactly this, and I found something called timeoutd in the repos as well. I was wondering if you have seen this or if you have made any changes to your script since the last update.

I had tried timeoutd – one of the problems I found with it is that it doesn’t seem to give any warnings to the desktop when it is about to kill your session. I does fit a real need, it can limit console and shell sessions as well, but it doesn’t really fit my need/want as well.

My script is in active use at our house and has been effective in limiting my teens computer time – that being said, I really need to add a GUI so my wife can control their time allotments and I would like to add some kind of a reward system where Mom can give them extra time to use when they want when they have earned it.

Hi Chris, I loved this script. I’ll put it to use as is to do the same need you mentioned.

But I’ll also try to upgrade it, here is a list of functionalities I believe we can improve:

Must Have
1) Different limitations per day of the week
2) Limit not only by seconds in use, also by specific times of the day, it would be like the user connects but has only 30 seconds to logout (or will be forced logout) if he is not inside a allowed window.
3) Hability to extend the allowed time for the current day if an administrator types in a password and how many time to extend (this would attend your need to give extra time as reward).

Good to Have
4) A graphical interface to administer these options
5) Prevent the user to manually change his control file to “obtain” extra time.
6) A better logout method, something that pushes a logout allowing to save data
7) Support for different languages, best if it is done by plug ins (will allow anyone to create a plug in)
8) Create a control window for the user, so he can check the remaining time at any time and giving him some options, could be the ability to use extra credit (off course reducing the credit from a pool)
9) Create a .deb package

I personally don’t have much available time, but I also need to ensure proper usage for my teenagers (that is thought) so I will try, first thing first, I will translate the script to Brazilian-Portuguese as I am from Brazil and post it for anyone else to use.

Thanks for the ideas, wagnerpereirasp! I have just started to play with pygtk to see how easy a UI would be and have been thinking about moving the time and limits data to sqlite3.
I currently limit access to the data files by making them only editable but root or an sudoer – there should be a better way. The script has to run as root to force shutdown someone anyone’s session. I wish there was a cleaner way to shutdown gnome-session other than just killing it, but I haven’t found one yet.

A system tray applet that displays the remaining time and can launch the time editor (after a gksudo login) sounds like a great idea!

Translations is also a great idea – I am afraid I’m personally limited to English, so any help is appreciated!

Wow! I really want to thank you for this. I’ve been looking for EXACTLY this script at the request of my wife. She had my back against the wall and was going to make me install windows so that NetNanny could be installed. I sure hope this works as described on my system. Life will be good again. Thanks. I use Ubuntu 8.04.1

bucknasty:
Yes, there is a bit of a bug that allows one loop or so worth of time before checking for live sessions so, 20 – 40 seconds passes before a new login gets kicked.
The code depends on a utility called ‘notify-send’ which I think is in the libnotify package on Fedora and Ubuntu. Without this, notification area applet, and d-bus running, you don’t get alerts, but these are common defaults on current Ubuntu and Fedora systems.
If you still have issues, drop a message here – I have been needing to put some time into making this more useful to people other than myself, so the feedback helps.

I added the libnotify package and it worked out great. I also have a modified version of your script I’d like to send you. It limits the users login so that if they are logged out at 10pm (for instance), they can’t simply wait until midnight and start over. You can set the allowed time frame for usage to prevent sneaky kids.

I have one problem though. I added the line indicated above that should start the script upon reboot but it doesn’t restart. My youngest has already learned all she needs to do is reboot the computer and she has unlimited time (unless I login and launch the script).

#!/bin/bash
###
# timekpr.sh – simple
# watches gnome sessions and logs them out once the user has exceeded a set, per day limit
# /var/lib/timekpr/$username.time hold a count of seconds user has had a gnome session
# /var/lib/timekpr/$username hold the daily allowed seconds for the user
#
# you may need to install notify-send with: $apt-get install libnotify-bin
#
# Copyright 2008 Chris Jackson
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# See .
#

default_limit=86400 # All day, new users is given this value
grace_period=60 # how much time do we give to let the user log out?
poll_time=20 # How often the script should check the timelogs
from=7 # When is the users allowed to log in
to=22 # When is the users no longer allowed to be loged in

#Ubuntu uses alternatives so we look for x-session-manager instead of gnome-session
SESSION_MANAGER=x-session-manager

# find DBUS session bus for this session
DBUS_SESSION_BUS_ADDRESS=grep -z DBUS_SESSION_BUS_ADDRESS
/proc/$pid/environ | sed -e 's/DBUS_SESSION_BUS_ADDRESS=//'
# use it – give a warning, then another one 1/2 way through grace_period
XAUTHORITY=”$XAUTHORITY” DISPLAY=”$UDISPLAY” DBUS_SESSION_BUS_ADDRESS=”$DBUS_SESSION_BUS_ADDRESS”
notify-send –icon=gtk-dialog-warning –urgency=critical -t 10000 “$1″ “Your session is about to expire! You have $grace_period sec to save your work and logout.”
sleep $(($grace_period/2)) # FIXME: this gives other sessions a free grace_period added to their accounting

XAUTHORITY=”$XAUTHORITY” DISPLAY=”$UDISPLAY” DBUS_SESSION_BUS_ADDRESS=”$DBUS_SESSION_BUS_ADDRESS”
notify-send –icon=gtk-dialog-warning –urgency=critical -t 10000 “$1″ “Your session is about to expire! You have $(($grace_period/2)) sec to save your work and logout.”
sleep $(($grace_period/2)) # FIXME: this gives other sessions a free grace_period added to their accounting

## uncomment the following to brutally kill all of the users processes
sleep 10
pkill -u $username

## killing gnome-session should be more like:
#DISPLAY=”:0″ XAUTHORITY=”/tmp/.gdmEQ0V5T” SESSION_MANAGER=”local/wretched:/tmp/.ICE-unix/$pid” su -c ‘gnome-session-save –kill –silent’ $username
## but this can still leave processes to cleanup – plus it’s not easy to get SESSION_MANAGER

# Is the user allowed to be loged in at this time?
# We take it for granted that if they are allowed to log in all day ($default_limit) then
# they can log in whenever they want, ie they are normal users
if [ $(cat /var/lib/timekpr/$username) -ne $default_limit ]; then
# Are we out of hours?
if ( [ $(date +%k) -lt $from ] || [ $(date +%k) -ge $to ] ); then
# Has the user been given extended log in hours?
if [[ -e “/var/lib/timekpr/$username.allow” ]]; then
# Was the extended log in hours given today?
if [[ $(stat -c ‘%z’ /var/lib/timekpr/$username.allow | cut -c9,10) != $(date +%d) ]]; then
logOut “Only log in between $from and $to”
rm /var/lib/timekpr/$username.allow
fi
else
# User has not been given extended log in hours
logOut “Only log in between $from and $to”
fi
fi
fi

# Is the limit exeeded
if [[ $timekpr -ge cat /var/lib/timekpr/$username ]]
then
logOut “Daily time limit”
fi
done
# Wait a while before we check again
sleep $poll_time
done

The modifications were done by a helpful person (.nedberg) on the Ubuntu forums. I take no credit for his hard work. I wish I knew how to program / script but it’s way over my head. Thanks to people like you and .nedberg, Ubuntu is a joy to run.