Automating EC2 System Shutdown with Bash Script if the docker container is idle

Streamlining Docker Container Management and System Shutdown with a Bash Script

Kuharan Bhowmik
4 min readJul 9, 2023

--

Photo by Rohit Tandon on Unsplash

Automation plays a crucial role in managing and maintaining systems efficiently. In this article, we will explore a Bash script that checks the status of Docker containers and the system’s uptime to perform automatic actions, such as stopping Docker containers and shutting down the system if certain conditions are met. This script can be useful in scenarios where you want to ensure that Docker containers are not running for system maintenance or when you want to automatically shut down the system after a specific period of time.

#!/bin/bash

# Check if any Docker container is running
containers_running=$(docker ps --format '{{.State}}' | grep -c "running")

if [ $containers_running -gt 0 ]; then
echo "At least one Docker container is running. Exiting..."
exit 0
fi

# Get system uptime in minutes
uptime_seconds=$(awk '{print $1}' /proc/uptime | awk -F. '{print $1}')

# Convert uptime to hours
uptime_hours=$((uptime_minutes / 3600))

if [ $uptime_hours -ge 2 ]; then
echo "System uptime is more than 2 hours. Shutting down..."
sudo shutdown -h now
else
echo "System uptime is less than 2 hours. No action required."
fi

Script Overview:

The provided Bash script consists of several steps to determine whether any Docker containers are running and the system’s uptime. Let’s break down the script to understand its functionality.

Checking Docker Containers:

The script starts by using the command docker ps --format '{{.State}}' to retrieve the state of all Docker containers currently running. The output is then piped into grep -c "running" to count the number of containers with a "running" state. If there is at least one running container, the script displays a message indicating that and exits gracefully.

System Uptime Calculation:

To determine the system’s uptime, the script reads the contents of the /proc/uptime file using awk '{print $1}'. The value represents the uptime in seconds with decimal places. To convert this value to minutes, the script uses awk -F. '{print $1}'. The resulting uptime in minutes is stored in the variable uptime_seconds.

Converting Uptime to Hours:

Next, the script divides uptime_secondsby 3600 to convert the uptime to hours. The result is stored in the variable uptime_hours.

Taking Action:

If the system’s uptime is greater than or equal to 2 hours, the script displays a message stating that the system uptime is more than 2 hours and proceeds to execute the command sudo shutdown -h now. This command initiates an immediate system shutdown. On the other hand, if the uptime is less than 2 hours, the script displays a message indicating that no action is required.

Compact Script Overview:

The compact version of the script consists of condensed commands that achieve the same functionality as the original script. Let’s break down the script to understand its purpose and how it works.

Creating the Script:

To create the script, the echo command is used in combination with the tee command, which writes the output to a file. The script is created at /usr/local/bin/check_container.sh. Here's the compact version of the script:

echo '#!/bin/bash' | sudo tee /usr/local/bin/check_container.sh
echo 'if [ $(docker ps --format "{{.State}}" | grep -c "running") -gt 0 ]; then' | sudo tee -a /usr/local/bin/check_container.sh
echo ' exit 0' | sudo tee -a /usr/local/bin/check_container.sh
echo 'fi' | sudo tee -a /usr/local/bin/check_container.sh
echo 'if [ $(awk "{print $1}" /proc/uptime | cut -d" " -f1 | cut -d"." -f1) -ge 7200 ]; then' | sudo tee -a /usr/local/bin/check_container.sh
echo ' sudo shutdown -h now' | sudo tee -a /usr/local/bin/check_container.sh
echo 'fi' | sudo tee -a /usr/local/bin/check_container.sh

Scheduling the Script with Cron:

Cron is a time-based job scheduler in Unix-like operating systems. Here, we schedule the script to run every 5 minutes using cron. The script is added to the /etc/cron.d/check_container_cron file and the cron service is restarted to activate the new schedule. Here's the command for scheduling the script:

echo '*/5 * * * * root /usr/local/bin/check_container.sh >/dev/null 2>&1' | sudo tee /etc/cron.d/check_container_cron > /dev/null
sudo systemctl restart cron

Conclusion:

Automation can significantly simplify routine tasks and ensure systems operate smoothly. The Bash script we’ve discussed provides a simple yet effective way to check the status of Docker containers and the system’s uptime, enabling automated actions based on predefined conditions. By leveraging this script, you can effortlessly manage Docker containers and automatically shut down the system as needed. Feel free to modify and enhance the script to suit your specific requirements.

Happy scripting!

--

--