douhuan1908 2017-10-27 14:25
浏览 46
已采纳

无法在Cpanel上设置cron作业

Trying to add a cron job on Cpanel to run every 5 minutes. I added this to the command input (removed personal details from the url)

/usr/bin/php -q /home/my_name/public_html/staging/the_web_site/wp-content/themes/my_child_theme/functions.php updateproducts

Then on the functions.php, I have this:

if (!empty($argv[1])) {
    switch ($argv[1]) {
        case "updateproducts":
            update_products(); 
            break;
    }
}

The update_products() function runs without any error manually if I trigger it with a button on admin page. But, no matter what I do on the cronjob tab, it doesn't run.

Any idea?

function update_products() {
   global $wpdb;
   $groups = get_groups_from_cron_jobs(100);
   foreach ( $groups as $group ) {
       $name = $group->group_name;
       $sql = $wpdb->prepare("SELECT * FROM {$wpdb->prefix}products WHERE name='%s'", $name);
       $products = $wpdb->get_results($sql);
       if ( !empty($products) ){
           $post_id = get_post_id_from_products($products);
           //if there isn't a parent_id then create a new product
           if ( !$post_id && $name != '' ) {
               $post_id = create_a_new_product($name);
           }
           // make sure that all products will have now a parent_id
           add_parent_id_on_products($name, $post_id);
           insert_product_attributes($post_id, $products);
           insert_product_variations($post_id, $products);
           delete_group_from_cron_jobs($name);
       }

   }
}

Edit: Based on the answers/comments, I made extra research and found that I can load the $wpdb on any script. What I did is this:

if (!empty($argv[1])) {
    switch ($argv[1]) {
        case "updateproducts":
            $path = $_SERVER['DOCUMENT_ROOT'];
            require( $path . '/staging/the_web_site/wp-load.php' );
            update_products(); 
            break;
    }
}

However, I still get the error on email:

Status: 500 Internal Server Error
X-Powered-By: PHP/5.6.31
Content-type: text/html; charset=UTF-8
  • 写回答

1条回答 默认 最新

  • dongqieli4164 2017-10-27 14:55
    关注

    I'm afraid there's some misunderstanding about how inter-operable the web environment and console environment are here.

    In short, your function expects WordPress to exist. It expects that the user has accessed the function through the WordPress application, and that global variables such as $wpdb exist (among other things). If you access this function via the admin page, then this is all true, and the function works.

    But if you access this function via the console (command line), none of this is true. WordPress does not exist within the context of that request. global $wpdb does not exist, because the function has not been accessed through WordPress, but through a direct command.

    If you want this function to work on the command line, you will need to rewrite it to work within that environment. Which means no WordPress-specific helpers or functionalities at all.

    Alternatively, you can use WordPress Crons, which are not true Cron jobs, but instead run whenever the site is accessed (within their defined time range). So if you have a cron that must run every five minutes, but you will not reliably have a new visitor every few minutes, these will not suit your needs, but they are an option.

    http://www.wpbeginner.com/plugins/how-to-view-and-control-wordpress-cron-jobs/

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器