Hi norcal618
I run fstrim once a week from a crontab
This is my script
Code:
#!/bin/bash
# run fstrim either once/week or once/day not once for every reboot
# To run every other week, determine the week number using the
# number of seconds since 1 Jan 1970,
#
# use the date function to extract today's day number or week number
# the day number range is 1..366, weekno is 1 to 53
#WEEKLY=0 #set to 0 for once per dau
WEEKLY=1 #set to 1 for once per week
#EEKLY=2 #set to 2 for every second week
lockdir='/scratch/dofstrim/lock/' #you can replace with /root/lock if you only have 1 system installed.
if [[ $WEEKLY -eq 1 ]]; then
dayno="$lockdir/dofstrim.weekno"
number=$(date +%U)
else
if [[ $WEEKLY -eq 0 ]]; then
dayno=$lockdir/dofstrim.dayno
number=$(date +%j)
else
if [[ $WEEKLY -eq 2 ]]; then
number=$(date +%s) #seconds since
number=$(expr $number / 604800 ) #weeks since 1/1/1970 (86400 * 7)
echo "number of weeks since 1970/01/01 = $number"
number=$(expr $number % 2 ) #remainder multiple of 2 weeks
if [[ $number == 0 ]];then
echo "week number is even"
else
echo "week number is odd"
fi
exit #decide what you want to do for every other week.
fi
fi
fi
prevval="000"
if [ -f "$dayno" ]
then
prevval=$(cat ${dayno} )
else
mkdir -p $lockdir
fi
if [ ${prevval} -ne ${number} ]
then
sudo /sbin/fstrim -a
echo $number > $dayno
fi
For root's crontab, I have two entries.
@reboot /usr/local/bin/dofstrim.sh
@daily /usr/local/bin/dofstrim.sh
The dofstrim.sh writes the week number into a file
when it is invoked via @reboot or @daily, it obtains the current week number and compares it to the one on file.
If there is a change, the new week number is written to the file and the fstrim -a is executed.
If you also have a hard disk, you may want to consider moving /var from the SSD to a 4 gig partition on that hard disk. Most system tmp files are written to a subdirectory below /var. Some say to also relocate swap, but when I last checked, my swap file was only used if I did a suspend for the night, and a resume in the morning.
If you have 8 gigs ram, I was told that the suspend will write to a tmpfs (ram disk). I have a 128gig SSD for Fedora, I keep 15% (roughly 15gigs as an unallocated partition. That protects the SSD internals from thrashing (eg too soon reusing the same 4k page). My crontab entries and dofstrim.sh allows me to easily go 1 week between fstrim's.
Week=0 sets up a daily fstrim
Week=1 sets up a weekly fstrim
week=2 I left as future. It calculates a week number using seconds since Jan 1 1970
If you dual boot (more than 1 linux on your desktop), put the "lock" file to a directory common to both/all systems.
My /scratch/dofstrim/lock is common to my three Linux systems on my desktop. Does not matter from which I boot, fstrim is run only once per 7 days.
If you are using gnome then
test this
within ~/.config/autostart/dofstrim.sh
Code:
[Desktop Entry]
Type=Application
Name=fstrim to run weekly
Exec=/usr/local/bin/dofstrim.sh
Everytime you log in, the dofstrim.sh script is executed. To be honest, I did not as yet test this gnome autostart