Skip to content

Instantly share code, notes, and snippets.

@LieBtrau
Last active July 12, 2024 16:09
Show Gist options
  • Save LieBtrau/21c9c752306cbd5a05a3df6e0ea61be0 to your computer and use it in GitHub Desktop.
Save LieBtrau/21c9c752306cbd5a05a3df6e0ea61be0 to your computer and use it in GitHub Desktop.
Log user out of Ubuntu session when the sesion duration has been exceeded or total logon time for today has been exceeded.
#!/bin/bash
function current_session_time() {
# Get the duration of the current logon session in seconds
username="$1"
info=$(last -Fn1 $username)
# Get the current time and the session time in seconds
current_time=$(date +%s)
session_time=$(date -d "$(echo $info | awk '{print $5, $6, $7, $8}')" +%s)
# Calculate the difference between the current time and the session time
difference=$((current_time - session_time))
}
function get_time_previous_sessions() {
# Get the total time of previous logon sessions in seconds.
username="$1"
info=$(last -F --since today $username)
# Example output:
# miguel tty3 tty3 Sun Jul 7 16:14:53 2024 still logged in
# miguel tty3 tty3 Sun Jul 7 14:07:12 2024 - Sun Jul 7 15:03:10 2024 (00:55)
# miguel tty3 tty3 Sun Jul 7 14:01:10 2024 - Sun Jul 7 14:06:59 2024 (00:05)
# miguel tty3 tty3 Sun Jul 7 09:00:17 2024 - Sun Jul 7 14:00:59 2024 (05:00)
# Get the time (xx:xx) from each of the following lines
times=$(echo "$info" | grep -o '([0-9][0-9]:[0-9][0-9])')
# Calculate the total time in seconds from the session durations
for time in $times; do
hours=$(echo $time | cut -d: -f1)
minutes=$(echo $time | cut -d: -f2)
total_time=$((total_time + ($hours * 3600) + ($minutes * 60)))
done
}
#!/bin/bash
function log_user_out() {
# Notify the user that the session will be logged out
username="$1"
export DISPLAY=$username
export XAUTHORITY=/home/$username/.Xauthority
export XDG_RUNTIME_DIR=/run/user/$(id -u $username)
/usr/bin/notify-send 'Je zit te lang op de computer.'
sleep 3
# Users still have the option here to cancel the logout
#gnome-session-quit --logout
# Force user logout
pkill -SIGKILL -u $(whoami)
}
#!/bin/bash
# Log the user out if the current session duration has been exceeded.
# Provide the full path to the functions file, otherwise cron will not find them
source "$HOME"/cron_jobs/logout.sh
source "$HOME"/cron_jobs/functions.sh
#set -x
duration_limit=1800 # 30 minutes
current_session_time $(whoami)
echo $(date -u) "Current session duration: $difference seconds"
# Check if the session duration exceeds the limit
if (($difference > $duration_limit)); then
echo $(date -u) "Session duration exceeds the limit of 30 minutes"
# Log the user out
log_user_out $username
fi
#!/bin/bash
# Log the user out if the total login time today has been exceeded.
# Provide the full path to the functions file, otherwise cron will not find them
source "$HOME"/cron_jobs/logout.sh
source "$HOME"/cron_jobs/functions.sh
#set -x
username=$(whoami)
total_time_allowed=3600 # One hour a day allowed
current_session_time $username
get_time_previous_sessions $username
total_time=$((difference + total_time))
echo $(date -u) "Total time logged in today: $total_time seconds"
# Check if the session duration exceeds the limit
if (($total_time > $total_time_allowed)); then
echo $(date -u) "User has been logged in for more than one hour today."
# Log the user out
log_user_out $username
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment