0
0
Bash Scriptingscripting~10 mins

Log rotation script in Bash Scripting - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Log rotation script
Start script
Check if log file exists
Yes
Check log file size > limit?
NoExit script
Yes
Rename current log file to backup
Create new empty log file
Exit script
The script checks if the log file exists and is too big, then renames it and creates a new empty log file.
Execution Sample
Bash Scripting
#!/bin/bash
LOGFILE="app.log"
MAXSIZE=1000
if [ -f "$LOGFILE" ] && [ $(stat -c%s "$LOGFILE") -gt $MAXSIZE ]; then
  mv "$LOGFILE" "$LOGFILE".1
  touch "$LOGFILE"
fi
This script checks if 'app.log' exists and is larger than 1000 bytes, then renames it to 'app.log.1' and creates a new empty 'app.log'.
Execution Table
StepCheckCondition ResultActionOutput
1Does 'app.log' exist?YesProceed to size check
2Is size of 'app.log' > 1000 bytes?YesRename 'app.log' to 'app.log.1'
3Create new empty 'app.log'N/Atouch 'app.log'
4End of scriptN/AExit
💡 Script ends after creating new empty log file.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
LOGFILE"app.log""app.log""app.log""app.log""app.log"
MAXSIZE10001000100010001000
app.log fileexists, size > 1000 bytesexists, size > 1000 bytesrenamed to app.log.1new empty app.log creatednew empty app.log
Key Moments - 3 Insights
Why do we check if the log file exists before checking its size?
Because checking the size of a non-existent file would cause an error. The execution_table row 1 shows we first confirm the file exists.
What happens if the log file size is not greater than the limit?
The script skips renaming and creating a new file and exits immediately, as shown in execution_table row 2 where condition would be No.
Why do we use 'touch' after renaming the log file?
To create a new empty log file so the application can continue logging without errors. This is shown in execution_table row 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the action taken at step 2?
ARename 'app.log' to 'app.log.1'
BCreate new empty 'app.log'
CExit script
DCheck if log file exists
💡 Hint
Refer to execution_table row 2 under 'Action'
At which step does the script create a new empty log file?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at execution_table row 3 under 'Action'
If the log file does not exist, what will the script do?
ARename the log file
BExit without any action
CCreate a new empty log file
DThrow an error
💡 Hint
Check execution_table row 1 condition result and flow
Concept Snapshot
Log rotation script in bash:
- Check if log file exists
- Check if file size > limit
- If yes, rename old log file
- Create new empty log file
- Exit script
Use 'stat -c%s' for size and 'mv' to rename.
Full Transcript
This bash log rotation script starts by checking if the log file 'app.log' exists. If it does, it checks whether the file size is greater than the maximum allowed size (1000 bytes). If the file is too large, the script renames the current log file to 'app.log.1' to keep it as a backup. Then it creates a new empty 'app.log' file so the application can continue logging without interruption. If the file does not exist or is not too large, the script exits without making changes. This process helps manage log files by preventing them from growing too large and consuming too much disk space.