Script to print return code from `zfs trim` command?

Hello,

I’ve got a cron job set up to manually trim my NVME pools once a week.

This works fine. (It has to run as root since I’m invoking zpool.)

However, on success, it doesn’t send me an email , because zpool trim doesn’t print anything to the standard output when it succeeds.

I’m not very good at shell scripting yet, at least when it comes to conditional statements and working with return codes from commands.

Can anyone recommend how to write a simple script that I can run in place of the raw zpool trim command that will run the trim, and then print a message to the standard output when the trim succeeds? As long as it prints something, I should get an email when it works.

Also, where is the best place to put such a script so it can be run as root?

You can use this one-liner to print if it was successful or not.

zpool trim -v "$ZPOOL_NAME" &> /dev/null && echo "Trim operation for $ZPOOL_NAME completed successfully." || echo "Error in Trim Operation: $?"

Note: You should replace "$ZPOOL_NAME" with your actual zpool name where you want to perform the trim operation.