I have a cron which runs daily (at midnight) and modifies a few database tables of a web application.
I want to make sure it worked OK before letting users continue to use the application. Currently some functions in the app. are temporarily disabled for 1 minute.
Anyway since I’m on a shared server I cannot be sure the cron will execute without errors. There could be a database connection error etc. So I need to check if the cron executed.
It could be checked at PHP level and I have one solution in my mind.
How would you check whether a cron is executed without errors in this situation?
How do you let the app know whether the cron was successful? I ask because I don’t see how that would work. As far as I can see there are two options with both the same drawback.
Via the database
Set some value in a database to true or false, but if the cron was not successful because no database connection could be made then you can also not put in the database that the cron did not work correctly and the app would not know the when the cron didn’t run correctly.
Via a file
Write a value to a file indicating if the cron worked correctly. If the file is for some reason not writable (HDD full, wrong permissions) the app would not know when the cron didn’t run correctly.
Personally, I would invoke a script which emailed my aging mother, woke her from her slumbers and made her respond at 00:01am.
OK, it might be old-skool, but it should work till the end of the epoch.
Simply updating (at the end of the script) and checking the timestamp, as I mentioned before. I think I won’t need exit status for now. If the database connection fails the timestamp cannot be updated as it’s stored in the same database.
Out of curiousity, what do you have?
Okay, similar to what I have. Thanks.
What I would do is something like this:
if (cannot connect to db)
if (something else is wrong)
do some stuff
update db to current timestamp
and then make the cron job for
job.php && update.php
If job.php executed normally it will have exit status 0, and update.php will be executed, otherwise update.php won’t be executed.
What I thought was to insert the current timestamp into a database field when a cron executed successfully. Then I could check it with PHP.
Like in the particular case above; if the current timestamp in the database belongs to yesterday, it would mean cron failed today.