How to force a Linux server to shutdown if there is no active SSH connection for the last hour

Ahh the cloud! What a wonderful time to work as a sysadmin/devops. you have the power to create an unlimited number of virtual machines…

How to force a Linux server to shutdown if there is no active SSH connection for the last hour
Photo by Chiara Guercio on Unsplash

Ahh the cloud! What a wonderful time to work as a sysadmin/devops. you have the power to create an unlimited number of virtual machines, with multiple CPUs and RAM sizes two decades before could be a hard drive.. also this power can create costly mistakes, assume that devs start some virtual machines to run some tests, they end their tests but they forgot to shutdown the virtual machines.. we know what happens next.. a costly bill, but I have a handy solution on this! I will show you how to create a script that will shutdown the server if there is no active tty SSH connection for more than one hour!

How to check for active tty SSH connections

The following one-liner checks if there is established an interactive SSH connection by a user, if does not return anything means that there is no user connected with SSH

who | grep -E 'tty[0-9]+|pts/[0-9]+' | grep -E '([0-9]{1,3}\.){3}[0-9]{1,3}' 
root     pts/0        2024-09-12 21:53 (185.44.146.81)

The script

Create the following script as /usr/local/bin/inactivity-shutdown.sh

#!/bin/bash 
 
# Time to wait after the last SSH session 
IDLE_TIMEOUT=3600  # 1 hour = 3600 seconds 
 
active_ssh_sessions() { 
    active_sessions=$(who | grep -E 'tty[0-9]+|pts/[0-9]+' | grep -E '([0-9]{1,3}\.){3}[0-9]{1,3}') 
    if [[ -z "$active_sessions" ]]; then 
        return 1  # No SSH session 
    else 
        return 0  # SSH session exists 
    fi 
} 
 
while true; do 
    if active_ssh_sessions; then 
 echo "$(date) -  Active ssh connection(s) found, taking a nap and check later" 
        sleep 300 
    else 
        sleep $IDLE_TIMEOUT 
        if ! active_ssh_sessions; then 
     echo "$(date) -  Zero active ssh connection(s) found.. shuting down system now" 
            sudo shutdown -h now 
 fi 
    fi 
done
  • active_ssh_session : This function returns 1 if there is no active SSH session, else returns 0
  • The while loop checks every 300 seconds (5 minutes) if there is an active SSH connection, if there is it sleeps for one hour, if still there is not it shutdowns the server, else the loop starts again.

To make the script executable do the following

sudo chmod +x /usr/local/bin/inactivity-shutdown.sh

Configure the script to run on the system boot

To enforce this script to work must start on boot time and executed as root, to do this create the following crontab entry as root

@reboot /usr/local/bin/inactivity-shutdown.sh &

This entry will execute the script in the background as root and will shutdown the server after one hour!

Conclusion

In the era of costly clouds is very useful to know how to shield the organization from costly mistakes, one such mistake could be a forgotten virtual machine.. I hope this article helped you!