Desligamento automático se o temporizador de uso exceder o limite diário

1

Gostaria de ativar um cron job válido somente para usuários específicos (para meus filhos) que verificará se as crianças usam o computador mais do que, por exemplo, 60 min por dia e, em seguida, sistema de desligamento. Isso significa que, se a condição for verdadeira, o PC será encerrado e, caso o usuário seja reinicializado, ele não poderá efetuar login novamente. Obrigado antecipadamente.

    
por bob 10.09.2013 / 07:48

2 respostas

1

Você pode adicionar para executar na inicialização desses usuários específicos o seguinte script (que é muito próximo de este aqui ):

#!/bin/bash

#timelimit - Set daily time limits for a user

#Licensed under the standard MIT license:
#Copyright 2013 Radu Rădeanu (https://askubuntu.com/users/147044/).
#Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
#The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE

export DISPLAY=:0

time_to_play_daily="0:60:00"  # channge as you wish; the format is: H[:M[:S]]
file="$HOME/.count_time" 

if [ -a $file ]; then
    if [ "$(head -1 $file)" != "$(date +%D)" ]; then
        echo $(date +%D) > $file
        echo $time_to_play_daily >> $file
    fi
else 
    touch $file
    echo $(date +%D) >> $file
    echo $time_to_play_daily >> $file
fi

time_to_play_left=$(sed -n '2p' $file)

sec_left=$(echo $time_to_play_left | awk -F: '{ print ( * 3600) + ( * 60) +  }')

function countdown
{
    sec_left=$(echo $time_to_play_left | awk -F: '{ print ( * 3600) + ( * 60) +  }')
    local start=$(date +%s)
    local end=$((start + sec_left))
    local cur=$start

    while [[ $cur -lt $end ]]; do
        cur=$(date +%s)
        sec_left=$((end-cur))
        time_to_play_left="$((sec_left/3600)):$(((sec_left/60)%60)):$((sec_left%60))"
        sed -i "2s/.*/$time_to_play_left/" $file
        sleep 1
    done
}

if [ $sec_left -gt 0 ]; then
    notify-send -i "Time left to play for today: $time_to_play_left"
else
    notify-send -i "error" "Your time to play has finished for today!"
    sleep 3
fi

countdown $time_to_play_left

sudo poweroff 
# or, only for logout you can use:
# gnome-session-quit --logout --no-prompt

Não se esqueça de torná-lo executável:

chmod +x script_name

Importante:

por Radu Rădeanu 10.09.2013 / 10:52
1

Eu uso isso em meus irmãos e irmãs quando eles usam meu computador. É claro que você teria que distribuir um pedaço de tempo e não segmentos, mas:

#!/usr/bin/env python3

import time
import os
import sys

time.sleep(int(sys.argv[1]))
os.system("gnome-screensaver-command -l")

Salve-o como kids.py, dê a ele direitos executivos, coloque-o em seu diretório pessoal, não informe a senha do usuário e, sempre que precisar usá-lo, mantenha pressionada a tecla ALT + F2 e digite ./kids.py segundos onde segundos é a quantidade de tempo (em segundos) que você deseja que eles tenham. Basicamente, bloqueará a tela depois que o tempo acabar.

[EDITAR] Você também pode colocar isso em sua pasta home, colocá-lo em cada um dos seus arquivos de inicialização, negar-lhes a capacidade de alterar seus arquivos de inicialização, remover "importar sys", substituir "int (sys.argv [1])" com o número de segundos que você quer e, em seguida, altere "gnome-screensaver-command -l" para "gnome-session-save --force-logout" fornecer a todos os usuários a capacidade de executar o arquivo. não tentei isso. . . [/ EDIT]

    
por KI4JGT 10.09.2013 / 09:09