Have the pkg process perform a quick case-insensitive search for "database is locked" in the output, and recursively try the command again if necessary.