Restart container from host

If you need to restart a container, such as conjob, from the host side due to occasional issues with it getting stuck, this can be easily achieved with a shell script and a cron job on the host:

Create a shell script with the following content:

#!/bin/bash

# Find cronjob container by it's ID and kill it, which triggers a restart
# due to setting restart: always

# Find the container ID of the cronjob container
CONTAINER_ID=$(docker ps -q --filter "name=cronjob")

# Check if the container ID was found
if [ -n "$CONTAINER_ID" ]; then
    echo "Restarting the cronjob container with ID: $CONTAINER_ID"

    # Kill the container
    docker kill "$CONTAINER_ID"

    if [ $? -eq 0 ]; then
        echo "Container restarted successfully."
    else
        echo "Failed to restart the container."
        exit 1
    fi
else
    echo "No container with the name 'cronjob' is currently running."
    exit 1
fi

Edit the cron file on the server host to include the following entry:

Execution time

This configuration will execute the shell script every day at 3:05 AM.

For more details on defining cron time expressions, refer to: https://crontab.guru/

Last updated