Created
December 8, 2024 21:18
-
-
Save cgobat/cc15a015814aaf0dd7000e1255a1c2c9 to your computer and use it in GitHub Desktop.
Script to easily print current CPU temperature
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| """Display/print current CPU temperature. | |
| Copyright Caden Gobat (@cgobat on GitHub) | |
| This program is free software. You may use, copy, modify, and/or redistribute it under the terms of the GNU GPL v3. | |
| The lm-sensors package (or whatever provides the `sensors` command on your distro) is required to run this program. | |
| This file is most useful if it is made executable and placed somewhere on your $PATH. | |
| """ | |
| import json | |
| import subprocess | |
| def get_sensors_json() -> dict: | |
| cmd = ["sensors", "-j"] | |
| proc = subprocess.run(cmd, text=True, capture_output=True) | |
| json_str = proc.stdout | |
| json_dict = json.loads(json_str) | |
| return json_dict | |
| def get_temp_from_json(sensors_dict: dict) -> float: | |
| return sensors_dict["cpu_thermal-virtual-0"]["temp1"]["temp1_input"] | |
| if __name__ == "__main__": | |
| sensors_dict = get_sensors_json() | |
| temperature = get_temp_from_json(sensors_dict) | |
| print(f"{temperature:.1f}\xB0C") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment