Many of my scripts work with temporary files, usually relative to the scripts directory1,
while at the same time using set -e to exit as soon as something fails.
In this scenario the script leaves behind these temporary files by default, which is not desirable.
We can however do a proper cleanup using the trap concept.
Often times we need the current date (and time) when scripting inside bash or other shells.
For example when creating a backup file or writing to a log.
Often times when writing scripts I want to reference files in the same directory,
but keep the script portable in case it is part of a git repository being checked out somewhere else
or just the folder getting moved.
Afterwards, we can create a logroatet configuration going in /etc/logrotate.d/mongodb.
/var/log/mongodb/mongod.log
{
rotate 5 # Keep the last 5 rotated logs, so 6 files including the currently active
size 10M # Rotate once the log reaches 10MB in size, depending on your envrionment you could instead use daily, weekly, monthly, etc
missingok # It's ok if the log file does not exist
create 0600 mongodb mongodb # Permissions and ownership for the roatetd logs
delaycompress # Don't compress on first rotation, so we have the current log and log.1 uncompressed
compress # Compress everything else
sharedscripts # Run this script only once and not for every matching file
postrotate # Script to run after rotating
/bin/kill -SIGUSR1 $(systemctl show --property MainPID --value mongod.service)
endscript # End of script
}
The postrotate script simply finds the PID of mongod.service: systemctl show --property MainPID --value mongod.service
and sends it a SIGUSR1 which is the signal MongoDB expects if you wish to rotate the log.