Automate Docker Service Monitoring with a Bash Script.
Docker is a powerful tool for containerization, but what happens if the Docker service stops unexpectedly? Manually checking and restarting the service can be tedious, especially in production environments. In this blog, we’ll automate the monitoring of the Docker service using a simple Bash script and a scheduled cron job.
Why Automate Docker Monitoring?
Imagine running critical applications inside Docker containers, and suddenly, Docker stops working. Without an automated restart mechanism, your applications go down, leading to downtime and disruptions. Automating Docker service monitoring ensures that the service is always running, reducing manual intervention.
The Bash Script
The following Bash script checks if the Docker service is running. If it's inactive, the script attempts to start it automatically.
#!/bin/bash
echo "=== Status Check - Docker Service ==="
status=$(systemctl is-active docker) # Check whether the docker service is active or not.
echo "$status"
if [ "$status" == "active" ] # If the status is active then execute first statement, else if status is inactive then execute second statement.
then
echo "Service is Running..."
else
echo "Service is not running..."
echo "Enabling the service:"
sudo systemctl start docker # If service is inactive then active it.
if [ $? -eq 0 ] # Checking whether the service is enabled or not.
then
echo "Service is Enabled successfully."
else
echo "Could not enable the service."
fi
fi
Breaking Down the Script
systemctl is-active docker: Checks if the Docker service is running.
if [ "$status" == "active" ]: If the service is running, it prints a confirmation message.
sudo systemctl start docker: If the service is not running, it attempts to restart it.
$? -eq 0: Checks if the restart was successful.
Automating with Crontab
To ensure this script runs periodically, we’ll schedule it with a cron job. Crontab allows us to execute commands at specified intervals.
Open the crontab editor:
crontab -e
Add the following line at the end:
* * * * * /home/omkar/Shell_Scripting_Projects/docker_service_check.sh >> /home/omkar/docker_service_check.log 2>&1
Explanation:
* * * * *
– Runs the script every minute.>> /home/omkar/docker_service_check.log 2>&1
– Redirects output to a log file.
Save and exit the editor.
Handling Permission Issues
If you see errors like “Interactive authentication required”, it means your user doesn’t have permission to restart services via cron. Here’s how to fix it:
Add User to the Docker Group:
sudo usermod -aG docker $USER newgrp docker # Apply changes
Allow Passwordless Systemctl in Sudoers:
sudo visudo
Add this line at the end:
%sudo ALL=(ALL) NOPASSWD: /bin/systemctl
Modify the script to run
sudo systemctl start docker
without prompting for a password.
Conclusion
With this Bash script and cron job, we can ensure Docker is always running without manual intervention. This automation saves time and ensures your containerized applications stay up and running.