简体   繁体   中英

Cron job runs once only

Hi I am trying to create a cron job that runs every 2 min. This code is showing on WPCrontrol plugin that is running only once. Any idea why? I want it to run every 2 min irrespective if it is already scheduled. Furthermore the function to log in console is not working for some reason. Any idea how I can debug please?

    function my_cron_schedules($schedules){
   

    if(!isset($schedules["2min"])){
        $schedules["2min"] = array(
            'interval' => 2*60,
            'display' => __('Once every 2 min'));
    }
    return $schedules;
}
add_filter('cron_schedules','my_cron_schedules');

wp_schedule_event(time(), '2min', 'run_pdf_script_hook');

add_action ( 'run_pdf_script_hook', 'run_pdf_script' );

function run_pdf_script(){
    echo '<script>console.log("Welcome to Geeks for Geeks")</script>';
}

The console log output won't show up as it's a cron job that will be running on server end/ not on browser end.

To debug the cron output you should be adding/updating a test value in wp_options table.

for example.

function my_cron_schedules($schedules){
   
if(!isset($schedules["2min"])){
    $schedules["2min"] = array(
       'interval' => 2*60,
       'display' => __('Once every 2 min'));
    }
    return $schedules;
}
add_filter('cron_schedules','my_cron_schedules');

wp_schedule_event(time(), '2min', 'run_pdf_script_hook');

add_action ( 'run_pdf_script_hook', 'run_pdf_script' );

function run_pdf_script(){
    update_option('execution_log', date('Y-m-d H:i:s')); //monitor value of this in wp_options table and if you see different output your cron job is working;
}

If you want something to execute on user's browser end at each 2 min, implement setInterval javascript function at each 2 min and trigger AJAX request if required server side action.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM