in reply to one script at a time (race condition)
I see you already have your problem solved. I wanted to mention, though, that instead of using a file, you could use the database itself. At the start of the run, check whether a lock entry exists in the table. If not, add an entry to the table. Do it in a transaction so you can avoid any race conditions. Then, when the run ends, delete the entry.
I only mention it because you're already pounding on the database.
When your only tool is a hammer, all problems look like your thumb.