brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: smartctl returns a bitmask, so a script can read smart trouble from $?

Smart
~/tips/tip-smart-exit-status-bits

sudo smartctl -q silent -a /dev/sda

Decodes the smartctl exit status so your script knows what went wrong.

The output of smartctl is made for people. Its exit status is the part made for scripts, and it is richer than a plain zero or one.

Zero means all is well. Anything else is a set of eight bits that can be on at the same time. Bit 0 is a bad command line, bit 1 a device that would not open (or is in low power mode), bit 2 a command that failed. Bit 3 means the drive reports DISK FAILING. Bits 4 and 5 are about attributes that crossed their threshold now or in the past, bit 6 means the error log has entries and bit 7 the self test log has errors.

In bash you test a bit with a mask:

sudo smartctl -q silent -a /dev/sda
rc=$?
(( rc & 8 ))  && echo "drive says FAILING"
(( rc & 64 )) && echo "error log has entries"
(( rc & 128 )) && echo "self test log has errors"

-q silent suppresses all output, so you only get the status. The mask is two to the power of the bit number: 8 for bit 3, 64 for bit 6.

Don’t write if [ $rc -ne 0 ]; then and page yourself. An old drive with one ancient entry in its error log sets bit 6 forever, and you would soon ignore the alert. I’d treat bit 3 as urgent and log the others for a weekly look.

One trap: with set -e a nonzero status ends your script right there, so capture it with || rc=$? instead.

Further reading: smartctl man page

Going deeper: Keep a domain pointed at your home with Cloudflare DDNS on Unraid

More Smart tips sudo nvme smart-log /dev/nvme0 sudo smartctl -l devstat /dev/sda sudo smartctl -l scttemphist /dev/sda sudo smartctl -t select,1000000-2000000 /dev/sda sudo smartctl -l sataphy /dev/sda All Smart tips (19) All topics

Link