Some of my scripts for daily use on a linux desktop

Wednesday, March 31, 2010

Automatic logout for kids


I have a little problem with my little kid : when he plays on computer, I have to tell him at least 10 times to stop when his time is up !
So I wrote this conky (to use in Gnome) that will display remaining time and logout the session !
And after that, if he tries to login again, the session will run only 30 seconds ;-)

Of course, it's for a small kid who doesn't know commands like "killall conky" or tricks like that !

First, create a hidden folder in the kid session :
mkdir ~/.logout/

and create the next 3 files in the folder:
the conkyrc-logout

double_buffer yes
no_buffers yes

#own_window yes
own_window_type desktop
own_window_transparent yes
own_window_hints undecorate,sticky,skip_taskbar,skip_pager,below

border_inner_margin 0
border_outer_margin 0

# X font when Xft is disabled, you can pick one with program xfontsel
font 7x13

# Use Xft?
use_xft yes


# Xft font when Xft is enabled
xftfont monospace-18

# Text alpha when using Xft
xftalpha 0.5

# Draw shades?
draw_shades yes

# Stippled borders?
stippled_borders 0

# Default colors and also border colors
default_color yellow
default_shade_color blue

alignment br
gap_x 1
gap_y 1

# Add spaces to keep things from moving about? This only affects certain objects.
use_spacer right

# Subtract file system buffers from used memory?
no_buffers yes

TEXT
Stop in ${exec ~/.logout/time_left.sh}



The script time_left.sh which send remaining time to the conky :

#! /bin/bash

#st in seconds
st=$(~/.logout/read_time.sh)

RUNTIME=$((st))
SECOND=$((RUNTIME%60))
MINUTE=$((RUNTIME/60%60))
HOUR=$((RUNTIME/3600))

if [ "$SECOND" -lt 10 ]; then
SECOND="0$SECOND"
fi
if [ "$MINUTE" -lt 10 ]; then
MINUTE="0$MINUTE"
fi
if [ "$HOUR" -lt 10 ]; then
HOUR="0$HOUR"
fi

echo $HOUR":"$MINUTE":"$SECOND
if [ "$RUNTIME" -lt 0 ]; then
gnome-session-save --force-logout
fi




The script read_time.sh which read remaining time and save it in a file .counter.txt
The default time for a day is one hour (3600 secondes) on line 4.


#! /bin/bash

log=~/.logout/.counter.txt
duration=3600

if [ ! -f $log ]; then
echo "no file"
echo $(date +%Y%m%d)";"$duration > $log
fi

today=$(date +%Y%m%d)

reste=-1
line=`tail $log`

xday=$(echo $line | cut -d";" -f1)
if [ $xday -eq $today ]; then
reste=$(echo $line | cut -d";" -f2)
hasdate=true
if [ $reste -lt 0 ]; then
reste=30
fi
reste=$((reste-1))
else
reste=$duree
fi
echo $today";"$reste > $log

echo $reste



To run it at startup, add this line in gnome startup:
conky -c ~/.logout/conkyrc-logout

Voilà ! I'm sure there is other to do that, but I like the display of the remaining time...
Make sure the paths are OK for in the three files and don't forget to make the scripts executables


The script in french here.

No comments:

Post a Comment