I have a few custom tables in the database and I need to clean expired data from time to time.
If I was using WP-Cron, it would be easy, but WP-Cron sucks and is a no-go (especially for what I am doing). Having that in mind, I need to launch some code using crontab
. How should I do it in a clean, effective manner that would not require nasty hacks?
I was thinking of creating a REST endpoint, but how to protect it so it could only be opened by the crontab?
The solution should be inside the plugin folder so it could be stored in GIT easily (i.e. nasty workarounds storing files in the main folder (a.k.a public_html
) are not welcomed).
Was searching for a good solution for hours with no success... Thanks in advance!
I have a few custom tables in the database and I need to clean expired data from time to time.
If I was using WP-Cron, it would be easy, but WP-Cron sucks and is a no-go (especially for what I am doing). Having that in mind, I need to launch some code using crontab
. How should I do it in a clean, effective manner that would not require nasty hacks?
I was thinking of creating a REST endpoint, but how to protect it so it could only be opened by the crontab?
The solution should be inside the plugin folder so it could be stored in GIT easily (i.e. nasty workarounds storing files in the main folder (a.k.a public_html
) are not welcomed).
Was searching for a good solution for hours with no success... Thanks in advance!
Share Improve this question asked Aug 13, 2020 at 10:48 Mindaugas JakubauskasMindaugas Jakubauskas 1646 bronze badges 2 |1 Answer
Reset to default 1The solution is as @Tom J Nowell mentioned to launch WP Cron via crontab (and disable it on user visits).
crontab
instead? WP Cron normally relies on a nonblocking request when a user visits because WP can't modify crontab, but if you have access to crontab you can trigger it at regular intervals so it always runs even if nobody visits, and at specific intervals. On my own site I trigger it every 5 minutes via a cron job, and I recently changed it to use a job manager such as Cavalcade to run cron events. Even WP CLI can trigger it – Tom J Nowell ♦ Commented Aug 13, 2020 at 11:53