Update monitor-lxcs.sh

add option to exclude containers
This commit is contained in:
tteckster 2023-04-27 18:38:02 -04:00 committed by GitHub
parent 700717da43
commit 488895ad5a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 19 additions and 7 deletions

View File

@ -27,6 +27,11 @@ while true; do
done done
echo '#!/usr/bin/env bash echo '#!/usr/bin/env bash
# Read excluded containers from command line arguments
excluded_containers=("$@")
echo "Excluded containers: ${excluded_containers[@]}"
while true while true
do do
# Get the list of containers # Get the list of containers
@ -34,6 +39,11 @@ do
for container in $containers for container in $containers
do do
# Skip excluded containers
if [[ " ${excluded_containers[@]} " =~ " ${container} " ]]; then
continue
fi
# Skip containers based on templates # Skip containers based on templates
template=$(pct config $container | grep -q "template:" && echo "true" || echo "false") template=$(pct config $container | grep -q "template:" && echo "true" || echo "false")
if [ "$template" == "true" ]; then if [ "$template" == "true" ]; then
@ -46,27 +56,29 @@ do
# Ping the container # Ping the container
if ! ping -c 1 $IP >/dev/null 2>&1; then if ! ping -c 1 $IP >/dev/null 2>&1; then
# If the container can'\''t be pinged, stop and start it # If the container can'\''t be pinged, stop and start it
echo -e "$(date): Container $container is not responding, restarting..." echo "$(date): Container $container is not responding, restarting..."
pct stop $container >/dev/null 2>&1 pct stop $container >/dev/null 2>&1
sleep 5 sleep 5
pct start $container >/dev/null 2>&1 pct start $container >/dev/null 2>&1
fi fi
done done
# Wait for 5 minutes # Wait for 5 minutes. (Edit to your needs)
echo -e "$(date): Sleeping for 5 minutes..." echo "$(date): Sleeping for 5 minutes..."
sleep 300 sleep 300
done >> /var/log/ping-containers.log 2>&1' >/usr/local/bin/ping-containers.sh done >> /var/log/ping-containers.log 2>&1' >/usr/local/bin/ping-containers.sh
# Change the permissions # Change file permissions to executable
chmod +x /usr/local/bin/ping-containers.sh chmod +x /usr/local/bin/ping-containers.sh
# Create service # Create ping-containers.service
echo '[Unit] echo '[Unit]
Description=Pings containers every 5 minutes and restarts if necessary Description=Pings containers every 5 minutes and restarts if necessary
[Service] [Service]
Type=simple Type=simple
# Include the container ID at the end of the line where ExecStart=/usr/local/bin/ping-containers.sh is specified,
# to indicate which container should be excluded. Example: ExecStart=/usr/local/bin/ping-containers.sh 100 102
ExecStart=/usr/local/bin/ping-containers.sh ExecStart=/usr/local/bin/ping-containers.sh
Restart=always Restart=always
StandardOutput=file:/var/log/ping-containers.log StandardOutput=file:/var/log/ping-containers.log