Skip to content

Instantly share code, notes, and snippets.

@Deathproof76
Created October 3, 2023 08:06
Show Gist options
  • Save Deathproof76/a4edfcb737f7743ab109312065d08e48 to your computer and use it in GitHub Desktop.
Save Deathproof76/a4edfcb737f7743ab109312065d08e48 to your computer and use it in GitHub Desktop.
busy db cron checkv2 tbu
#!/bin/bash
# Timestamp file to keep track of last "busy db" message
timestamp_file="/tmp/plex_busy_db_timestamp"
# Function to check the health of the plex container
check_plex_health() {
# Get the last 10 lines of log output from the plex container
plex_logs=$(docker logs --tail 10 plex)
# Count the number of times the "busy DB" line appears in the logs
count=$(grep -c "Sqlite3: Sleeping for 200ms to retry busy DB" <<< "$plex_logs")
# If the line appears more than 4 times, return a non-zero exit code to indicate a failure
if [ "$count" -gt 4 ]; then
echo "Oh no, I guess we have to restart the container."
docker restart plex
return 0
fi
# Get the last line of log output
last_line=$(docker logs --tail 1 plex)
# Check if the last line contains the "busy DB" message
if grep -q "Sqlite3: Sleeping for 200ms to retry busy DB" <<< "$last_line"; then
# If the timestamp file already exists, check the time difference
if [ -f "$timestamp_file" ]; then
# Get the current time and the time from the timestamp file
current_time=$(date +%s)
last_time=$(cat "$timestamp_file")
# Calculate the time difference
time_diff=$((current_time - last_time))
# If the time difference is more than 90 seconds, restart the server
if [ "$time_diff" -gt 150 ]; then
echo "Oh no, I guess we have to restart the container."
docker restart plex
# Remove the timestamp file after restarting the container
rm "$timestamp_file"
fi
else
# If the timestamp file does not exist, create it with the current time
date +%s > "$timestamp_file"
fi
else
# If the last line does not contain the "busy DB" message, remove the timestamp file
[ -f "$timestamp_file" ] && rm "$timestamp_file"
fi
}
# Run the health check function
check_plex_health
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment