From 04fa68bfed8637e19292498e77fcfcc22e42cf39 Mon Sep 17 00:00:00 2001 From: Nick Chambers Date: Thu, 20 Oct 2022 03:00:10 -0500 Subject: [PATCH] Close the shell after 10 minutes of inactivity --- dotlib/cattle.sh | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 dotlib/cattle.sh diff --git a/dotlib/cattle.sh b/dotlib/cattle.sh new file mode 100644 index 0000000..8fdbc99 --- /dev/null +++ b/dotlib/cattle.sh @@ -0,0 +1,28 @@ +reset-elapsed() { + (( last_cmd_at = SECONDS )) +} + +ping-timer() { + kill -USR1 "$reaper_pid" 2>/dev/null +} + +kill-reaper() { + kill -KILL "$reaper_pid" +} + +{ + trap reset-elapsed USR1 + last_cmd_at=0 + + while sleep 1; do + (( elapsed = SECONDS - last_cmd_at )) + + if (( elapsed > 600 )); then + kill -KILL "$$" + exit + fi + done +} & + +reaper_pid=$! prompt_cmds+=(ping-timer) +trap kill-reaper EXIT