Quick Linux Tip #119:
Question: My config reload reads a half-written file. How do I update it safely?
Try: mv /etc/myapp/config.new /etc/myapp/config.json
Info: Same-filesystem file renames are atomic at the inode level, ensuring readers see either the complete old file or the new file.
Examples:
- $
sed -i.bak 's/old/new/' file# Perform an in-place file edit with an automatic backup - $
python3 -c 'import os; os.rename("new.conf", "live.conf")'# Execute an atomic rename using Python - $
install -m 644 new.conf /etc/live.conf# Safely copy a new configuration file while enforcing permissions
Note: Atomicity only applies within the same filesystem (cross-filesystem moves fall back to a copy-and-delete operation). Writing to a temporary file and renaming it is the standard safe pattern for configs and databases.
Leave a Reply