Skip to content

Instantly share code, notes, and snippets.

@Deathproof76
Last active March 26, 2025 16:24
Show Gist options
  • Select an option

  • Save Deathproof76/bd547640e8a13d738dbee68fe8aef156 to your computer and use it in GitHub Desktop.

Select an option

Save Deathproof76/bd547640e8a13d738dbee68fe8aef156 to your computer and use it in GitHub Desktop.
Crontab restart/repair script for Plex "Sqlite3: Sleeping for 200ms to retry busy DB"
#!/bin/bash
# Plex busy DB workaround
# Set the maximum number of allowed occurrences of the Sqlite3: Sleeping for 200ms to retry busy DB string
# about 3 times usually indicates an instance that can't recover by itself
# if your instance restarts your docker logs should reset, or "cover up" the busy DB lines, afaik
MAX_OCCURRENCES=3
# this part "docker logs --tail 10 plex" "--tail 10" means the last ten lines of the container logs
# of the container named "plex", change the name according to your setup
# you can try "docker logs --tail 10 plex" in the terminal and if everything is setup correct
# it will spit out the last 10 lines of the logs and should work
LOG_LINES=$(docker logs --tail 10 plex)
# Count the number of occurrences of the specified string in the log lines
OCCURRENCES=$(echo "$LOG_LINES" | grep -o "Sqlite3: Sleeping for 200ms to retry busy DB." | wc -l)
# If the health check fails, restart the plex container
if [ "$OCCURRENCES" -gt "$MAX_OCCURRENCES" ]; then
# Restart the container
# Again this part "docker restart plex" assumes that your container is named plex, change the name according to your setup
docker restart plex
fi
@Deathproof76
Copy link
Author

Deathproof76 commented Jun 28, 2023

Basically a last resort. It happens extremely rarely on my server currently. But if it does, the script will resolve it. It checks for "Sqlite3: Sleeping for 200ms to retry busy DB" in the docker logs. If it happens more than 4 times in the last 10 lines the container will be restarted, which should get plex going again.
see here for more info and also continuous bash script version https://www.reddit.com/r/PleX/comments/zrfosu/sqlite3_sleeping_for_200ms_to_retry_busy_db_i/

Just put it into crontab -e like

*/1 * * * * "/home/max/Desktop/scripts/plex scripts/plexdb_healthcheck.sh"

in this case it'll check every minute for a busy db

(it assumes that you running plex in docker, that the container is named "plex" and that you are able to use docker without "sudo". see here https://docs.docker.com/engine/install/linux-postinstall/ )

edit: updated the cron script a bit and added explanations

@Fespinoza831
Copy link

Im trying to use the script in unraid via the user scripts. is this the only part I need to modify "docker restart Plex-Media-Server" with the name of my container

@BBergle
Copy link

BBergle commented Feb 6, 2024

My Plex logs seem to retain after the container restarts, will that cause an issue for the script?

@KatCountryGirl
Copy link

Im trying to use the script in unraid via the user scripts. is this the only part I need to modify "docker restart Plex-Media-Server" with the name of my container

Did you ever figure this out? As I have 2 plex instances I want it to watch both

@Deathproof76
Copy link
Author

Im trying to use the script in unraid via the user scripts. is this the only part I need to modify "docker restart Plex-Media-Server" with the name of my container

Did you ever figure this out? As I have 2 plex instances I want it to watch both

you need two instances of the script in your case plexdb1_healthcheck.sh and plexdb2_healthcheck.sh for example. both need their own crontab entry. Also yes there are two cases in the script where you need to rename according to you instances name. I extended the commentary and explanations within the script

@KatCountryGirl
Copy link

KatCountryGirl commented Oct 10, 2024

Im trying to use the script in unraid via the user scripts. is this the only part I need to modify "docker restart Plex-Media-Server" with the name of my container

Did you ever figure this out? As I have 2 plex instances I want it to watch both

you need two instances of the script in your case plexdb1_healthcheck.sh and plexdb2_healthcheck.sh for example. both need their own crontab entry. Also yes there are two cases in the script where you need to rename according to you instances name. I extended the commentary and explanations within the script

Thank you so much for your reply. So I have 2 container "plex" and "plex2" I made a second because I share with my family out of state and the first one gets this error so often I have the second so there is always a running instance. I even moved my appdata to the cache and it didn't fix it. I'm using unraid so I set this up as a userscript and used a cronjob to run it every minute. It seems to work fine on plex. So what exactly so I need to change for plex2 to work. It just reads the docker image right so no need to like link to the appdata or anything like that? I appreciate your help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment