简体   繁体   English

无法在Cpanel上设置Cron作业

[英]Cannot setup a cron job on Cpanel

Trying to add a cron job on Cpanel to run every 5 minutes. 尝试在Cpanel上添加cron作业以每5分钟运行一次。 I added this to the command input (removed personal details from the url) 我将此添加到命令输入中(从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: 然后在functions.php ,我有这个:

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. 如果我通过管理页面上的按钮触发了update_products()函数,则该函数将手动运行而不会出现任何错误。 But, no matter what I do on the cronjob tab, it doesn't run. 但是,无论我在cronjob选项卡上做什么,它都不会运行。

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. 编辑:基于答案/评论,我进行了额外的研究,发现我可以在任何脚本上加载$ wpdb。 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

I'm afraid there's some misunderstanding about how inter-operable the web environment and console environment are here. 恐怕人们对Web环境和控制台环境之间的可互操作性存在一些误解。

In short, your function expects WordPress to exist. 简而言之,您的函数希望WordPress存在。 It expects that the user has accessed the function through the WordPress application, and that global variables such as $wpdb exist (among other things). 它期望用户已经通过WordPress应用程序访问了该函数,并且存在诸如$wpdb类的全局变量(还有其他东西)。 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. 在该请求的上下文中存在WordPress。 global $wpdb does not exist, because the function has not been accessed through WordPress, but through a direct command. global $wpdb不存在,因为该功能尚未通过WordPress访问,而是通过直接命令访问。

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. 这意味着根本没有特定于WordPress的帮助程序或功能。

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). 或者,您可以使用WordPress Crons ,这不是真正的Cron作业,而是在访问网站时(在其定义的时间范围内)运行。 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. 因此,如果您有一个必须每五分钟运行一次的cron,但是您不会每隔几分钟可靠地拥有一个新访客,则这些将不满足您的需求,但是可以选择。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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