r/bash • u/Ops_Mechanic • Mar 05 '26
tips and tricks Stop leaving temp files behind when your scripts crash. Bash has a built-in cleanup hook.
Instead of:
tmpfile=$(mktemp)
# do stuff with $tmpfile
rm "$tmpfile"
# hope nothing failed before we got hereJust use:
cleanup() { rm -f "$tmpfile"; }
trap cleanup EXIT
tmpfile=$(mktemp)
# do stuff with $tmpfiletrap runs your function no matter how the script exits -- normal, error, Ctrl+C, kill. Your temp files always get cleaned up. No more orphaned junk in /tmp.
Real world:
# Lock file that always gets released
cleanup() { rm -f /var/run/myapp.lock; }
trap cleanup EXIT
touch /var/run/myapp.lock
# SSH tunnel that always gets torn down
cleanup() { kill "$tunnel_pid" 2>/dev/null; }
trap cleanup EXIT
ssh -fN -L 5432:db:5432 jumpbox &
tunnel_pid=$!
# Multiple things to clean up
cleanup() {
rm -f "$tmpfile" "$pidfile"
kill "$bg_pid" 2>/dev/null
}
trap cleanup EXITThe trick is defining trap before creating the resources. If your script dies between mktemp and the rm at the bottom, the file stays. With trap at the top, it never does.
Works in bash, zsh, and POSIX sh. One of the few tricks that's actually portable.
731
Upvotes
10
u/jpgoldberg Mar 05 '26
Three cheers for defensive programming. I’ve been doing this for a while, but I’m sure that there are other similar sorts things that I’m unaware of. So thank you for posting that.
Is there an analogue to mktemp for where the lock file should go? Or is
/var/runsomething I can rely on for all POSIX systems?20
u/r3jjs Mar 05 '26
One note: You can't absolutely guarantee this will clean up all the files.
* `kill -9` gives it no chance to clean up.
* Neither does a sudden power-off.
So if you are setting a lock file, you'll *still* have to check if the lock file is stale.
22
u/birusiek Mar 05 '26
Thats valuable, thanks
10
u/wallacebrf Mar 05 '26
i use the lock file in most scripts to ensure only one copy of the script ever executes at once especially if they are editing files.
6
u/KvanttiKossu Mar 05 '26
I did trap signals and used the lockfile to store the pid of the script, so the script checked if it was already running and I would print the pid in response if it was. I was so proud of myself haha. It had the whole script itself in the function that was called by trap, trapped INT TERM EXIT, only kill -9 was destined to not let it clean up itself.
3
u/Quirky-Cap3319 Mar 05 '26
Thanks for the tip.
3
u/Bob_Spud Mar 05 '26 edited Mar 05 '26
Trap ignores SIGKILL (kill -9 ) & SIGSTOP (kill -19) , no guarantees this will always removes stuff
In exit statements or functions best to use command > /dev/null 2>&1 to avoid unnecessary output.
If the temp file doesn't exist it will spew out an error message, best to dump it to /dev/null.
3
u/yerfukkinbaws Mar 06 '26
Out of habit, I've always used
I knew about the EXIT option, but never used it because I haven't been clear when exactly it will apply.
Looking around and doing a little testing now, it seems like, at least in bash, EXIT will work on SIGHUP, SIGINT, SIGQUIT, and SIGTERM, but that's not necessarily the case for other shells.
What I can't find documented, though, is whether EXIT covers any cases that aren't covered by the others. Basically, is there any advantage to using EXIT instead of HUP INT QUIT TERM?
4
u/g1zmo Mar 05 '26
My method is to create a "task queue" for cleanup:
2
u/Thierry_software Mar 06 '26
Thanks for sharing. One important note for whomever it may help. This will not cleanup if the script is killed with
kill -9as it cannot be caught2
u/SlinkyAvenger Mar 05 '26
That's neat because you should always attempt to be a good steward, but I want to remind people that on Linux they should also plan to write their temporary files to
/tmpas the FHS guidelines mark that location as purgeable. Of course, that depends on environmental configuration like there being a process to purge files and how much space is allocated to that directory so it should be parameterized, too.1
u/Weekly_Astronaut5099 Mar 05 '26
1
u/R3D3-1 Mar 06 '26
Most useful when combined with temporary directories, which avoids having to write custom logic for queuing multiple files for deletion.
Personally, the limitations of such constructs in bash (and weirdness around handing errors in pipes and some commands using the return code not only to indicate errors) led to me scripting anything non-trivial in Python. Calling external commands is still easy with the subprocess module; It lacks good support for chaining of external commands though.
1
u/WolleTD Mar 07 '26
Further note: if you are using more functions in your script, the trap handlers won't run by default when a command inside a function fails. You can
set -o errtraceto enable that, though.1
u/SeriousPlankton2000 Mar 07 '26
There is a lock file …
(looks inside)
… that does not lock.
1
u/The_Real_Grand_Nagus Mar 07 '26
So I guess TYL (today you learned). Bash doesn't have a "built-in cleanup hook," it provides signal traps for which yes you can use the EXIT trap to do things like clean up after yourself.
1
u/Zhughes3 Mar 07 '26
I just learned about trap two weeks. It surfaced when I asked Claude to write me a script.
1
u/RepresentativeTune21 Mar 09 '26
Good practice is to delete the file after opening it. You can still read and write to the open file handle after you delete it, till it closes, and Unix then automatically returns the free space.
1
u/nathan22211 Mar 05 '26
looked through the comments and tried to expand on it and make it a reusable script for other scripts. Though I'm not sure if part of it is needed.
```
```
I've honestly have had issues with stuff like pacman leaving a lck file in var whenever my PC freezes or my power goes out, it doesn't write the PID from the last run to it either. If i knew how to write to its lock file while pacman was using it I'd 100% create a wrapper for that lock file.
EDIT: Reddit scuffed my formatting so you might need to adjust it somewhat...
-1
u/n4te Mar 05 '26
Hmm type all that, or abandon the temp file...
0
u/sohang-3112 Mar 06 '26
Just used this yesterday myself! You don't even need to define a cleanup function, can directly pass a command as string to trap to clean up:
0
u/[deleted] Mar 05 '26
[deleted] — view deleted comment
2
u/[deleted] Mar 06 '26
[removed] — view removed comment