Help with creating a startup shell script to run update & fstrim on startup

I am trying to create a startup shell script to run udate; autoclean; autoremove; and fstrim at startup. Below is what I've come up with. Can someone validate my syntax and confirm where this script should be placed (i.e. etc/init.d)?

#!/bin/sh

run update, autoclean, autoremove and fstrim on system at startup

sudo /bin/sh -c "apt-get update && apt-get autoclean && apt-get autoremove && fstrim -v --all"

What I did:

First, I made the script executable:

chmod +x myscript.sh

Then, I placed my script in the following directory:

sudo cp myscript.sh /etc/init.d/

Then, I once again made the script executable:

sudo chmod +x /etc/init.d/myscript.sh

Then I used the following command to run the script at startup:

sudo update-rc.d myscript.sh defaults

However, I cannot tell if the script runs at startup.

1 Like

I am not sure if this will work on Gnome 43 but, you might try setting it to send a Notification:

#!/bin/bash
sudo apt-get update
sudo apt-get autoclean
sudo apt-get autoremove
sudo fstrim -v --all

notify-send "MyScript" "Completed successfully."
1 Like

Additionally:

Is Fstrim Still Necessary for SSDs

Running the fstrim command on SSDs in Linux is still beneficial for maintaining optimal performance and extending the lifespan of the SSD. Modern Linux distributions, such as Ubuntu and Fedora, typically have fstrim scheduled to run automatically on a weekly basis via a systemd timer or cron job. This automation helps ensure that unused blocks are discarded regularly, which can improve write performance and wear leveling.

However, it’s important to verify that your SSD and filesystem support TRIM. You can check if TRIM is enabled by running:

sudo hdparm -I /dev/sda | grep TRIM

If your system is already set up to handle TRIM operations automatically, manually running fstrim might be redundant. Additionally, ensure that you’re not running fstrim during intensive operations, as this could impact performance.

In summary, while fstrim is still necessary for SSD optimization, many modern Linux distributions handle it automatically. If your distribution does not have fstrim enabled by default, you can enable it using:

sudo systemctl enable --now fstrim.timer
5 Likes

You can use the following command to see when it runs.

sudo systemctl status fstrim.timer

2 Likes

hdparm only works with older SSDs and not with NVMe SSDs.

Interesting discussion!

This is something I haven't had to think about, since I was on Windows, and I had to do what is known as defragging, cause the Windows NTFS system was so inefficient, you'd develop file system issues, within only days!

On Linux, it never seems to be an issue, since Linux uses a GPT4 file system, and in its nature, doesn't to screw me over. But out of curiosity, I ran that command, and I discovered that my NVME drives auto trim weekly.


2 Likes