I’m betting that someone has already solved this and maybe I’m using the wrong search terms for google to tell me the answer, but here is my situation.
I have a script that I want to run, but I want it to run only when scheduled and only one at a time. (can’t run the script simultaneously)
Now the sticky part is that say I have a table called ‘myhappyschedule’ which has the data I need and the scheduled time. This table can have multiple scheduled times even at the same time, each one would run this script. So essentially I need a queue of each time the script fires and they all need to wait for each one before it to finish. (sometimes this can take just a minute for the script to execute sometimes its many many minutes)
What I’m thinking about doing is making a script that checks myhappyschedule every 5 min and gathers up those that are scheduled, puts them into a queue where another script can execute each ‘job’ or occurrence in the queue in order. Which all of this sounds messy.
To make this longer – I should say that I’m allowing users to schedule things in myhappyschedule and not edit crontab.
What can be done about this? File locks and scripts calling scripts?
add a column
exec_statustomyhappytable(maybe alsotime_startedandtime_finished, see pseudocode)run the following cron script every x minutes
pseudocode of cron script:
This way, the script first checks if none of the commands is running, then runs first not-yet run command, until there are no more commands to be run at the given moment. Also, you can see what command is executing by querying the database.
A potential pitfall: if the cron script is killed, a scheduled task will remain in ‘executing_now’ state. That’s what the pid lock at beginning and end is for: to see if the cron script terminated properly. pseudocode of create/check pidlock: