Tuesday, August 19, 2008

How to Limit Daily Desktop Usage in Ubuntu

This is not my script but I'm placing it here for me and my family to reference. It comes from another blog called "Waggle Dance" The link to that bog is posted on the left hand side of this page under "My Links". All credit for the following scripts go to the author of the original timekpr.txt, and to Ubuntu Forums member .nedberg for making the modifications and additional two scripts I requested in this thread.

Here's the original content.

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:
  1. My kids share a PC with their Mom using fast-user-switching, so I needed it to keep track of multiple sessions.

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

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

With those ideas in mind I came up with this:

timekpr - (Modified - this is not the creator's original version)
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.

Lets make sure you have the notifier libs installed so that you can get the nice graphical notifications that your user is about to be kicked to the curb. Open up a terminal and paste

sudo apt-get install libnotify-bin

To install it, down load the script, timekpr , and rename it. Copy it someplace useful and make it executable:

mv timekpr.txt timekpr.sh
chmod 755 timekpr.sh
sudo mv timekpr.sh /usr/local/bin

Next make a directory for the time tracking files and limits:

sudo mkdir /var/lib/timekpr

I added a line to /etc/rc.local

/usr/local/bin/timekpr.sh &

to start the script after a re-boot.

To limit someone’s per day usage, just add the number of seconds they will be allowed to a file in /var/lib/timekpr

sudo -i
echo 7200 > /var/lib/timekpr/username
where username is the account you want to time limit.


This is a new version and needs a new config file for each userername located at:


To use the new script, login to a terminal with root permissions and delete the old script AND all the old user configuration files and REBOOT your system. Then install the new script and create all new username config files. Then paste the settings (as shown below) into the newly created confg files. Reboot your system again, and the script should work it's magic provided you have added it to the: /etc/rc.local file as instructed above. If you don't create a configuration file for any user, they will have unrestricted access.

Each of the username files should have the following format:
limit=( 345 345 345 345 170 345 345 )
from=( 7 7 7 7 7 7 7 )
to=( 22 22 22 22 23 22 22 )

Each of the seven numbers on a line stands for the value of that certain day of week


An unrestricted user does not need this file, so don't make one for yourself.

To start the script without rebooting, open up a terminal session and type:
$ bash /usr/local/bin/timekpr.sh

You need to be sure that libnotify is installed. Type the following code in a terminal session.

$ sudo apt-get install libnotify-bin

Here are the scripts
- Notice the 2 extra scripts for added usefulness.

This is a modified version of the original. It lets you set additional time controls that prevent the user from being able to login outside of the allowable limits (7 & 22 by default) of 0700 & 2200 hours.

This is a "rewards" script to give additional time to your well behaved little users. This script (when ran) is only effective for the day you run it. Run this script in the following format:

sudo addtime.sh
This does not expand the login hours regular boundaries of 0700-2200 hours.

This script will allow you to set user privileges to extend beyond the default login boundaries of 0700 to 2200 hours. This is only effective for one session and will reset when the timekpr.sh script is next executed. To run this script, use the following format:

sudo extendlimits.sh

What's ahead?
.nedberg of the Ubuntu Forums is currently working on a GUI frontend for this script. Here is a preview of the work as it stands.


Anonymous said...

You and .nedberg saved me one h**l of a migrane and who knows how many of the nerves I have left.

10 thousand thank yous to you both!

Bucknasty said...

I'm glad it was helpful to you. Stay tuned for further developments.