How does fstrim.timer know when I week has passed if I power down
FedoraForum.org - Fedora Support Forums and Community
Results 1 to 5 of 5
  1. #1
    Join Date
    Jun 2004
    Location
    Maryland, US
    Posts
    7,512
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How does fstrim.timer know when I week has passed if I power down

    systemd runs the fstrim.timer weekly by default as set by:

    Code:
    ~ $> cat /usr/lib/systemd/system/fstrim.timer
    Code:
    [Unit]
    Description=Discard unused blocks once a week~ $> cat /usr/lib/systemd/system/fstrim.timer
    Documentation=man:fstrim
    
    [Timer]
    OnCalendar=weekly
    AccuracySec=1h
    Persistent=true
    
    [Install]
    WantedBy=timers.target
    What I don't know is, suppose I power my host off then come back later and power it on, does the systemd fstrim.timer just restart the clock at 7 days again? I suppose the idea is a powered off computer can't be reading, writing and deleting files on the SSD for the expended time so powered off time doesn't leave trimmable blocks on the SSD. But that tacks all that time before the previous trim to the next one (and that could be anything less than 7 days). So power off and restarting the machine sets the timer back to 7 days so the trim can really be delayed a lot more than expected even for up time.


    UPDATE: I just want to answer my own post for information I've found for anyone else that wants to know, the 'Persistent=true' handles this issue (see especially the bolded part)

    Code:
    Persistent=
    
          Takes a boolean argument. If true, the time when the service unit was last triggered is stored on disk.
    When the timer is activated, the  service unit is triggered immediately if it would have been triggered at
    least once during the time when the timer was inactive. This is useful  to catch up on missed runs of the service
    when the machine was off. Note that this setting only has an effect on timers configured with OnCalendar=
    Last edited by marko; 14th April 2019 at 09:58 PM.

  2. #2
    Join Date
    Jun 2005
    Location
    Montreal, Que, Canada
    Posts
    5,861
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: How does fstrim.timer know when I week has passed if I power down

    I decided to not rely on systemd.

    I wrote a script /dofstrim.sh that I run on reboot and at least once a day.
    The script takes either no argument or a number (of days).
    Essentially my algorithm is looking at elapsed time since the last fstrim

    If I want the script to do the fstrim once a week, I use built in default of 7 days.
    For example, If I want the fstrim to be done every 23 days I run it with dofstrim.sh 23

    If my system is off for a week or more, it does not bother me, and any extra fstrim execution does no harm.
    To preserve my SSD, I keep all my /development and it's sub-directories on a spinner, my SSD has just /, /home,/boot and /var . I also moved the /var/log logfiles to the spinner,

    I think my SSD will give me about 6 to 8 years of good performance before I replace it. That is much more than the 3-4 years that the hard disk with the /var/log and /Development will show signs of failing.

    Simple rule, in psuedo code.
    remainder=NumberDaysSinceLinuxStart % frequencyInDays # Number of days since 1 Jan 1970 = (date +%s)/ 86400

    read daynumber of the last fstrim.
    if remainder is zero and NumberofDaysSinceLinuxStart is different from daynumber
    record new daynumber=NumberofDaysSinceLinux start
    and run fstrim
    fi
    Last edited by lsatenstein; 16th April 2019 at 11:22 PM.
    Leslie in Montreal

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

  3. #3
    Join Date
    Dec 2013
    Location
    United Kingdom
    Posts
    7,096
    Mentioned
    5 Post(s)
    Tagged
    1 Thread(s)

    Re: How does fstrim.timer know when I week has passed if I power down

    running trim every day will cause excessive wear. that's the whole point of having a timer run the job once a week instead.

  4. #4
    Join Date
    Jun 2005
    Location
    Montreal, Que, Canada
    Posts
    5,861
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: How does fstrim.timer know when I week has passed if I power down

    Some clarification. ./dofstrim.sh xx can be run multiple times per day (I run it once every hour)
    When it runs, my script checks for a remainder. The value (date +%s)/86000 is the number of days since 1 Jan 1970. The remainder is modulo xx (above example xx was 23)
    If the remainder is zero, then I check if I should run fstrim by looking at the last daynumber that fstrim was executed. Non zero remainder ==>do not run fstrim.
    When dofstrim.sh runs the /sbin/fstrim, it also posts the execution day number into a file.
    If it is the same value as the "number of days since"' then fstrim was run today and it is skipped.

    I posted the script for anyone to review.
    https://forums.fedoraforum.org/showt...78#post1821778
    Last edited by lsatenstein; 17th April 2019 at 02:58 AM.
    Leslie in Montreal

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

  5. #5
    Join Date
    Dec 2013
    Location
    United Kingdom
    Posts
    7,096
    Mentioned
    5 Post(s)
    Tagged
    1 Thread(s)

    Re: How does fstrim.timer know when I week has passed if I power down

    fstrim timer triggers the event at 0:00 hours on a Monday. If the machine is powered down at that time, the fstrim service will run the next time it is on instead during the boot sequence. so if you didn't use the machine until Wednesday it would run then but also still try running it the next Monday regardless

Similar Threads

  1. FSTRIM Cron Job?
    By norcal618 in forum Using Fedora
    Replies: 4
    Last Post: 15th January 2018, 06:46 AM
  2. fstrim thoughts
    By lsatenstein in forum Hardware & Laptops
    Replies: 2
    Last Post: 2nd October 2017, 05:47 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: 0
    Last Post: 1st March 2011, 03:20 AM
  5. Mary Travers passed on this week.
    By beaker_ in forum Wibble
    Replies: 3
    Last Post: 19th September 2009, 01:51 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
  •