从下面的代码中我想获取CPU温度,但出现以下错误:
Error: sudo: a terminal is required to read the password; either use the -S option to read from standard input or configure an askpass helper
sudo: a password is required
我尝试了很多方法来绕过密码,但没有任何效果,有人可以帮助我吗?
import subprocess
command = "sudo powermetrics --samplers smc | grep -i 'CPU die temperature'"
try:
result = subprocess.run(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
if result.returncode == 0:
# Command was successful, and the output is in result.stdout
output = result.stdout
print(output)
else:
# There was an error, and the error message is in result.stderr
error_message = result.stderr
print(f"Error: {error_message}")
except Exception as e:
print(f"An error occurred: {str(e)}")
作为数据科学家或软件工程师,监控 CPU 和 GPU 的温度至关重要,尤其是在长时间运行密集型应用程序时。高温可能会损坏您的硬件并导致系统不稳定