Skip to content

Instantly share code, notes, and snippets.

@Deathproof76
Created September 23, 2023 20:03
Show Gist options
  • Save Deathproof76/af1ec37bd377a36adbbea5cc5f8486b3 to your computer and use it in GitHub Desktop.
Save Deathproof76/af1ec37bd377a36adbbea5cc5f8486b3 to your computer and use it in GitHub Desktop.
#!/bin/bash
set -x # Enable debug mode
# Function to check the health of the plex container
check_plex_health() {
# Directly print the logs to the terminal for debugging
docker logs --tail 10 plex
# Get the last 10 lines of log output from the container named plex
plex_logs=$(docker logs --tail 10 plex)
echo "Plex logs: $plex_logs" # Print the logs for debugging
# Count the number of times the "Sqlite3: Sleeping for 200ms to retry busy DB" line appears in the logs
count=$(grep -c "Sqlite3: Sleeping for 200ms to retry busy DB" <<< "$plex_logs")
echo "Count of 'Sqlite3' lines: $count" # Print the count for debugging
# If the line appears more than 3 times, return a non-zero exit code to indicate a failure
if [ "$count" -gt 3 ]; then
echo "Oh no, I guess we have to restart the container."
return 1
fi
# Otherwise, return a success exit code and print a message to the terminal
echo "Plex seems to be okay! I just checked!"
return 0
}
# Run the health check function continuously
while true; do
check_plex_health
# If the health check fails, restart the plex container
if [ $? -ne 0 ]; then
echo "Restarting Plex container..." # Debug statement
docker restart plex
fi
# Sleep for 3 minutes between each health check
sleep 180
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment