41 lines
914 B
Bash
41 lines
914 B
Bash
#!/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"
|