how to check hdd temperature in linux

How to Get HDD Temperature in Linux

System administrators need to monitor their system’s temperature to ensure that it is not getting overheated. This also involves keeping track of hard disk (HDD) temperature. When system overheats it can affect performance and can even cause system to crash. So many system administrators use shell scripts to monitor HDD temperature and alert them in case of sudden rise in it. However, there are various third party tools like inxi and hddtemp that you can use check HDD temperature and also use it in shell scripts. In this article, we will learn how to get HDD temperature in Linux.


How to Get HDD Temperature in Linux

Here are the steps to get HDD temperature in Linux. We will use inxi and hddtemp for this purpose.

1. Install HDD Temperature Checking tools

By default, inxi and hddtemp are not installed in Linux systems. Open terminal and run the following commands to install them.

# Ubuntu, Debian, and Linux Mint

$ sudo apt install inxi hddtemp

# Fedora, CentOS, AlmaLinux, and Red Hat

$ sudo dnf install inxi hddtemp

2. Get HDD Temperature

Next, we will list all our disks using fdisk or other such commands.

$ sudo fdisk -l
OR
$ lsscsi -g

Hard disks typically have the path as /dev/sdX format. Here is the command to get temperature of all hard disks on your system using inxi.

$ sudo inxi -xD

Here is the sample output.

Drives:    HDD Total Size: 75.5GB (70.3% used) 
ID-1: /dev/sda model: HTS721060G9SA00 size: 60.0GB temp: 35C
ID-2: USB /dev/sdb model: TransMemory size: 15.5GB temp: 0C

The temp attribute indicates the temperature of each hard disk. Please note, the above command needs to be run with root or sudo privileges.

If you want to get temperature of specific hard disk, you need to mention it at the end of the command as shown below.

$ sudo inxi -xD /dev/sdb

You can also get the same information using hddtemp utility tool.

$ sudo hddtemp /dev/sda
/dev/sda: HTS721060G9SA00: 36°C

3. Automate Temperature Checks

You may need to regularly check temperature of your hard disks so it is convenient if you automate using shell scripts. Create an empty shell script for the same using the following command.

$ vi temp_check.sh

Add the following lines to it.

#!/bin/bash

temperature=$(hddtemp /dev/sda | cut -d : -f3 | sed 's/[^0-9]*//g')

# REPORT when hard drive's temperature is above 50C

if [ $temperature -ge 50 ]; then
	echo "ALERT: hard drive's temperature is above: $temperature"
fi

Save and close the file. In the above code, we use hddtemp utility to obtain the temperature of our hard disk /dev/sda. We pass its output to cut & sed command to extract the exact numerical temperature value. Then we use an if condition to check if it is above 50C. If so, we display an alert message about it.

Replace /dev/sda with your choice of hard disk. Also you can replace echo command with a cat command to write the output to another file.

Make the shell script executable.

$ sudo chmod +x temp_check.sh

You can run the shell script using the following command.

$ ./temp_check.sh

Once you have the shell script ready, you can even create a cronjob to run it every day at same time. Open crontab with the following command.

$ crontab -e

Add the following line to it to run your shell script every day at 10.a.m.

0 10 * * * ./temp_check.sh >/dev/null 2>&1

Save and close the crontab file. In this article, we have learnt how to install and use tools to check hard disk temperature via terminal as well as using shell script. We also learnt how to automate the entire process by creating a cronjob to run the script every day.

Also read:

How to Install New Fonts in Linux
How to List GPG Keys in Linux
How to Find Oldest File in Directory
How to Show Disk Usage Only for Top Level Directories
How to Resolve bin/sh: source not found

Leave a Reply

Your email address will not be published.