Thinkfan not starting at reboot on Ubuntu 19.10

These days I somehow screwed up my system and I had to setup the OS from scratch. Since I went from Ubuntu 16.04 to Ubuntu 18.04 only with updating the system, I felt like it is about time to do everything new to get rid of some old stuff and mess from the previous installation.

My Laptop is a Lenovo ThinkPad E580. It’s the version with the i7-8550U CPU, a 250GB M2 SSD and a Radeon RX 550 graphics chip. It is quite a nice system and I am very happy with my choice.

During the last year I was a bit annoyed because there was a lot of thermal throttling on the cpu and I was maybe to lazy to dig into it. Since I was setting up this thing from scratch, I was now looking a bit deeper into it.

thinkfan in combination with lm-sensors tracks the temperature of certain sensors and allows you to control the fan spin at different temperatures. In fact you can also screw up your cpu, and likely other components on the mainboard as well, by configuring thinkfan the wrong way. By default thinkfan will only use the highest temperature it gets to control the fan by it’s default value – which is quite OK but not suitable for my laptop be cause it usually kicked it to late and cause my cpu to throttle down. So you should configure it. Look for the config file in /etc/thinkfan.conf.

In order for thinkfan to know the temperature of certain sensors, you need to tell it to monitor some system files. Usually you will find in forums or stackoverflow to run something like: find /sys/devices -type f -name "temp*_input" and place the output of it in the config file with a leading hwmon . In fact, this post askubuntu.com/a/834215 did help me a lot. But at the end of the day, after rebooting the laptop, thinkfan didn’t start up. Trying to start thinkfan manually using: sudo thinkfan -n showed me, that thinkfan couldn’t find the paths to check the temperature. So I updated the config file and rebooted and again – same issue. So I compared the files on the system with the ones in the config and I found that they are different. So something in Ubuntu is for whatever reason changing the paths on reboot and the config file is of course not updated.

So to get it working again properly, we need to edit the config file on a reboot just before systemd starts up thinkfan. That should be a big deal.

Bash script for thinkfan

So we can use a very small bash script which removes old entries from the config file and then runs the “find” command and adds the new lines to /etc/thinkfan.conf.

This is the script I fixed and I placed it in /etc/thinkfan_prestart.sh. This script is just a very quick and dirty version. Anyone is free to use it and extend it. I’m sure some more checks would be nice.

#!/bin/bash

## This script removes the hwmon definitions for thinkfan in "/etc/thinkfan.conf" and writes the new ones to the bottom of the file.
## Ubuntu somehow changes the directories /sys/devices/platform/thinkpad_hwmon/hwmon/hwmonX on every reboot and this is just a workaround.

## Im using sed to remove all lines in the config file beginning with hwmon
/usr/bin/sed -i /^hwmon/d /etc/thinkfan.conf

## This for-loop writes the result right at the end of the config file.
## In my case, "find", finds a pci address as well and since I don't want that in the config file (thinkfan will not start) I'm removing it from the output with grep -v.
for i in $(/usr/bin/find /sys/devices -type f -name "temp*_input" | /usr/bin/grep -v "pci"); do /usr/bin/echo "hwmon $i" >> /etc/thinkfan.conf; done

## We want to wait a little bit before thinkfan can start 
/usr/bin/sleep 2

Now it needs to be made executable with a chmod +x /etc/thinkfan_prestart.sh.

Edit the systemd startup file

Now we need to tell systemd to run this script before it tries to start up thinkfan. In order to do that you would need to edit it’s .service file. In my case it is located at: /usr/lib/systemd/system/thinkfan.service
Open the file and add the line: ExecStartPre=/etc/thinkfan_prestart.sh just right above the ExecStart parameter. My Service file looks like this:

[Unit]
Description=simple and lightweight fan control program
After=syslog.target

[Service]Type=forking
EnvironmentFile=-/etc/default/thinkfan
ExecStartPre=/etc/thinkfan_prestart.sh
ExecStart=/usr/sbin/thinkfan $DAEMON_ARGS
PIDFile=/var/run/thinkfan.pid
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target

After that you might need to enable the thinkfan service again by running: sudo systemctl enable thinkfan. That’s basically it. What I did in addition and to prevent any thinkfan update to mess with systemd .service file is to put a hold on the thinkfan packages: sudo apt-mark hold thinkfan*.
I think thinkfan is well tested software and I don’t expect plenty updates coming to it. In case there will be an update, apt will let me know that it skipped an update for thinkfan, which allows me to remove the hold, update thinkfan and fix the service file when I feel it is the right time to do.

Leave a Reply

Your email address will not be published. Required fields are marked *

*