Skip to content

Instantly share code, notes, and snippets.

@marek-saji
Last active August 29, 2015 14:10
Show Gist options
  • Save marek-saji/d558f30372dabec955b5 to your computer and use it in GitHub Desktop.
Save marek-saji/d558f30372dabec955b5 to your computer and use it in GitHub Desktop.
Clean up after script ends (e.g. on ssh timeout)
#!/bin/bash
# Clean up after script ends (e.g. on ssh timeout)
TIMEOUT=3
HEARTBEAT=$(( TIMEOUT - 2 ))
TIMESTAMP_FILE=$( mktemp )
date +%s > ${TIMESTAMP_FILE}
nohup bash -c "
while sleep ${TIMEOUT}
do
TIMESTAMP=\$( cat ${TIMESTAMP_FILE} )
NOW=\$( date +%s )
if (( NOW - TIMESTAMP > ${TIMEOUT} ))
then
notify-send --hint=int:transient:1 -- OHAI
break
fi
done
" 2>/dev/null &
echo 'Kill me with ^C after some while'
while sleep ${HEARTBEAT}
do
NOW=$( date +%s )
echo $NOW > ${TIMESTAMP_FILE}
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment