Your laptop shutting down unexpectedly can be a sign of several issues. Here's a Python script that can help you check for some common problems. This script will monitor your CPU and GPU temperature, which is a frequent cause of unexpected shutdowns. It will also log the current time and temperature so you can correlate the shutdowns with specific events.Since you're using Ubuntu and have an NVIDIA GeForce RTX™ 3050 Laptop GPU, this script is tailored to work with those specifics. It uses lm-sensors for CPU temperature and nvidia-smi for GPU temperature, as these are standard tools on your system.Before You Run the ScriptYou'll need to install a few things first if you don't already have them.Install lm-sensors and python3-psutil: lm-sensors is a tool to monitor hardware, and psutil is a Python library for system monitoring.Bashsudo apt update
sudo apt install lm-sensors python3-psutil
Install NVIDIA GPU management tools: nvidia-smi should be part of your NVIDIA driver installation, but if not, you can install the driver from the official site. You can also install the nvidia-cuda-toolkit, which includes nvidia-smi.Bashsudo apt install nvidia-cuda-toolkit
Configure lm-sensors: Run sensors-detect to set up the sensors for your system. Follow the on-screen prompts.Bashsudo sensors-detect
Once this is done, you should be able to run the sensors command and see your CPU temperature.Python Monitoring ScriptSave the following code as a Python file (e.g., monitor_temp.py).Pythonimport psutil
import subprocess
import time
import datetime
import os
def get_cpu_temp():
"""Reads CPU temperature using lm-sensors."""
try:
temp_output = subprocess.check_output(['sensors']).decode('utf-8')
for line in temp_output.split('\n'):
if 'Core' in line or 'Package id' in line:
temp_str = line.split('+')[1].split('°C')[0]
return float(temp_str)
except (subprocess.CalledProcessError, FileNotFoundError):
return None
return None
def get_gpu_temp():
"""Reads GPU temperature using nvidia-smi."""
try:
temp_output = subprocess.check_output(['nvidia-smi', '--query-gpu=temperature.gpu', '--format=csv,noheader,nounits']).decode('utf-8')
return float(temp_output.strip())
except (subprocess.CalledProcessError, FileNotFoundError):
return None
def log_temperatures():
"""Logs temperatures to a file."""
log_file_path = os.path.expanduser('~/temperature_log.txt')
print(f"Logging temperatures to {log_file_path}. Press Ctrl+C to stop.")
try:
with open(log_file_path, 'a') as f:
while True:
cpu_temp = get_cpu_temp()
gpu_temp = get_gpu_temp()
timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
log_entry = f"[{timestamp}] "
if cpu_temp is not None:
log_entry += f"CPU Temp: {cpu_temp}°C "
else:
log_entry += "CPU Temp: N/A "
if gpu_temp is not None:
log_entry += f"GPU Temp: {gpu_temp}°C"
else:
log_entry += "GPU Temp: N/A"
print(log_entry)
f.write(log_entry + '\n')
time.sleep(5) # Log every 5 seconds
except KeyboardInterrupt:
print("\nMonitoring stopped.")
if name == "main":
log_temperatures()
How to Use the ScriptOpen a terminal.Navigate to the directory where you saved monitor_temp.py.Run the script with the following command:Bashpython3 monitor_temp.py
The script will continuously log the CPU and GPU temperatures to a file named temperature_log.txt in your home directory. Let it run for a while, especially when you are doing a demanding task like training an AI model or playing a game. If your laptop shuts down again, you can check the last few lines of the log file to see if the temperatures were extremely high right before the shutdown.Other Possible ReasonsWhile overheating is a common cause, other issues could be at play:Power Supply Issues: A faulty power adapter or battery can cause a sudden shutdown, especially under load.Driver Problems: Outdated or corrupted GPU drivers can sometimes lead to system instability and shutdowns. Make sure your NVIDIA drivers are up to date.Dust Accumulation: A buildup of dust in the fans and vents can block airflow, causing the system to overheat.RAM/Hardware Failure: A failing component like a RAM stick or hard drive can cause critical errors that force a shutdown.This script is a great first step in diagnosing an overheating problem, which is a common cause for the symptoms you described. It's a useful tool for your research in AI, as training models can push hardware to its limits.