FedoraForum.org - Fedora Support Forums and Community
Results 1 to 5 of 5
  1. #1
    Join Date
    Jan 2018
    Location
    Stockton
    Posts
    2

    FSTRIM Cron Job?

    Hello, I'm new to Fedora, coming from using Ubuntu/Mint for the past few years. I followed the recommended partition scheme during install. I have a root partition, home partition, and a swap partition which are all lvm. My ssd is a samsung nvme type pcie ssd. From what I gather TRIM is not enabled by default in Fedora.

    I've added the noatime flag to all of the above mentioned partitions in my fstab, and I've created a daily cron job to call fstrim on all partitions. Is this an acceptable way to set up TRIM on Fedora?

  2. #2
    Join Date
    Dec 2013
    Location
    United Kingdom
    Posts
    5,872

    Re: FSTRIM Cron Job?

    daily is a bit aggressive but it's your choice. the easiest way to set TRIM is to use the timer service which sets TRIM to run once a week

    Code:
    systemctl enable fstrim.timer

  3. #3
    Join Date
    Jan 2018
    Location
    Stockton
    Posts
    2

    Re: FSTRIM Cron Job?

    Quote Originally Posted by antikythera
    daily is a bit aggressive but it's your choice. the easiest way to set TRIM is to use the timer service which sets TRIM to run once a week

    Code:
    systemctl enable fstrim.timer
    Thank you for the response. I work, and basically live on my computer, so it sees allot of use. I used to run trim once a week on Ubuntu, but noticed that I was having some I/O lag with the ssd. I switched over to daily trim since then and have seen improvements.

  4. #4
    Join Date
    Dec 2013
    Location
    United Kingdom
    Posts
    5,872

    Re: FSTRIM Cron Job?

    Quote Originally Posted by norcal618
    Thank you for the response. I work, and basically live on my computer, so it sees allot of use. I used to run trim once a week on Ubuntu, but noticed that I was having some I/O lag with the ssd. I switched over to daily trim since then and have seen improvements.
    fair enough, which I/O scheduler were you using with ubuntu? I currently use mq-deadline with my notebook's SSD (manjaro x86_64 kernel 4.14.13)

  5. #5
    Join Date
    Jun 2005
    Location
    Montreal, Que, Canada
    Posts
    4,981

    Re: FSTRIM Cron Job?

    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
    Last edited by lsatenstein; 15th January 2018 at 07:00 AM.
    Leslie in Montreal

    Interesting web sites list
    http://forums.fedoraforum.org/showth...40#post1697840

Similar Threads

  1. fstrim thoughts
    By lsatenstein in forum Hardware & Laptops
    Replies: 2
    Last Post: 2nd October 2017, 05:47 PM
  2. Is this fstrim result real?
    By dd_wizard in forum Using Fedora
    Replies: 1
    Last Post: 12th April 2017, 01:07 PM
  3. systemctl enable fstrim.timer command for trimming ssd?
    By Tugs4Life in forum Installation, Upgrades and Live Media
    Replies: 1
    Last Post: 29th January 2015, 05:33 AM
  4. Replies: 1
    Last Post: 8th April 2012, 05:54 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •