Note, that this recipe is not updated long time and could be outdated!
Got it.

Datorlaika ierobežošana

Šajā lapā ir aprakstīts, kā veikt datora lietošanas ierobežošanu, izmantojot standarta Linux komandas un Bash skriptu.

  1. Izveido /home/bin failu ar sekojošu saturu:

    #!/bin/bash
    # warning shown to user
    warning="Tavs datorlaiks beigsies pēc 1 minūtes!"
    # Ja pēc tam nerādās pieteikšanās logs, spied: \
    # Ctrl+Alt+F7 vai F8, F9, utt."
    # depending on crontab, set amount of time which should be added
    addtime=2

    # Get user, who runs xwindow session (assuming only one simultaneous X window user)
    who=$(who|grep -E "tty([7-9]|1[0-2])"|head -n1) # user info
    user=$(echo $who|cut -d" " -f1)   # current user
    if [ -z "$user" ];then
        user="nobody"
    fi
    #user="ada" # for testing only !!
    #term=$(echo $who|cut -d"(" -f2|cut -d")" -f1) # current terminal
    term=$(echo $who|awk '{print $2}') # current terminal
    cdate=$(date +"%F")
    # time for user logged in before
    daytime=$(grep -aE "^$cdate.*user:$user.*daytime:[0-9]+$" /var/log/timeout.log|tail -n1|awk -F "daytime:" '{print $2}'|awk '{print $1}')

    # Calculate total daytime
    daytime=$(expr $daytime + $addtime)

    check=true
    daylimit=120
    if [ "$(echo $user|grep -E 'nobody|ieva|odo|valdis')" != "" ]; then
        check=false
    fi

    if [ "$(echo $user|grep -E 'ubuntu')" != "" ]; then
        daylimit=1
    fi

    message="$(date +'%F %T') user:$user term:$term daytime:$daytime"
    # for debbuging purposes only:
    #echo addtime:$addtime
    #echo who:$who
    #echo user:$user
    #echo term:$term
    #echo cdate:$cdate
    #echo daytime: $daytime
    #echo check: $check
    #echo $message
    #exit 0

    if [ $check == true ]; then
    if [ $daytime -gt $daylimit ]; then
    message="$message, Limit exceeded"
    #export DISPLAY=$term
    export DISPLAY=:0.0 # assuming only one Xterm session
    #su - $user -c "/usr/bin/xmessage -display :0.0 -center \"$warning\" &" # old school, doesnt support UTF8
    su -c "zenity --warning --text=\"$warning\"" $user 2>/dev/null & # dialog window
    #su $user -l -c "notify-send -u critical \"$warning\" &" # notification
    sleep 60
    # check if banned user session is still active
    user1=$(who|grep -E "tty([7-9]|1[0-2])"|head -n1|cut -d" " -f1)
    # and kill it, if necessary
    if [ "$user1" == "$user" ]; then
    #/etc/init.d/gdm restart # doesn't work
    /sbin/restart lightdm  # get off user from session by restarting xwindow manager
    /etc/init.d/lightdm restart # get off user from session by restarting xwindow manager
    message="$message, logged off"
    fi
    fi
    fi

    # Log
    echo $message >> /var/log/timeout.log
    exit 0
  2. Padara failu izpildāmu ar komandu:

    sudo chmod +x /home/bin/check-timeout
  3. Pēc vajadzības izmaina lietotāju vārdus un citus parametrus skriptā.
  4. Pievieno crontab uzdevumu:

    sudo crontab -e
  5. Redaktorā pievieno sekojošu rindu:

    */2 * * * * /home/bin/check-timeout
  6. Skripta darbību var pārbaudīt, skatot žurnāla failu /var/log/timeout.log:

    ...
    2013-06-01 20:04:01 user:audris term::0 daytime:88
    2013-06-01 20:06:01 user:audris term::0 daytime:90
    2013-06-01 20:08:01 user:audris term::0 daytime:92, Limit exceeded, logged off
    2013-06-01 20:10:01 user:nobody term: daytime:40
    2013-06-01 20:12:01 user:karlis term::0 daytime:42
    2013-06-01 20:14:01 user:karlis term::0 daytime:44
    2013-06-01 20:16:01 user:karlis term::0 daytime:46

    ...

Alternatīvas

Ja darbojas (jo šīs lapas rediģēšanas laikā nestrādāja neviens), tad varētu izmantot:

Skatīt arī


  
Tags Linux Serveris Darbstacija
Created by Valdis Vītoliņš on 2008-08-09 14:24
Last modified by Valdis Vītoliņš on 2021-04-13 14:30
 
Xwiki Powered
Creative Commons Attribution 3.0 Unported License