WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] run wp-cron.php in a timebased way (16 posts)

  1. zerpex
    Member
    Posted 1 year ago #

    A lot of wordpress people, know about the wp-cron.php file, what it kind of does, and how it's run.

    My feature request, is the way the wp-cron is run, because the current method really suck, if you have a high traffic website.

    A small example, is from 3.50pm to 3.53pm today, the wp-cron.php was requested 143 times, not only it slows down the site, it also take a lot of ressources, why is the cron being called that many times?

    [03/Jan/2013:15:54:26 +0100] "POST /wp-cron.php?doing_wp_cron=1357224865.9989430904388427734375 HTTP/1.0" 200 - "-" "WordPress/3.5;

    And can you make some kind of way, that it runs maximum like every minute or something? Because 143 times within 180 seconds is quiet a lot IMO.

    - Zerpex

  2. dmahler
    Member
    Posted 1 year ago #

    If you get an answer to this I'd like to know. I posted a question about this same thing and no one ever answered me. I have a client that's getting threatened by their hosting provider and this file is running all the time on one of the websites. All the other WordPress installs aren't doing the same thing and they are all updated to 3.5 so I don't know what the heck is causing this issue.

    It's a shame we can't get answers.

  3. WPAndrew
    Member
    Posted 1 year ago #

    This executes every time a page is viewed, if you have a lot of visitors it can be a problem. I am convinced this is the reason why my admin has become glacially slow and my site often crashes when making edits.

    This is a solid article however it is unclear where to paste the code in wp 3.5 as the directions appear to be a little outdated.

    http://www.inmotionhosting.com/support/website/wordpress/disabling-the-wp-cronphp-in-wordpress

    Basically you want to disable the cron in the file and manually configure it to run with less frequency using server settings.

    Changing the settings has implications but for me, I don't use WP as a blog and I get fair amount of traffic so I cant wait to disable. If anyone knows where exactly to paste this code in the wordpress 3.5 cron file, it would be great to share.

  4. fonglh
    Member
    Posted 1 year ago #

    however it is unclear where to paste the code in wp 3.5

    What it says. In the wp-config.php file under the database settings.

  5. zerpex
    Member
    Posted 1 year ago #

    I'm a hosting provider myself - I know I can easily disable the cron, via wp-config.php - and then set up a manual cron job in the clients control panel.

    But I think generally this should be avoided, not because it doesn't work - but more because, yes we can fix a lot of problems, by doing this. But it's not really 'fixing' the problem, its just avoiding the problem.

    If the WP-devs actually did care, they would surely make some more logic behind it, to make sure it only get run x amount of minutes at least. Instead of just saying "You can disable it like this".

    I hope some devs see this topic, and will look into it, because it suck if you need to do it for a lot of sites :)

  6. Changing the settings has implications but for me, I don't use WP as a blog and I get fair amount of traffic so I cant wait to disable. If anyone knows where exactly to paste this code in the wordpress 3.5 cron file, it would be great to share.

    Add this to your wp-config.php file:

    define( 'DISABLE_WP_CRON', true );

    Next you will have to create a real server cron job to ping (I use curl) http://example.org/wp-cron.php?doing_wp_cron. You will need to check with your host or control panel documentation on how to do this. If you are command line savvy just create a crontab.

    $ crontab -e

    */1 * * * * curl -I http://example.org/wp-cron.php?doing_wp_cron

    This is saying that every minute I want my server to execute that command which will ping wp-cron and make it run.

  7. The Hack Repair Guy
    Member
    Posted 1 year ago #

    I wrote an article on this not too long ago as well that's worked quite well for my clients:
    http://hackrepair.com/how-can-i-improve-the-performance-of-my-wordpress-website

    [If you want suggestions for your website, use the Your WordPress forum]

  8. If you use multisite, you're better off using cron with the domain, since you have to call it for each site on your network.

  9. dmahler
    Member
    Posted 1 year ago #

    Thank you all for the responses.

    I was able to disable cron and set up a cron run on the server. This seemed to help greatly.

    Since I stopped that load on the server, it became apparent that we had other issues which I'm still correcting by having to modify the .htaccess file to block nasty bots and attacks from China.

    Sigh..... a webmasters work is never done! :P

  10. PJ Brunet
    Member
    Posted 1 year ago #

    I disabled DISABLE_WP_CRON, now my question:

    Is it OK for me to run wp-cron.php twice overlapping?

    Sometimes it runs in just a few seconds. Sometimes it needs several minutes!

    Like say I have it running every 10 mins with a real crontab

    */10 * * * etc.

    And it needs a full 15 minutes to do its business.

    Can two "instances" of wp-cron.php safely run together? Does the oldest instance die and the new ones takes over? Is any data lost from the first instance? Scheduled posts not published?

  11. zerpex
    Member
    Posted 1 year ago #

    WPCron locks itself, and keep track of its progress, so no, 2 crons will never do the same task once. But both crons can run at same time.

  12. 70relay
    Member
    Posted 1 year ago #

    This thread has been helpful, but I'm not sure the best solution.

    My site is at Dreamhost, and I used their cpanel ui to set up a cron instead of doing it directly.

    Below are what I've tried and the log file results (from access.log).

    1. Using wgetn(roughly per Hack Repair Guy):
    command: wget -q --delete-after http://annieandre.com/wp-cron.php

    log file: 5.119.198.89 - - [19/Apr/2013:23:50:01 -0700] "GET /wp-cron.php HTTP/1.0" 200 194 "-" "Wget/1.12 (linux-gnu)"

    2. Using curl (from Chris O. above):
    command: curl -I http://annieandre.com/wp-cron.php?doing_wp_cron

    log file: 75.119.198.89 - - [19/Apr/2013:14:00:02 -0700] "HEAD /wp-cron.php?doing_wp_cron HTTP/1.1" 301 218 "-" "curl/7.21.0 (x86_64-pc-linux-gnu) libcurl/7.21.0 OpenSSL/0.9.8o zlib/1.2.3.4 libidn/1.15 libssh2/1.2.6"

    3. Using php:
    command: /usr/local/php5/bin/php -q /home/username/annieandre.com/wp-cron.php

    log file: Warning: require_once(./wp-load.php): failed to open stream: No such file or directory in /home/username/annieandre.com/wp-cron.php on line 26

    Fatal error: require_once(): Failed opening required './wp-load.php' (include_path='.:/usr/local/lib/php:/usr/local/php5/lib/pear') in /home/username/annieandre.com/wp-cron.php on line 26

    4. Last, here's the log file output before I disabled wp-cron.php in wp-config:
    75.119.198.89 - - [19/Apr/2013:03:53:15 -0700] "POST /wp-cron.php?doing_wp_cron=1366368795.4622540473937988281250 HTTP/1.0" 200 158 "-" "WordPress/3.5.1; http://www.annieandre.com"

    So the php method in the cron cpanel throws an error. The curl gets a 301 then a 200, a little odd and I'm not sure why. Right now I've been running the wget method for about a day and that seems fine.

    Thoughts? Leave it with wget?

    Again thanks to all for posting suggestions above.

  13. Ipstenu-DH
    DreamHost Rep
    Posted 1 year ago #

    70relay, I use this on DreamHosts's panel

    curl http://domain.com/wp-cron.php

    Narry a problem :)

  14. 70relay
    Member
    Posted 1 year ago #

    Thank you Ipstensu. I'll try that variation of curl (my example above had slightly different syntax).

    Cheers.

  15. queberican351
    Member
    Posted 1 year ago #

    Ooh, goody goody. Thank you to all the contributors here. Searched pages upon pages of not so clear basics, but the solutions and links here seem to be a bit more thorough. I have a third party plugin that doesn't natively run any cron jobs, basically have to go into the settings page and hit "go". Trying to automate this. I'm extremely familiar with server side cron and bash shell scripting, but never attacked something like this before. Still getting to know my new friend php a little better.
    Any productive links not written in "masters degree of data programming" language would be awesome as well :)
    Thanks,
    [Signature moderated]

  16. Paul G.
    Member
    Posted 8 months ago #

    I'm not sure if you're all satisfied with what you've come up with here, but I found as I was reading through the code, there's a way to prevent the cron running every minute. You can limit it by defining, probably in your wp-config.php file, the Cron lock timeout.

    So basically, the cron will prevent overlaps of itself by creating a database-based lock with a timestamp. It says this:

    if the current lock timestamp is greater than 60s ago, I'll proceed, otherwise I'll quit so I don't step over another cron process that could be already running.

    Very simply then, you can increase the length of the lock period.
    Example: if you want no more than 1 cron to run every 10 minutes, set the lock time out to 600 (10x60).

    So, in your wp-config.php you'll want:

    define( 'WP_CRON_LOCK_TIMEOUT', 600 ) ;

    I hope that helps.
    Paul.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags