0

I want make a scheduler to update a table in the database every 24 hours.

After searching, I found this:

if (!wp_next_scheduled('jw_cron_hook')) {
    wp_schedule_event(time(), 'two-minutes', 'jw_cron_hook');
}

add_action('jw_cron_hook', function () {

    wp_mail('mm.mmm@gmail.com', 'salam', 'bedone init');

});
add_filter('cron_schedules', function ($schedules) {
    $schedules['two-minutes'] = array(
        'interval' => 120,
        'display' => 'Every Two Minutes'
    );
    return $schedules;
});

It works when someone visits my website, but doesn't when no-one visits. How can I create schedules that run whether someone visits my site or not?

  • There's no way to do this in WordPress, as WordPress is only called when someone visits your website. You'll need to look either at cron jobs at the system level, or alternatively an external service that can automatically 'visit' your website for you at a predefined interval. – Tim Malone Jul 24 '16 at 20:45
0

If you want to disable the "fake" WordPress cron and trigger your scheduled tasks, then you will need to use a real cron job to ping WordPress.

How to disable WP Cron

Just add a single line to your instances wp-config.php file:

define( 'DISABLE_WP_CRON', true );

How to set up a real cron job

The easiest might be to just open you hostings configuration user interface and click it together. In case you are like me not really satisfied by a manual click-job, you can use the crontab command on your server. You can search it up on your own, here are the basics:

  1. SSH into your server
  2. Figure out the user who owns the PHP process. ps -efj will tell you the process and the user who owns it
  3. Enter crontab -u $PHP_USER_NAME -l to see the existing jobs (backups for e.g.) for this user
  4. Add a new cronjob to ping your server – some examples

How to run WP Cron

Now you have to decide if you want to ping WordPress via HTTP or via the filesystem. Your options are: php, curl/wget.

In case you go with PHP, check whereis php to get the full path to the PHP executable. This way you do not have to rely on the global and the PATH environment variable:

/usr/local/bin/php /var/www/wp/wp-cron.php

In case you use wget:

wget -qO - https://example.com/wp-cron.php?doing_wp_cron >/dev/null 2>&1

That's pretty much it. Keep in mind that the ?doing_wp_cron query arg is important as cron will else not run.

Additional info

The crontab has a built in editor: crontab -e so you can type things fullscreen.

In case your cronjob throws an error (because there's an echo or other things that should not be there) and you just want to test things, you can route it to >/dev/null 2>&1, therefore hiding its output and errors. You do not have to do that.

Alternatives

When you have a very long running task or a task with an unpredictable running time – like importing things, reworking images, converting mp3 or mp4 files – then you might have a still running cronjob while there is already another one started, therefore failing. You can prevent that by using a crontab alternative like runwhen that has a job queue or, if you like the Go Language, use Jobber.

Another alternative is to use a uptime monitoring service. Those are available for free and you can directly ping your server (see above wget example) without doing any setup.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.