psensor temp alarm script

2 min read 17-10-2024
psensor temp alarm script

Psensor is a graphical hardware temperature monitoring tool for Linux that allows users to keep track of the temperature of various components in their systems, such as the CPU, GPU, and hard drives. In this article, we will explore how to set up a temperature alarm script using Psensor to notify you when temperatures exceed a certain threshold.

What is Psensor?

Psensor is a tool that helps monitor temperature sensors and displays the information in a user-friendly graphical interface. It can also be configured to trigger alerts or actions when certain temperature conditions are met. This is particularly useful for preventing overheating and ensuring system stability.

Installing Psensor

Before you can create an alarm script, you need to have Psensor installed on your system. You can install it using the following command on Debian-based systems like Ubuntu:

sudo apt install psensor

For Fedora or Red Hat-based distributions, you might use:

sudo dnf install psensor

Configuring Psensor

Once Psensor is installed, you need to configure it to monitor the desired temperature sensors. Start Psensor by running the following command:

psensor

The Psensor GUI will open, and you will see a list of available sensors. Add the sensors you wish to monitor by right-clicking on them and selecting "Add sensor."

Creating a Temperature Alarm Script

To set up an alarm script, you'll need to create a Bash script that uses Psensor's built-in features to check the temperature and send alerts. Below is a simple example of a script that checks the CPU temperature and sends an alert if it exceeds a specified threshold.

Example Script: temp_alarm.sh

#!/bin/bash

# Threshold for temperature in Celsius
THRESHOLD=80
# Log file for temperature readings
LOGFILE="/var/log/temp_alarm.log"

# Get current CPU temperature
TEMP=$(psensor --get-cpu-temperature)

# Check if temperature exceeds the threshold
if (( $(echo "$TEMP > $THRESHOLD" | bc -l) )); then
    # Log the alarm
    echo "$(date): CPU temperature alert! Current temperature: $TEMP°C" >> $LOGFILE
    # Send an alert notification (optional)
    notify-send "Temperature Alert" "CPU temperature is above $THRESHOLD°C! Current temperature: $TEMP°C"
fi

Explanation of the Script

  • THRESHOLD: This variable defines the maximum temperature that you want to monitor. You can adjust it according to your system specifications.
  • LOGFILE: This is where the script will log alerts for later review.
  • TEMP: This variable retrieves the current CPU temperature using Psensor's command line tool.
  • Alert Condition: The if statement checks if the current temperature exceeds the defined threshold. If it does, it logs the alert and can send a notification.

Setting Permissions and Scheduling

  1. Save the script as temp_alarm.sh and give it executable permissions:

    chmod +x temp_alarm.sh
    
  2. To run this script at regular intervals, you can use cron. Open the crontab editor:

    crontab -e
    
  3. Add a line to run the script every 5 minutes:

    */5 * * * * /path/to/temp_alarm.sh
    

Conclusion

Using Psensor with a custom temperature alarm script allows you to proactively monitor your system's temperature and take action before overheating becomes a serious issue. By setting up alerts and logging, you can ensure that your system remains stable and performative under various conditions. Adjust the script to fit your specific requirements, and keep an eye on your hardware's health!

close