Cronjob for Container Restart
July 5, 2024
A historical script for restarting a Kasm-related Docker container.
- Create a Script to Check the Docker Container and Manage Port 443:
Create a script, say check_and_restart_docker.sh, with the following content:
```bash #!/bin/bash
# Docker container ID to check
CONTAINER_ID=" # Check if the Docker container is running
if ! docker ps | grep -q "$CONTAINER_ID"; then
echo "Docker container $CONTAINER_ID is not running." else
echo "Docker container $CONTAINER_ID is running."
fi
``` Open the cron job editor: Add the following line to run the script every 5 minutes (adjust the interval as needed): This will run the script every 5 minutes and log the output to Save the file and exit the editor (for This setup will ensure that the specified Docker container is checked every 5 minutes. If it is not running, it will kill any process using port 443 and restart the Docker container. # Find the process using port 443
PID=$(sudo lsof -t -i:443)
if [ -n "$PID" ]; then
echo "Killing process $PID using port 443."
sudo kill -9 $PID
fi
# Start the Docker container
echo "Starting Docker container $CONTAINER_ID."
docker start $CONTAINER_ID
bash
sudo chmod +x /path/to/check_and_restart_docker.sh
bash
sudo crontab -ebash
*/5 * * * * /path/to/check_and_restart_docker.sh >> /var/log/check_and_restart_docker.log 2>&1/var/log/check_and_restart_docker.log.
nano, press Ctrl+X, then Y, and Enter).