Knowledge Base

Preserving for the future: Shell scripts, AoC, and more

Delayed cleanup of temp files for shell script

If you want the temp files from your script to be left around for a few minutes, you can use a few shell tricks to remove a temp directory a few minutes later. https://gist.github.com/bgstack15/0bccc83672ac8ecdd5f6df0cc8e59511 If the environment variable FETCH_NO_CLEAN has any content at all, the script will not clean up at all. The script sends a heredoc to a separate, nohupped shell instance. The commands are to wait so many seconds (default of 300 seconds), and then remove the directory. To ensure the cleanup function is called, set a trap on the most common exit codes (0 through 20) to call the function, unset the traps, and then exit. Then you can define a tempdir, and use it to make some temp files. Using the TMPDIR variable is safe on FreeBSD mktemp, but bash will be /usr/local/bin/bash.

Comments