3 ms·
This is a good guide for Bash, but I wouldn't use the code as-is in production. For example, the following code is recommended for cleaning up a log file and le
by dotancohen 4y ago
This is a good guide for Bash, but I wouldn't use the code as-is in production. For example, the following code is recommended for cleaning up a log file and leaving only the last 50 lines:
tail -n $lines messages > mesg.temp
mv mesg.temp messages
That code has a clear race condition and will lose lines on a busy server.
- metadat 4y agoYes, use `mktemp', just as you would in Python, Go, Rust, C, or Java.
- metadat 4y agoActually, to protect against the clobber of the original file, you could use a flocker lock on the process to serialize the operations. Otherwise it will be unsafe.
- wswope 4y agoHow would you fix that race condition?
- formerly_proven 4y agoHave the writing process do it. Which kind of reminds me of that product running on a pseudo-distributed NoSQL database built out of text files with "record per line" structure and sort of a compacting garbage collector for these files to remove dead records. And there's just absolutely nothing that can go wrong with that.
- dotancohen 4y agoI would rotate the log file, and possibly store only the last log file.