SQLITE | BASH | handle ‘database locked’

function do_sql {
DB="$1"
shift
test -r "$DBDIR/$DB"
if [ $? -ne 0 ]; then
write2log "Creating DB $DB"
create_db "$DB"
fi
if [ "x$@" == "x" ]; then return 1; fi
LOOP=1;MAXLOOP=10
while true;do
$SQLITE "$DBDIR/$DB" "$@" 2>/dev/null && return 0
LOOP=$(( $LOOP + 1 ))
RD="0.$(( ( RANDOM % 9 ) + 1 ))"
write2log "SQL RETRY #$LOOP (WAITING $RD s) FOR $@"
sleep $RD
if [ $LOOP -gt $MAXLOOP ]; then
$SQLITE "$DBDIR/$DB" "$@"
return $?
fi
done
return 2
}

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s