diff --git a/mcTimer.sh b/mcTimer.sh deleted file mode 100644 index cf8311d..0000000 --- a/mcTimer.sh +++ /dev/null @@ -1,46 +0,0 @@ -#!/bin/bash - -user=Milo # Milo | Ayana - -exec="/usr/bin/minecraft-launcher" -test -x "$exec" || ( -wget -O /tmp/Minecraft.deb https://launcher.mojang.com/download/Minecraft.deb & -dl="$!" -sudo aptitude update ; sudo aptitude upgrade -y -sudo aptitude install -y default-jre default-jdk -wait "$dl" -sudo apt install -y /tmp/Minecraft.deb -) - -notify="notify-send" -limit="3600" # seconds / day -logfile="$HOME/mc$user" - -today="$(date -I)" -start="$(date +%s)" -last="$(tail -n 1 $logfile)" -remaining="$(bc <<< "$limit - 0$(grep -q "$today" <<< "$last" && cut -d "T" -f 2 <<< "$last")")" - -test "$remaining" -le "0" && "$notify" "Temps dépassé pour aujourd'hui" && exit 1 - -function conclude() { - end="$(date +%s)" - - grep -q "$today" <<< "$last" && end="$(bc <<< "$(cut -d "T" -f 2 <<< "$last") + "$end"")" - - time="$(bc <<< "$end - $start")" - - echo "$start $end $time" - - echo "${today}T${time}" >> "$logfile" -} - -"$exec" & -pid="$!" - -( sleep "$remaining" ; "$notify" "Limite de temps atteinte. Extinction dans 1 minute" ; sleep 60 ; kill "$pid" ; conclude ) & -killer="$!" - -wait "$pid" -conclude -kill "$killer" diff --git a/timeout.sh b/timeout.sh new file mode 100644 index 0000000..577c66a --- /dev/null +++ b/timeout.sh @@ -0,0 +1,40 @@ +#!/bin/bash + +exec="/usr/local/bin/factorio" + +notify="notify-send" +limit="3600" #seconds / day +logfile="$HOME/.timeout-$(basename "$exec")" + +today="$(date -I)" +start="$(date +%s)" +last="$(tail -n 1 $logfile)" +remaining="$(bc <<< "$limit - 0$(grep -q "$today" <<< "$last" && cut -d "T" -f 2 <<< "$last")")" + +if test "$remaining" -le "0" ; then + "$notify" "Time limit exceeded for today" & + exit 1 +fi + +function conclude() { + end="$(date +%s)" + + grep -q "$today" <<< "$last" && end="$(bc <<< "$(cut -d "T" -f 2 <<< "$last") + "$end"")" + + time="$(bc <<< "$end - $start")" + + echo "${today}T${time}" >> "$logfile" +} + +"$exec" & + +while test "$(wc -l <<< "$pid")" -le 1 ; do + pid="$(pgrep factorio)" +done + +( sleep "$remaining" ; "$notify" "Time limit reached." "1 minute to extinction." & sleep 60 ; kill $(tr "\n" " " <<< "$pid") ; conclude ) & +killer="$!" + +wait "$(head -n2 | tail -n1 <<< "$pid")" +conclude +kill "$killer"