-
Notifications
You must be signed in to change notification settings - Fork 1
/
limitime.sh
executable file
·34 lines (31 loc) · 1.21 KB
/
limitime.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/bin/bash
# this file is executed with root permissions every minute by cron
# so that users who belong to the admin group can change the created files
umask 002
# echo the $1 nth line of stdin
# if that line doesn't exist than echo $2
nth_or() { (cat; echo;)|head -n "$1" | tail -n 1 | sed 's/^\s*$/'"$2"'/'; }
for username in $(ls /home)
do
if [ -e "limits/$username" ] && # if a roule is defined for user
{ who | grep "^$username" > /dev/null; } # user is logged on
then
if [ ! -e "consumed/$username" ] ||
[ $(( $(date -d "$(date +%Y/%m/%d)" +%s) >
$(stat "consumed/$username" -c %Y) )) == 1 ] # consumed/$username was not modified today
then
echo 0 > "consumed/$username"
fi
weekday=$(date +%u)
# blank lines are interpreted as INFINITY (there are less than 9999min in a day)
timepermited=$(nth_or "$weekday" 9999 < "limits/$username")
timeconsumed=$(cat "consumed/$username")
if [ $((timeconsumed >= timepermited)) = 1 ]
then
# exit users session
pkill -u "$username" -KILL
else
echo $((timeconsumed + 1)) > "consumed/$username"
fi
fi
done