SSD trimming as a cron job

I use XFS for my SSD and the partitions are mounted with default settings in /etc/fstab. Instead of using the discard option, I decided to set the fstrim command running as a cron job. As a side note, other filesystems that support trimming listed in the Arch Wiki are: Btrfs, Ext4, JFS.

How often shall the cron job run? In Ubuntu, it seems they run it weekly. This discussion about TRIM suggests that in order to prevent long-term performance degradation, running fstrim weekly is just fine. However, depending on the usage, more regular trimming may be needed. I searched a bit and found suggestions for running the TRIM command daily. To do this, I simply created a file in /etc/cron.daily called trim.sh. On the SSD, I have separate partitions for /home, /var and /tmp, so in my case that file should contain:

#!/bin/sh
fstrim /
fstrim /home
fstrim /tmp
fstrim /var

Another blog suggests to run fstrim in verbose mode (with the -v option) and log its output. In such case, the contents of the above file would be:

#!/bin/sh
LOG=/var/log/trim.log
echo "*** $(date -R) ***" >> $LOG
fstrim -v / >> $LOG
fstrim -v /home >> $LOG
fstrim -v /tmp >> $LOG
fstrim -v /var >> $LOG

I don’t think I need to have the output logged, but it is an option. Make trim.sh executable and it should run:

chmod +x /etc/cron.daily/trim.sh

Another approach is running fstrim more frequently, say twice a day. To do this, parameters should be specified by crontab -e. The default editor is set to vim, but something else can be used, such as nano. Run as root:

EDITOR=nano crontab -e

I modified a bit the example given by Gentoo, to trim each partition at a different time and not to be verbose:

#Mins  Hours  Days   Months  Day of the week   command
15     10,22   *      *       *                 /sbin/fstrim /
20     10,22   *      *       *                 /sbin/fstrim /home
25     10,22   *      *       *                 /sbin/fstrim /tmp
30     10,22   *      *       *                 /sbin/fstrim /var

In this way, fstrim will run every day in the mornings and evenings, starting with the root partition at 10.15 h and 22.15 h. The other partitions, /home, /tmp and /var will be trimmed successively, 5 min apart. May be it does not matter, but I decided not to trim all partitions at the same time. As the Gentoo Wiki points, if the computer is turned off when cron scheduled its job, fstrim would not be called at all. To go around this problem, they suggest the fstrimDaemon, but I have not tried it, yet. Instead I just specified times, when my computer is normally turned on. By running crontab -e, the time settings for the daily and other jobs can be tweaked, as well. For example, the daily jobs are set for 4:40h by default:

# Run hourly cron jobs at 47 minutes after the hour:
47 * * * * /usr/bin/run-parts /etc/cron.hourly 1> /dev/null
#
# Run daily cron jobs at 4:40 every day:
40 4 * * * /usr/bin/run-parts /etc/cron.daily 1> /dev/null
#
# Run weekly cron jobs at 4:30 on the first day of the week:
30 4 * * 0 /usr/bin/run-parts /etc/cron.weekly 1> /dev/null
#
# Run monthly cron jobs at 4:20 on the first day of the month:
20 4 1 * * /usr/bin/run-parts /etc/cron.monthly 1> /dev/null

This could be changed to whenever the computer is usually on.

Read the original sites first and decide what fits you best, yourself!


3 Comments on “SSD trimming as a cron job”

  1. Anonymous says:

    >As the Gentoo Wiki points, if the computer is turned off when cron scheduled its job, fstrim would not be called at all.

    This is with Gentoo. Slackware uses a better cron, if you put the following to the root’s crontab:
    @hourly ID=sys-hourly /usr/bin/run-parts /etc/cron.hourly 1> /dev/null
    @daily ID=sys-daily /usr/bin/run-parts /etc/cron.daily 1> /dev/null
    @weekly ID=sys-weekly /usr/bin/run-parts /etc/cron.weekly 1> /dev/null
    @monthly ID=sys-monthly /usr/bin/run-parts /etc/cron.monthly 1> /dev/null

    it will run all jobs even if you power off/on your computer. man has all details.

    (Also, usually people run fstrim once a day.)

  2. slackalaxy says:

    thanks for the hint!

  3. Nathalie says:

    This is a great post thankks


Leave a comment