Skip to content

Instantly share code, notes, and snippets.

@ebirn
Last active November 20, 2016 22:11
Show Gist options
  • Save ebirn/c2d8ada655da29d0e2a2b89f05c17381 to your computer and use it in GitHub Desktop.
Save ebirn/c2d8ada655da29d0e2a2b89f05c17381 to your computer and use it in GitHub Desktop.
auto remove directory after SIGNAL or exit
#!/bin/sh
export MEM_ROOT=/dev/shm
export MEM_DIR=$MEM_ROOT/$PBS_JOBID
# create directory per job
mkdir -p $MEM_DIR
# cleanup function
function mem_dir_cleanup {
# try to be extra careful with rm
if [[ $MEM_DIR == "$MEM_ROOT"* ]] ; then
echo "cleaning up memdir: $MEM_DIR"
rm --one-file-system --preserve-root -rf $MEM_DIR
else
echo "$MEM_DIR looks bad, not cleaning up"
fi
}
# do the cleanup, no matter how we exit (EXIT should catch all the signals)
trap mem_dir_cleanup EXIT PIPE
# now we can use MEM_DIR do whatever we want
# do some heavy work
sleep 30
# orderly cleanup should be the normal thing
mem_dir_cleanup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment