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
Code: Select all
gksudo gedit /root/limit-usage-time.sh
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 the script executable
Code: Select all
sudo chmod u+x /root/limit-usage-time.sh
Code: Select all
sudo crontab -e
Code: Select all
# m h dom mon dow command
* * * * * /root/limit-usage-time.sh
I hope someone finds this useful. Regards!
PT