[HOW TO] Get NMVe temperature for conky

EDIT: With kernel 5.5, none of this is necessary. You can simply use hwmon. Example:

${hwmon nvme temp 1}

Taken from Is there an hddtemp fork that reads NVMe drives?

Conky can execute a shell command from within the .conkyrc file, but the developers recommend against it for efficiency reasons. Also, since you must run nvme or smartctl as root, you get sudo spam in the logs. So this post describes a very short shell script and systemd unit file that put the temperature in a file in /tmp that is world readable for conky to use. If the daemon dies for some reason, the file in /tmp is deleted. Otherwise, the last known temperature would be returned and you'd have no idea it was a fictitious reading.

Instructions:

Install mksh and nvme-cli or smartmontools. Either can be used, examples below.

sudo pacman -Syu mksh nvme-cli

or

sudo pacman -Syu mksh smartmontools

Find your nvme drive, usually nvme0:

$ ls -l /dev/nvme*
crw------- 1 root root 244, 0 Oct 30 07:47 /dev/nvme0
brw-rw---- 1 root disk 259, 0 Oct 30 07:47 /dev/nvme0n1
brw-rw---- 1 root disk 259, 1 Oct 30 07:47 /dev/nvme0n1p1
brw-rw---- 1 root disk 259, 2 Oct 30 07:47 /dev/nvme0n1p2

Create the daemon script and open it in your favorite text editor like nano, gedit or kate:

touch ~/.local/bin/nvme0-temp
nano ~/.local/bin/nvme0-temp

Paste the contents (for nvme-cli):

#!/usr/bin/mksh

tempfile="/tmp/nvme0-temp"

trap "rm -f "$tempfile"; exit" 0 1 2 3 13 15  # Exit, HUP, INT, QUIT, PIPE, TERM

while true; do
    temperature="$(nvme smart-log /dev/nvme0 | sed -n '/^[Tt]emperature *: *\([0-9]*\).*/{s//\1/p;q}')"
    builtin echo "$temperature" > "$tempfile"
    builtin sleep 2
done

Paste the contents (for smartmontools):
Thanks to @anon27588196

#!/usr/bin/mksh

tempfile="/tmp/nvme0-temp"

trap "rm -f "$tempfile"; exit" 0 1 2 3 13 15  # Exit, HUP, INT, QUIT, PIPE, TERM

while true; do
    temperature="$(smartctl -A /dev/nvme0 | grep Temperature: |awk '{printf "%.0f",($2)}')"
    builtin echo "$temperature" > "$tempfile"
    builtin sleep 2
done

Now make it executable:

chmod +x ~/.local/bin/nvme0-temp

Now create the service file and open in a text editor:

sudo touch /usr/lib/systemd/system/nvme0-temp.service
sudo nano /usr/lib/systemd/system/nvme0-temp.service

Paste the contents:

[Unit]
Description=NVMe temperature monitor daemon

[Service]
ExecStart=~/.local/bin/nvme0-temp

[Install]
WantedBy=multi-user.target

Start and enable the service:

systemctl enable --now nvme0-temp

You can see the temperature with:

$ cat /tmp/nvme0-temp
40

Add this to your .conkyrc file:

${if_existing /tmp/nvme0-temp}${tail /tmp/nvme0-temp 1 1}°C${endif}

If you prefer Fahrenheit:

${if_existing /tmp/nvme0-temp}${execi 60 echo $(($(cat /tmp/nvme0-temp) * 9 / 5 + 32))}°F${endif}

My wonky conky:
Screenshot%20from%202019-10-30%2009-33-01

7 Likes

I didn't care for that script...it actually added a slight load (on my system, anyway), and I didn't like the journal sudo spam either.

I'm the only one using this machine anyway, so I went a bit extreme and did a good, old-fashioned SUID on smartctl.

Huh? It's run as a system service, there isn't any. However, I also use hddtemp -n --unit=F /dev/sda for my HDD and that does.

What does your smartctl command look like?

I was alluding to running the smartctl command as sudo.

${texeci 600 /usr/bin/smartctl -A /dev/nvme0 | grep Temperature: |awk '{printf "%.0f",($2*9/5+32)}'}°F
1 Like

Are you running hddtemp as a daemon (systemd service)? I don't have any journal entries for hddtemp, other than boot messages:

Oct 30 09:14:12 Jammin1 systemd[1]: hddtemp.service: Succeeded.

No, I added it to the sudoers file. It would be better to run it as as a daemon (hddtemp -d) and query it wtih nc, but I haven't got that far yet.

I am now. I followed the hddtemp Arch wiki article and edited the service:

[Service]
ExecStart=
ExecStart=/usr/bin/hddtemp -dF --unit=F /dev/sda --listen=127.0.0.1

Now all I need in my .conkyrc is ${hddtemp}°F.

Also, now there's no reason to edit the sudoers file and it should use less resources. Oh, and no journal sudo spam.

1 Like

Forum kindly sponsored by