Skip to content

Instantly share code, notes, and snippets.

@bgstack15
Last active March 14, 2018 12:18
Show Gist options
  • Save bgstack15/0bccc83672ac8ecdd5f6df0cc8e59511 to your computer and use it in GitHub Desktop.
Save bgstack15/0bccc83672ac8ecdd5f6df0cc8e59511 to your computer and use it in GitHub Desktop.
Delayed cleanup of temp files in shell
# Example script name: fetch
clean_fetch() {
# Delayed cleanup
if test -z "${FETCH_NO_CLEAN}" ;
then
nohup /bin/bash <<EOF 1>/dev/null 2>&1 &
sleep "${FETCH_CLEANUP_SEC:-300}" ; /bin/rm -r "${FETCH_TMPDIR:-NOTHINGTODELETE}" 1>/dev/null 2>&1 ;
EOF
fi
}
trap "__ec=$? ; clean_fetch ; trap '' {0..20} ; exit ${__ec} ;" {0..20}
FETCH_TMPDIR="$( mktemp -d )"
tmpfile1="$( TMPDIR="${FETCH_TMPDIR}" mktemp )"
tmpfile2="$( TMPDIR="${FETCH_TMPDIR}" mktemp )"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment