{"id":2009226,"date":"2022-03-16T08:43:45","date_gmt":"2022-03-16T08:43:45","guid":{"rendered":"https:\/\/wpx.net\/blog\/?p=2009226"},"modified":"2023-11-02T10:38:09","modified_gmt":"2023-11-02T10:38:09","slug":"disable-wp-cron-now","status":"publish","type":"post","link":"https:\/\/wpx.net\/blog\/disable-wp-cron-now\/","title":{"rendered":"Disable WP Cron Now"},"content":{"rendered":"\n<p>In this post I will show you how to disable WP Cron and explain why you should do that.<\/p>\n\n\n\n<p>Since its inception, WordPress is being developed to be simple to install and configure and to allow running on a large range of platforms where end-user have different levels of control.<\/p>\n\n\n\n<p>This certainly contributes to the popularity of WordPress, but over the years some of the architectural decisions have proven to be less than optimal.<\/p>\n\n\n\n<p>The way WordPress runs periodic housekeeping tasks is an example of this issue. The solution works, but it can decrease the performance of infrequently visited and heavily trafficked websites alike.<\/p>\n\n\n\n<p>But what are these periodic tasks and why are they needed? Let me explain.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"First_things_first_%E2%80%94_what_is_a_cron_job\"><\/span>First things first \u2014 what is a cron job?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>All modern operating systems have some kind of scheduler that allows automated execution of tasks at regular, predetermined intervals. This functionality has existed since the 1970s and these periodic tasks are called cron jobs.<\/p>\n\n\n\n<p>Funny enough, the name itself is a spelling error; Brian Kernighan \u2014 one of the fathers of the UNIX operating system and the C programming language \u2014 who implemented the idea, wanted a term that infers a relationship with time, and chose the Greek word for time (chronos) but, as he admitted later, \u201cI never could spell\u201d.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Why_WordPress_needs_to_execute_periodic_tasks\"><\/span>Why WordPress needs to execute periodic tasks<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Like essentially all other modern software, WordPress needs to execute certain housekeeping tasks from time to time to do its job and stay healthy. Examples of such tasks are:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>scheduled checks for updates to the WordPress core, themes, plugins, and translations;<\/li>\n\n\n\n<li>the sending of transactional emails (for example, to notify the administrator if a comment under a blog post has been flagged for moderation);<\/li>\n\n\n\n<li>updates to database tables, renewal of expired cached pages, etc.<\/li>\n<\/ul>\n\n\n\n<p>WordPress is designed to be as independent of its server environment as possible. Especially in the beginning (WordPress came out in 2003 \u2014 nearly 2 decades ago!) there weren\u2019t very many technically capable users. Many web hosting services did not offer their users the ability to schedule cron jobs for fear they would break things.<\/p>\n\n\n\n<p>That is why the WordPress developers had to come up with a way to go around that limitation. Their solution was to make WordPress trigger an internal task scheduler on every customer visit. This task scheduler would look at the current time, check if one or more cron jobs were due for execution, and start the cron process if needed.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Unwanted_delays\"><\/span>Unwanted delays<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>There are two problems with this solution. The first one is when the website receives too little traffic; it means that whenever a visitor comes after several hours of inactivity, there might be a long queue of tasks that are due for execution. And even though the website usually loads fast, that unlucky user will get a degraded experience while the tasks are executed.<\/p>\n\n\n\n<p>The second problem is when the website receives more traffic. The scheduler starts a check every time a new visitor comes, which could mean these checks pile up unnecessarily.<\/p>\n\n\n\n<p>Luckily, there is a fix for that situation, and I highly recommend that you take advantage of it.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Replacing_WP_Cron_with_a_real_cron_job\"><\/span>Replacing WP Cron with a real cron job<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>This requires two steps:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Configure WordPress to stop using WP Cron<\/li>\n\n\n\n<li>Configure an external task that runs on a specific schedule<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"How_to_deactivate_WP_Cron\"><\/span>How to deactivate WP Cron<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>To do this, you need to open the&nbsp;<code>wp-config.php<\/code>&nbsp;file in the root folder of your website, and scroll down until you find the following lines near the end of the file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>define( 'WP_DEBUG', false );\n\/* Add any custom values between this line and the \"stop editing\" line. *\/\n\n\/* That's all, stop editing! Happy publishing. *\/<\/code><\/pre>\n\n\n\n<p>Note: the contents of your\u00a0<code>wp-config.php<\/code>\u00a0might differ and there could be other lines there. This should not worry you; the sequence of defined directives is not important.<\/p>\n\n\n\n<p>Add this declaration between the two comment lines:&nbsp;<code>define('DISABLE_WP_CRON', true);<\/code><\/p>\n\n\n\n<p>The file should look like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>define( 'WP_DEBUG', false );\n\/* Add any custom values between this line and the \"stop editing\" line. *\/\ndefine('DISABLE_WP_CRON', true);\n\/* That's all, stop editing! Happy publishing. *\/<\/code><\/pre>\n\n\n\n<p>Save the file and close it; this is enough. However, remember this change has an immediate effect, and as soon as you disable WP Cron, your website will lose the ability to manage itself. That is why you should complete the next step soon after this one.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"How_to_create_a_real_cron_job\"><\/span>How to create a real cron job<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h4 class=\"wp-block-heading\">For WPX customers<\/h4>\n\n\n\n<p>If you are a customer of WPX, this one is a real piece of cake. You simply need to send a ticket to the friendly WPX support team from&nbsp;<a href=\"https:\/\/wpx.net\/tickets\/new\/\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/wpx.net\/tickets\/new\/<\/a>&nbsp;and ask them to set up a regular cron job for your WordPress site.&nbsp;<\/p>\n\n\n\n<p>The WP-Cron task is activated by loading the following URL (where example.com should be replaced with your domain name):https:\/\/example.com\/wp-cron.php?doing_wp_cron<\/p>\n\n\n\n<p>But of course, WPX support knows that. All you need to ask them is to set up a cron job to replace WP Cron, or even simply refer them to this article.<\/p>\n\n\n\n<p>Wait for WPX support to confirm that they are finished, then watch how your website behaves in the next 1-2 days or so; whether it still checks for updates regularly and so on. If something appears wrong, let support know.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">For other shared\/managed hosting services<\/h4>\n\n\n\n<p>If you are not a WPX customer, you could ask the support team the same; the only difference is that they will most likely not respond so quickly. That is why you should postpone adding the DISABLE_WP_CRON line to the&nbsp;<code>wp-config.php<\/code>&nbsp;file until you hear back from them (or even just ask them to add the line themselves after they are done with the cron job itself).<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Via the hosting control panel<\/h4>\n\n\n\n<p>The control panel provided by your hosting (e.g. Cpanel, DirectAdmin) might offer access to the cron job list and allow you to add a new cron job. Have a look around and if you find it, configure it to execute the following command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>wget -q -O - https:\/\/example.com\/wp-cron.php?doing_wp_cron &gt;\/dev\/null 2&gt;&amp;1<\/code><\/pre>\n\n\n\n<p>This is the Cpanel \u2018Add New Cron Job\u2019 screen:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"613\" height=\"594\" src=\"https:\/\/wpx.net\/blog\/wp-content\/uploads\/2023\/11\/image-123.png\" alt=\"\" class=\"wp-image-2016554\" srcset=\"https:\/\/wpx.net\/blog\/wp-content\/uploads\/2023\/11\/image-123.png 613w, https:\/\/wpx.net\/blog\/wp-content\/uploads\/2023\/11\/image-123-300x291.png 300w, https:\/\/wpx.net\/blog\/wp-content\/uploads\/2023\/11\/image-123-200x194.png 200w\" sizes=\"auto, (max-width: 613px) 100vw, 613px\" \/><\/figure>\n\n\n\n<p>And here is what the DirectAdmin \u2018Create Cron Job\u2019 screen looks like:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"820\" src=\"https:\/\/wpx.net\/blog\/wp-content\/uploads\/2023\/11\/image-124-1024x820.png\" alt=\"\" class=\"wp-image-2016555\" srcset=\"https:\/\/wpx.net\/blog\/wp-content\/uploads\/2023\/11\/image-124-1024x820.png 1024w, https:\/\/wpx.net\/blog\/wp-content\/uploads\/2023\/11\/image-124-300x240.png 300w, https:\/\/wpx.net\/blog\/wp-content\/uploads\/2023\/11\/image-124-768x615.png 768w, https:\/\/wpx.net\/blog\/wp-content\/uploads\/2023\/11\/image-124-200x160.png 200w, https:\/\/wpx.net\/blog\/wp-content\/uploads\/2023\/11\/image-124.png 1244w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Via_PHP_script\"><\/span>Via PHP script<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>In the rare and weird situation, your web host doesn\u2019t want to cooperate with the adding of a cron job, you could try adding one via PHP.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>To do that, create a file called\u00a0<code>cron.php<\/code>\u00a0in your web root folder and put the following code inside (again, I\u00a0<strong>strongly<\/strong>\u00a0remind you to replace \u2018example.com\u2019 with your real domain, including the www part if you are using it to open your website):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;!--?php\n&lt;?php\n\/\/ Add cron job while avoiding duplication shell_exec(\"(crontab -l ; echo '*\/15 * * * * wget -q -O - https:\/\/example.com\/wp-cron.php?doing_wp_cron >\/dev\/null 2>&amp;1') | sort | uniq | crontab\") ;\n\n\/\/ Output cron jobs list\n$output = shell_exec('crontab -l') ; echo \"$output\" ; ?><\/code><\/pre>\n\n\n\n<p>There is a possibility that the web host which does not allow setting user cron jobs will also prohibit execution of shell commands via PHP but you have to try it to know for sure. And if it doesn\u2019t work, you should really think about changing hosts. Seriously.<\/p>\n\n\n\n<p>Note: the php script was adapted from\u00a0<a rel=\"noreferrer noopener\" href=\"https:\/\/adamtheautomator.com\/list-cron-jobs\/\" target=\"_blank\">this tutorial by Matt Nikonorov<\/a>\u00a0about managing cron jobs via the command line. The elegant thing about his solution is how the existing cron job list and the newly added cron job get piped through several commands: \u00a0(1) sort arranges them in alphabetic order and (2) uniq removes the repeating lines. The output then gets piped back to crontab. Using sort+uniq guarantees that no matter how many times you execute that script, you will not get duplicate cron jobs.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Via_3rd_party_Web_GUI\"><\/span>Via 3rd party Web GUI<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>If you do not have access to the cron job and don\u2019t want to wait for a sluggish response from the support of your web host, there is another option: a third-party service called&nbsp;<a href=\"https:\/\/www.easycron.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">EasyCron<\/a>&nbsp;which has a free tier and very inexpensive paid tiers starting from $1\/mo.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1011\" height=\"616\" src=\"https:\/\/wpx.net\/blog\/wp-content\/uploads\/2023\/11\/image-125.png\" alt=\"\" class=\"wp-image-2016556\" srcset=\"https:\/\/wpx.net\/blog\/wp-content\/uploads\/2023\/11\/image-125.png 1011w, https:\/\/wpx.net\/blog\/wp-content\/uploads\/2023\/11\/image-125-300x183.png 300w, https:\/\/wpx.net\/blog\/wp-content\/uploads\/2023\/11\/image-125-768x468.png 768w, https:\/\/wpx.net\/blog\/wp-content\/uploads\/2023\/11\/image-125-200x122.png 200w\" sizes=\"auto, (max-width: 1011px) 100vw, 1011px\" \/><figcaption class=\"wp-element-caption\">Creating a cron job inside EasyCron<\/figcaption><\/figure>\n\n\n\n<p>Note: the only drawback of the EasyCron free tier is that your account will expire in 30 days and you need to recreate it. So this solution is best used in combination with the previous one when you contact support. Even if they respond in 24-72 hours, your site will be covered by the EasyCron job during that time.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Recap\"><\/span>Recap<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>If everything has gone to plan, then congratulations! You have decoupled the execution of housekeeping tasks from customer visits.<\/p>\n\n\n\n<p>No longer will the rare visitor of your new and not popular (just yet!) blog wait for several seconds while WordPress tends the execution of a whole pile of overdue tasks.<\/p>\n\n\n\n<p>And if you have made it past that first stage and your website now serves multiple visitors per minute, you will have removed the unnecessary additional burden from the web server which until now had to perform slow repetitive, and unnecessary PHP jobs that increase CPU load.<\/p>\n\n\n\n<p>You also witnessed some examples of \u2018under the hood\u2019 operations in which we interacted directly with the OS that runs on your web hosting server. If you find such tricks interesting and don\u2019t find this overly technical, please let me know in the comments below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this post I will show you how to disable WP Cron and explain why you should do that. Since its inception, WordPress is being developed to be simple to install and configure and to allow running on a large range of platforms where end-user have different levels of control. This certainly contributes to the [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":2015327,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"cybocfi_hide_featured_image":"","footnotes":""},"categories":[88],"tags":[],"ppma_author":[111],"class_list":["post-2009226","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-development"],"blocksy_meta":[],"authors":[{"term_id":111,"user_id":3,"is_guest":0,"slug":"ivan-arnaudov","display_name":"Ivan Arnaudov","avatar_url":"https:\/\/secure.gravatar.com\/avatar\/168c2caf170cc2d43ef2ec57bc8af7347f86a5f5e825ee86026e52878df349b2?s=96&d=mm&r=g","0":null,"1":"","2":"","3":"","4":"","5":"","6":"","7":"","8":""}],"_links":{"self":[{"href":"https:\/\/wpx.net\/blog\/wp-json\/wp\/v2\/posts\/2009226","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wpx.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wpx.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wpx.net\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/wpx.net\/blog\/wp-json\/wp\/v2\/comments?post=2009226"}],"version-history":[{"count":12,"href":"https:\/\/wpx.net\/blog\/wp-json\/wp\/v2\/posts\/2009226\/revisions"}],"predecessor-version":[{"id":2016558,"href":"https:\/\/wpx.net\/blog\/wp-json\/wp\/v2\/posts\/2009226\/revisions\/2016558"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/wpx.net\/blog\/wp-json\/wp\/v2\/media\/2015327"}],"wp:attachment":[{"href":"https:\/\/wpx.net\/blog\/wp-json\/wp\/v2\/media?parent=2009226"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wpx.net\/blog\/wp-json\/wp\/v2\/categories?post=2009226"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wpx.net\/blog\/wp-json\/wp\/v2\/tags?post=2009226"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/wpx.net\/blog\/wp-json\/wp\/v2\/ppma_author?post=2009226"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}