Skip to content

Instantly share code, notes, and snippets.

@salewski
Last active June 30, 2024 16:17
Show Gist options
  • Save salewski/008f75c4a7c572168630fa0fec995959 to your computer and use it in GitHub Desktop.
Save salewski/008f75c4a7c572168630fa0fec995959 to your computer and use it in GitHub Desktop.
cron/anacron-suitable fstrim(8) invocation
#!/bin/bash -
# SPDX-FileCopyrightText: <text>
# © 2023, 2024 Alan D. Salewski <[email protected]>
# </text>
#
# SPDX-License-Identifier: GPL-2.0-or-later
# This file has been saved as a GitHub Gist here:
#
# https://gist.github.com/salewski/008f75c4a7c572168630fa0fec995959
# fstrim.cron: Wrapper around the fstrim(8) command, suitable for including in
# a crontab.
#
# On a Debian-based system, the root user can copy this file into one of the
# existing system-level directories for periodic invocation:
#
# # cp -pi /path/to/fstrim.cron /etc/cron.monthly/LOCAL-fstrim
# OR:
# # cp -pi /path/to/fstrim.cron /etc/cron.weekly/LOCAL-fstrim
#
# From there, cron(8) (or possibly anacron(8), if that is installed) will
# invoke the fstrim(8) program to attempt to discard (or "trim") blocks which
# are not used by the filesystem. Such an operation is useful for solid state
# drives (SSDs), which are likely configured at the file system level to /not/
# issue discard/TRIM commands to the underlying block device when block are
# freed.
#
# On Debian-based systems, the 'fstrim' command is provided by the
# 'util-linux' package.
#
# See also:
#
# * ext4(5) -- 'discard' mount option
#
# * /etc/lvm/lvm.conf -- 'devices.issue_discards' option
#
# * cryptsetup(5) -- 'discard' option for /etc/crypttab entries
#
# * wikipedia page: "Trim (computing)"
# https://en.wikipedia.org/wiki/Trim_(computing)
#declare -r prog='fstrim.cron'
declare -r fstrim_prog='/usr/sbin/fstrim'
# This will attempt to issue discard/TRIM commands for all blocks which are
# not used by the currently mounted file systems.
#
"${fstrim_prog}" --all "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment