smartctl Command: Tutorial & Examples

Display health and performance of disk drives

The smartctl command is a utility for controlling and monitoring the "Self-Monitoring, Analysis and Reporting Technology" (SMART) system built into most modern ATA and SCSI hard drives, as well as SSDs. SMART is a system that monitors the health and performance of a hard drive and can predict potential failures.

To use the smartctl command, you will need to specify the device that you want to check. For example, to check the SMART status of the first ATA hard drive on your system, you can use the following command:

sudo smartctl -H /dev/sda

This will produce output similar to the following:

smartctl 6.6 2016-05-31 r4324 [x86_64-linux-4.15.0-108-generic] (local build)
Copyright (C) 2002-16, Bruce Allen, Christian Franke, www.smartmontools.org

=== START OF READ SMART DATA SECTION ===
SMART overall-health self-assessment test result: PASSED

If the SMART overall-health self-assessment test result is "PASSED", then the hard drive is functioning properly and there are no known issues. If the result is "FAILED", then it may indicate that the hard drive is experiencing problems and may fail in the future.

You can also use the smartctl command to check the SMART status of a specific attribute of the hard drive. For example, to check the SMART status of the "Reallocated Sectors Count" attribute of the first ATA hard drive on your system, you can use the following command:

sudo smartctl -A /dev/sda | grep Reallocated

This will produce output similar to the following:

5 Reallocated_Sector_Ct   0x0033   100   100   036    Pre-fail  Always       -       0

The output shows the attribute ID, the attribute name, the current value, the threshold value, and the worst value for the attribute. If the current value is greater than the threshold value, it may indicate that the hard drive is experiencing problems.

When smartctl is executed, it will return an exit code that indicates the status of the operation. The exit codes can be used to determine whether the operation was successful or whether an error occurred.

And exit code of 0 means that everything is OK, while any other number larger than 0 means that something is wrong.

To quickly check a device, you can use:

smartctl -a /dev/sda && echo OK || echo Error

To display the actual exit code, you can run echo $? after smartctl which will print the exit code of the last executed command.

Every bit in this number has a certain meaning, so you first need to separate it into the different bits. This can be done like this:

status=$?
for ((i=0; i<8; i++)); do
  echo "Bit $i: $((status & 2**i && 1))"
done

This is the meaning of the different bits:

  • bit 0: Command line parse error
  • bit 1: Device could not be opened
  • bit 2: SMART command or checksum error
  • bit 3: SMART status check indicates "DISK FAILING"
  • bit 4: Some prefail attributes is below threshold
  • bit 5: Even though right now the disk seems OK, there was some prefail attribute below threshold in the past
  • bit 6: There are records of errors in the device error log
  • bit 7: There are records of errors in the device self-test log

For more information about the smartctl command and its options, you can refer to the manual page for the command by running the following command:

man smartctl

CleverUptime uses the smartctl command to check the health of your disk drives.

The text above is licensed under CC BY-SA 4.0 CC BY SA