I have a cron task that runs periodically. This task depends on a condition to be valid in order to complete its processing. In case it matters this condition is just a SELECT for specific records in the database. If the condition is not satisfied (i.e the SELECT does not return the result set expected) then the script exits immediately.
This is bad as the condition would be valid soon enough (don't know how soon but it will be valid due to the run of another script).
So I would like somehow to make the script more robust. I thought of 2 solutions:
- Put a
while
loop andsleep
constantly until the condition is valid. This should work but it has the downside that once the script is in the loop, it is out of control. So I though to additionally after waking up to check is a specific file exists. If it does it "understands" that the user wants to "force" stop it. - Once the script figures out that the condition is not valid yet it appends a script in crontab and stops. That seconds script continually polls for the condition and if the condition is valid then restart the first script to restart its processing. This solution to me it seems to work but I am not sure if it is a good solution. E.g. perhaps programatically modifying the crontab is a bad idea?
Anyway, I thought that perhaps this problem is common and could have a standard solution, much better than the 2 I came up with. Does anyone have a better proposal? Which from my ideas would be best? I am not very experienced with cron tasks so there could be things/problems I could be overseeing.