1

I would like to activate a cron job valid for specific users only (for my kids) that it will check if the kids use the pc more than e.g. 60 min per day and then poweroff system. This means that if the condition is true, pc shuts down and in case the user reboots then he/she is not able to login again. Thank you in advance.

Radu Rădeanu
  • 174,089
  • 51
  • 332
  • 407
bob
  • 1,198

2 Answers2

1

I use this on my brothers and sisters when they use my computer. Of course you would have to allot them 1 chunk of time and not segments but:

#!/usr/bin/env python3

import time
import os
import sys

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

Save it as kids.py, give it executive rights, put it in your home directory, don't tell them the user password and every time you need to use it hold down ALT + F2 and type ./kids.py seconds where seconds is the amount of time (in seconds) you wish for them to have. It will basically lock the screen after the time is up.

[EDIT] You could also place this in YOUR home folder, put it in each of their startup files, deny them the ability to change their startup files, remove "import sys", replace "int(sys.argv[1])" with the number of seconds you want and then change "gnome-screensaver-command -l" to "gnome-session-save --force-logout" give all users the ability to execute the file. haven't tried this so. . . [/EDIT]

KI4JGT
  • 1,888
1

You can add to run at start up for those specific users the following script (which is very close to this one here):

#!/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 ($1 * 3600) + ($2 * 60) + $3 }')

function countdown
{
    sec_left=$(echo $time_to_play_left | awk -F: '{ print ($1 * 3600) + ($2 * 60) + $3 }')
    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

Don't forget to make it executable:

chmod +x script_name

Important:

Radu Rădeanu
  • 174,089
  • 51
  • 332
  • 407