Question
Modify the bash program below to do the following: 1) Determine the file size 2) Set a limit to check if the file exceeds 200
Modify the bash program below to do the following:
1) Determine the file size
2) Set a limit to check if the file exceeds 200 MB
3) Add a conditional to check if it exceeds 200 MB then zip it and create a new replacement log
4) The script should not accept any parameters. It should look in a logs folder (SOME-PATH/logs) and iterate the checking/zipping functionality over all logs in that folder.
#!/bin/bash
# 1. Looks for a log file
logfile=$1
if [ ! -f $logfile ]; then
echo "log file not found $logfile"
exit 1
fi
# 2. Checks the size of disk usage
df -H | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $5 " " $1 }' | while read output;
do
echo $output
usep=$(echo $output | awk '{ print $1}' | cut -d'%' -f1 )
partition=$(echo $output | awk '{ print $2 }' )
# 3. If above threshold 90% then creates a new file
if [ $usep -ge 90 ]; then
echo "Running out of space \"$partition ($usep%)\" on $(hostname) as on $(date)"
timestamp=`date +%Y%m%d`
newlogfile=$logfile.$timestamp
cp $logfile $newlogfile
cat /dev/null > $logfile
# 4. zip the old log file
gzip -f -9 $newlogfile
fi
done
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started