Parental Control: Limiting computer usage time

Forum rules
Before you post please read how to get help

Parental Control: Limiting computer usage time

Postby Pilosopong Tasyo on Fri Jul 22, 2011 11:20 am

Limiting computer usage time: a poor man's attempt at parental control

This tutorial came about due to a question asked in another thread. The whole algorithm was posted over 2 months ago and I was hoping the original poster in that thread wrote back of his progress. Unfortunately that has yet to happen so I can only imagine if that small project ever became a reality.

But anyway, if you are in a situation where, for example:

- multiple users share a single computer
- several of those users are minors, and
- you as the parent/concerned adult wish to limit their daily computer usage time

then this might interest you. The requirements and rules have already been laid out in the other thread, and the script will be based on it albeit there are some slight modifications in key places. I recommend reading the whole thread to get an understanding of what it's all about.

The script itself is short; several if-then-else statements. I made liberal use of comments to document the whole process. To install the script:

Create a text file in your home directory, filename is users_and_times.cfg. This configuration file contains a list of users and their corresponding time limits (in minutes). Below is an example list:

Code: Select all
# Configuration file containing usernames
# and their corresponding alloted time.
# Format: <login-name> <allocated minutes>

bob 30
samantha 45
john 60
jess 30
cora 30
candy 15
tony 120
patrick 15
anne 60

Open a terminal window (Mint menu > Terminal).

Code: Select all
gksudo gedit /root/

Key in your password.

Copy and paste the whole script listed below to the gedit window.

Code: Select all

# Project       : Poor man's parental control
#                 Limiting a user's daily computer usage time
# Started       : May 12, 2011
# Last Modified : July 22, 2011
# Author        : Anthony David (Pilosopong Tasyo)
# Module        :
# Description   : Monitors the amount of time a user spends on the computer.
#                 Lock the screen when the limit has been reached.  Run this
#                 script as a cron job (executed once per minute).

# Some useful variables
VICTIM=`users`       # Who's currently logged in?
ADMIN=administrator  # ADMIN = login name of the computer's administrator.

# Date- and time-keeping variables.
TODAY=`date +%D`

# Shortcuts to configuration files.

# The entire script relies on the fact that a user must be logged in.
# Any user -- root, the admininstrator, user1, user2, etc. If nobody
# is logged in, the script has nothing to do. So, exit.

# combined with...

# The administrator has unlimited access (consequently, so does root),
# thus the script doesn't need to run if admin/root is logged in.

if [ -z "$VICTIM" -o "$VICTIM" == "$ADMIN" -o "$VICTIM" == "root" ]

# Check if $VICTIM already have the two configuration files in root's
# directory. If neither one nor both are present, it is assumed that
# $VICTIM is a new user account, so create them on the fly.  Otherwise
# read the files and store the values to variables.

if [ ! -e "$ROLLOVER_DATE_FILE" -o ! -e "$TIME_LEFT_FILE" ]

# No longer necessary since it's been superseded by
# the else statement in STEP THREE

# If $TODAY and $YESTERDAY do not match, either a new day already has begun
# or STEP THREE happened. If this is the case, re-set the configuration files.

if [ "$TODAY" != "$YESTERDAY" ]
  # Find out the allocated time for $VICTIM.  If $VICTIM's not found
  # in the configuration file, defaults to zero.
  TIME_LEFT=`grep $VICTIM $USERS_AND_TIMES_FILE | awk '{print $2}'`
  if [ -z $TIME_LEFT ]


# Remind the VICTIM that he/she has $TIME_LEFT minute(s) left for the day.

sudo -u $VICTIM DISPLAY=:0.0 notify-send -i gtk-info \
  "Reminder:" "You have $TIME_LEFT minute(s) left for the day."

sleep 10  # It takes 10 seconds for notify-send to finish the OSD

# Find out if $VICTIM exhausted the allowed time limit.  If there's still
# $TIME_LEFT, decrease by 1 and store the new value in the configuration file.
# Otherwise lock the screen (or force an ungraceful logout).

if [ $TIME_LEFT -gt 0 ]
  TIME_LEFT=`expr $TIME_LEFT - 1`
  sudo -u $VICTIM DISPLAY=:0.0 gnome-screensaver-command --activate --lock
  # The command below will force an ungraceful logout -- not recommended!
  # sudo pkill -u $VICTIM

# We're done!


Make sure you edit the ADMIN=administrator line and put your login name there instead. Save and close the file.

Make the script executable

Code: Select all
sudo chmod u+x /root/

Edit root's cron table to run the script every minute

Code: Select all
sudo crontab -e

Code: Select all
# m h  dom mon dow   command
* * * * * /root/

And you're done. If you're going to add a new user in the future, don't forget to update the users_and_times.cfg file as well. Using the crontab setting listed above, the script will run all year round. If you want to make an exception, for example, run the script during certain hours of the day or during weekdays only, all you have to do is modify the contab to suit your needs.

I hope someone finds this useful. Regards!

o Give a man a fish and he will eat for a day. Teach him how to fish and he will eat for a lifetime!
o If an issue has been fixed, please edit your first post and add the word [SOLVED].
User avatar
Pilosopong Tasyo
Level 6
Level 6
Posts: 1356
Joined: Mon Jun 22, 2009 3:26 am
Location: Philippines

Linux Mint is funded by ads and donations.

Re: Parental Control: Limiting computer usage time

Postby Habitual on Fri Jul 22, 2011 11:40 am

Good stuff.
First things first:
Code: Select all
inxi -c 0 -Fsx

Code: Select all
search engine "keyword"
User avatar
Level 9
Level 9
Posts: 2897
Joined: Sun Nov 21, 2010 8:31 pm
Location: LM17Q-Xfce

Re: Parental Control: Limiting computer usage time

Postby dritzominous on Fri Jul 22, 2011 2:26 pm

Wow! :shock:

I'm so glad I didn't grow up with you as my parent. :lol:

Excellent script though, I learned a lot from that. Thank you!
User avatar
Level 3
Level 3
Posts: 159
Joined: Sat Nov 14, 2009 12:49 am

Return to Scripts & Bash

Who is online

Users browsing this forum: No registered users and 3 guests