Parental Control: Limiting computer usage time

Forum rules
Before you post please read this

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:

/home/administrator/users_and_times.cfg
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/limit-usage-time.sh

Key in your password.

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

/root/limit-usage-time.sh
Code: Select all
#!/bin/bash

#####
#
# 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        : limit-usage-time.sh
# 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`
YESTERDAY=0
TIME_LEFT=0

# Shortcuts to configuration files.
ROLLOVER_DATE_FILE=/root/$VICTIM-rollover-date.cfg
TIME_LEFT_FILE=/root/$VICTIM-time-left.cfg
USERS_AND_TIMES_FILE=/home/$ADMIN/users_and_times.cfg

# STEP ONE
# 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...

# STEP TWO
# 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" ]
then
  exit
fi

# STEP THREE
# 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" ]
then
  echo $YESTERDAY > $ROLLOVER_DATE_FILE
  echo $TIME_LEFT > $TIME_LEFT_FILE
else
  YESTERDAY=`cat $ROLLOVER_DATE_FILE`
  TIME_LEFT=`cat $TIME_LEFT_FILE`
fi

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

# STEP FIVE
# 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" ]
then
  # 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 ]
  then
    TIME_LEFT=0
  fi

  echo $TODAY > $ROLLOVER_DATE_FILE
  echo $TIME_LEFT > $TIME_LEFT_FILE 
fi

# STEP SIX
# 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

# STEPs SEVEN and EIGHT
# 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 ]
then
  TIME_LEFT=`expr $TIME_LEFT - 1`
  echo $TIME_LEFT > $TIME_LEFT_FILE
else
  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
fi

# We're done!

# EOF

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/limit-usage-time.sh

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/limit-usage-time.sh

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!

PT
Image
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: 1101
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.
What's a landing but a take off in reverse?
My DorkBlog
User avatar
Habitual
Level 7
Level 7
 
Posts: 1999
Joined: Sun Nov 21, 2010 8:31 pm
Location: Slackware

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
dritzominous
Level 3
Level 3
 
Posts: 160
Joined: Sat Nov 14, 2009 12:49 am


Return to Scripts & Bash

Who is online

Users browsing this forum: No registered users and 1 guest