How to create a custom systemd service file

OK I stopped the services and started from scratch doing what you suggested in post 19. Still does not work. I did realize along the way once I reboot I can just shut down deluge, get my drives mounted, and then restart deluge and not have to do a recheck of the torrents in it. I really appreciate the time and effort that's been put in, Thanks again.
 


I got the idea for this from another thread about someone who's hard drive is over heating from time to time.

Lets create a bash shell script.
Code:
#!/bin/bash

# Run smartctl command and extract the temperature
TEMP=$(sudo smartctl -a /dev/nvme1n1 | grep "Temperature:" | awk '{print $2}')

# Check if the temperature exceeds 65°C
if [ "$TEMP" -gt 65 ]; then
    # Log the message
    echo "$(date): Temperature is $TEMP°C, shutting down the system." | sudo tee -a /var/log/temp_monitor.log

    # Display the message on the screen
    echo "Temperature is $TEMP°C, shutting down the system."

    # Shutdown the system
    sudo shutdown -h now
fi

Let's make the script executable

Code:
chmod +x check_temp.sh


Let's create a new file called check_temp.service at /etc/systemd/system/
We will name the file check_temp.service
Code:
[Unit]
Description=Temperature Monitoring Service

[Service]
ExecStart=/path/to/check_temp.sh

Adjust the path to wherever your bash shell script is located.
We also need a timer file since we want to run this every 3 mins or so.
Create a file named check_temp.timer in /etc/systemd/system/
Enter the code below.

Code:
[Unit]
Description=Run Temperature Monitoring Script Every 3 Minutes

[Timer]
OnBootSec=1min
OnUnitActiveSec=3min
Unit=check_temp.service

[Install]
WantedBy=timers.target

Then we will enable and start the service so it will run automatically every time the computer starts up.

Code:
sudo systemctl enable check_temp.timer
sudo systemctl start check_temp.timer
 
Last edited:

Members online


Latest posts

Top