Bash trap: Clean Up Temp Files and Locks on Exit
Temp files and stale locks left behind by failed scripts are avoidable. Tested trap patterns for EXIT, INT, TERM and ERR, plus mkdir and flock locks.
Topic
Temp files and stale locks left behind by failed scripts are avoidable. Tested trap patterns for EXIT, INT, TERM and ERR, plus mkdir and flock locks.
while IFS= read -r line is the correct loop, but the last line, pipes, commands that read stdin and Windows line endings each have a trap. Here they are, tested.
One missing slash nests a directory. One empty source wipes a backup. Tested rsync recipes for mirroring, filtering, verifying and copying over SSH.
Why a volume shows the image's files and a bind mount hides them, and how a root container leaves files your own user cannot edit or delete. Four tested fixes.
How I use python -m venv on servers and laptops: one environment per project, pinned requirements, no activation in cron, and why .venv never goes into Git.
Find processes by their full command line in pure Bash by reading /proc/PID/cmdline, with the NUL bytes handled properly and the 2009 bugs fixed.
Test whether a directory is empty in pure Bash with nullglob and dotglob, handle hidden and unreadable directories, and know when find -quit is faster.