A script to use if you do not want to use
Code:
#!/bin/bash
# This is licensed under gpl3
# Author Leslie Satenstein
# Sometime in 2017
# Revised in 19 April 2019
#
# Take it and do with it what you want
#
# What it does
# ===========
# run fstrim either once / 4 weeks or every 2 weeks once/week or once/day
# use the date function to extract seconds since 1 Jan 1970 UTC time
# Not modified, 4 * 3600 is the offset to Eastern Std time. (not included in calcs)
# Divide as appropriate to get day NextTrimDayNo or weekNextTrimDayNo since.
# Linux starts 1 Jan 1970 which is a Thursday or weekday 4
# 29 December 1969 (1 Jan 1970 -4 days) to Sunday
# NOTE: The next trimday number is stored in the logfile
# If today's date + number of days between trim is greater than the stored value
# do the trim, else wait another day.
# Not more than 90 days between trims.
#
function doPutHelp()
{
local bn
bn=$( basename $0 )
echo -e "\n$bn"
echo "========"
echo "$bn -h for this message. $bn without an argument calls \"fstrim\" every Sunday."
echo "$bn [-v] nnn does a \"fstrim\" every nnn modulo days, based on the Sunday which immediately precedes 1 Jan 1970 ."
echo "$bn has a variable SundayOffset=0. If you want the modulo day to be Tuesday, assign SundayOffset=2 ."
echo "Default variable OPTION=7 The -v option displays the number of days to the next scheduled fstrim."
echo ""
exit 1
}
#======start ====== start ===== start ===== start ===== start =====#
#======start ====== start ===== start ===== start ===== start =====#
#======start ====== start ===== start ===== start ===== start =====#
#
# verbose==1 then indicate NextTrimDayNo of days to go
if [ "x$1" = "x-v" ];
then
verbose=1
shift
else
verbose=0
fi
#--------------- constants ----------------
UPPERBOUND=31
SundayOffset=0 #optional change. If =1, week starts Mon, =2.week starts Tue .. =6, Week starts Sat
SecondsPerDay=86400 #(24*60*60)
TimeZoneAdj=$(( 4*3600 )) #Eastern Time Zone Adj
todaysNo=$(( 4 - $SundayOffset + ( $TimeZoneAdj + $(date +%s) ) / SecondsPerDay ))
bn=$(basename $0 .sh)
logdir='/var/log'
log="$logdir/${bn}.log"
Fstrim=$(sudo which fstrim)
set -e
#------------ end constants ----------------
if [ "$#" != 0 ]; then
if [ "$1" = "-h" ]; then doPutHelp ; fi
OPTION=$(( $1 + 0 )) #if $1 not a NextTrimDayNo, x will be 0
if [ $OPTION == 0 ]; then doPutHelp ; fi
if [ $OPTION -ge $UPPERBOUND ];
then
OPTION=$UPPERBOUND
fi
else
#OPTION=1 #do dofstrim daily, Also reset the calc for next day.
OPTION=7 #do dofstrim every 7 days
#OPTION=14 #do dofstrim every 14 days
#OPTION=21 #do dofstrim every 21 days
#OPTION=28 #do dofstrim every 28 days
#OPTION=$UPPERBOUND #do dofstrim every UPPERBOUND days
fi
#
if [ -f "$log" ];
then
LogDayNo=$(sudo cat "$log" )
else
mkdir -p $logdir
LogDayNo=$(($todaysNo + 4 - $SundayOffset ))
sudo touch $log
sudo chmod 666 $log
sudo echo "$LogDayNo" > $log
fi
#
# 1 Jan 1970 is a Thursday
# Thursday is the 4th day of the week (Sunday=0)
# adjust todaysNo = NoDaysSince to previous Sunday
# timezone is UTC.
NextTrimDayNo=$(($todaysNo + $OPTION))
#echo "Before if: LogDayNo = $LogDayNo todaysNo=$todaysNo"
if [ $todaysNo -ge $LogDayNo ];
then
sudo touch $log #mark when last checked
#sudo $Fstrim -a
sudo chmod 666 $log
TimeZoneAdj=$(( 4*3600 )) #Eastern Time Zone Adj
todaysNo=$(( 4 - $SundayOffset + ( $TimeZoneAdj + $(date +%s) ) / SecondsPerDay ))
bn=$(basename $0 .sh)
logdir='/var/log'
log="$logdir/${bn}.log"
Fstrim=$(sudo which fstrim)
set -e
#------------ end constants ----------------
if [ "$#" != 0 ]; then
if [ "$1" = "-h" ]; then doPutHelp ; fi
OPTION=$(( $1 + 0 )) #if $1 not a NextTrimDayNo, x will be 0
if [ $OPTION == 0 ]; then doPutHelp ; fi
if [ $OPTION -ge $UPPERBOUND ];
then
OPTION=$UPPERBOUND
fi
else
#OPTION=1 #do dofstrim daily, Also reset the calc for next day.
OPTION=7 #do dofstrim every 7 days
#OPTION=14 #do dofstrim every 14 days
#OPTION=21 #do dofstrim every 21 days
#OPTION=28 #do dofstrim every 28 days
#OPTION=$UPPERBOUND #do dofstrim every UPPERBOUND days
fi
#
if [ -f "$log" ];
then
LogDayNo=$(sudo cat "$log" )
else
mkdir -p $logdir
LogDayNo=$(($todaysNo + 4 - $SundayOffset ))
sudo touch $log
sudo chmod 666 $log
sudo echo "$LogDayNo" > $log
fi
#
# 1 Jan 1970 is a Thursday
# Thursday is the 4th day of the week (Sunday=0)
# adjust todaysNo = NoDaysSince to previous Sunday
# timezone is UTC.
NextTrimDayNo=$(($todaysNo + $OPTION))
#echo "Before if: LogDayNo = $LogDayNo todaysNo=$todaysNo"
if [ $todaysNo -ge $LogDayNo ];
then
sudo touch $log #mark when last checked
#sudo $Fstrim -a
sudo chmod 666 $log
OPTION=$(( $OPTION + 0 )) # is OPTION NUMERIC
#echo "LOGDAYNO=$LogDayNo, OPTION=$OPTION, NEXTrimDayNo=$NextTrimDayNo Line 103"
sudo echo "$NextTrimDayNo" > $log
fi
#echo "LOGDAYNO=$LogDayNo, OPTION=$OPTION, NEXTrimDayNo=$NextTrimDayNo Line 106"
if [ $OPTION == 1 ]; #force reset
then
sudo echo "$todaysNo" > $log #force reset
echo "forced reset to $todaysNo"
verboss=1
fi
#echo "after adjustment: NextTrimDayNo = $NextTrimDayNo LogDayNo = $LogDayNo todaysNo=$todaysNo"
if [ $verbose == "1" ];
then
diff=$(( $LogDayNo - $todaysNo ))
if [ $diff == 0 ];
then
echo "Next fstrim will be done at next execution"
else
echo "Next trim in $diff days."
fi
if [ $diff != 1 ] & [ $OPTION != 1 ];
then
echo "To reset frequency: $0 1 "
fi
fi
exit 0
OPTION=$(( $OPTION + 0 )) # is OPTION NUMERIC
#echo "LOGDAYNO=$LogDayNo, OPTION=$OPTION, NEXTrimDayNo=$NextTrimDayNo Line 103"
sudo echo "$NextTrimDayNo" > $log
fi
#echo "LOGDAYNO=$LogDayNo, OPTION=$OPTION, NEXTrimDayNo=$NextTrimDayNo Line 106"
if [ $OPTION == 1 ]; #force reset
then
sudo echo "$todaysNo" > $log #force reset
echo "forced reset to $todaysNo"
verboss=1
fi
#echo "after adjustment: NextTrimDayNo = $NextTrimDayNo LogDayNo = $LogDayNo todaysNo=$todaysNo"
if [ $verbose == "1" ];
then
diff=$(( $LogDayNo - $todaysNo ))
if [ $diff == 0 ];
then
echo "Next fstrim will be done at next execution"
else
echo "Next trim in $diff days."
fi
if [ $diff != 1 ] & [ $OPTION != 1 ];
then
echo "To reset fstrim frequency: $0 1 "
fi
fi
exit 0
My crontab entries
Code:
@reboot /usr/local/bin/dofstrim.sh # runs on reboot but only runs Linux fstrim on Sundays
9 9 * * * /usr/local/bin/dofstrim.sh # runs on reboot to check if need to run Linux fstrim