Last active
November 20, 2016 22:11
-
-
Save ebirn/c2d8ada655da29d0e2a2b89f05c17381 to your computer and use it in GitHub Desktop.
auto remove directory after SIGNAL or exit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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