CodeIgniter Cron Job on Shared Hosting?

耗尽温柔 提交于 2019-12-03 13:54:40

问题


I am trying to learn how to do my first cron job using CodeIgniter. In the past, it seemed the only way to do this with CI was to use the wget command instead of php.

The CodeIgniter User Guide, however, says that now you can do this from the command line, for example by running:

$ cd /path/to/project;
$ php index.php controller method

This works great using Terminal on my local setup. But when I use a similar command in the cron section of cPanel on my shared hosting, the task just returns the contents of index.php.

I'm not entirely sure what cPanel does with this command, so unsure as to whether it's using the command line at all.

Could someone explain how I might be able to set up a cron job on shared hosting using CodeIgniter please?

Here is the example code from the CodeIgniter user guide:

tools.php

public function message($to = 'World')
{
    echo "Hello {$to}!".PHP_EOL;
}

} ?>


回答1:


It's going to depend on your host. Cron jobs could really screw stuff up if you're not careful, so a lot of shared hosts don't allow it. You probably need to be on some virtual container (like a VPS, virtuozo, etc.) to do this. This isn't a CodeIgniter issue, but a hosting provider issue. Call them first.




回答2:


We worked around this exact issue as follows:

  1. Set up a normal php file that is scheduled by cron. Nothing to do with codeigniter yet
  2. Inside it, you can make an fsocket or curl request to perform your regular CodeIgniter call as you do from the web.

Here's an example (say, cron.php)

#!/usr/local/bin/php.cli
<?php
DEFINE('CRON_CALL_URL','https://my_server/'); // 

DEFINE('CRON_HTTPS_PORT', 443); // port to use during fsocket connetion
DEFINE('CRON_SSL_PREFIX', 'ssl://'); // prefix to be used on the url when using ssl
$current_time = now();
$md5_hash = md5('somevalue'.$current_time);
        $url = CRON_CALL_URL.'MYCTRL/MYMETHOD'; 
        $parts=parse_url($url);
        // 
        $parts['query']='md5_hash='.$md5_hash.'&time='.$current_time;    
        $fp = fsockopen(CRON_SSL_PREFIX.$parts['host'],
            isset($parts['port'])?$parts['port']:CRON_HTTPS_PORT,
            $errno, $errstr, 30);

        if (!$fp) {
        } else {

            if (!array_key_exists('query', $parts)) $parts['query'] = null;
            $out = "POST ".$parts['path']." HTTP/1.1\r\n";
            $out.= "Host: ".$parts['host']."\r\n";
            $out.= "Content-Type: application/x-www-form-urlencoded\r\n";
            $out.= "Content-Length: ".strlen($parts['query'])."\r\n";
            $out.= "Connection: Close\r\n\r\n";
            if (isset($parts['query'])) $out.= $parts['query'];

            fwrite($fp, $out);
            fclose($fp);
        }
}
?>

NOTE: Make sure that in your MYCTRL/MYMETHOD function you have

ignore_user_abort(true);

that way when you fsocket connection is closed, your script will still run to the end.

We actually have a bunch of these fsockets for various reasons. If you need to make sure that the call to that controller/method came from the cron script, you need to pass some additional hash values so that only cron and the script know it. Once the script is called it has access to any codeigniter functions. Works like a charm.




回答3:


I've set up 100s of CI cronjob on shared hosting like this: create a short php script which calls the CI controller as if it was a webbrowser.

So, script.php contains this:

      script #! /usr/local/bin/php -f /home/example/public_html/script.php 

  <?php
     get_get_contents('http:example.com/cronjob/');     
  ?>

Then set your cronjob in cPanel to call script.php When it runs Script.php will call the Codeigniter Cronjob controller. There you have the entire CI framework at your disposal.




回答4:


If you are going to call it like a web browser, why not replace the cronjob command with:

wget http://example.com/cronjob/

instead of creating something new or simply

curl --suppress http://example.com/cronjob/`


来源:https://stackoverflow.com/questions/8332278/codeigniter-cron-job-on-shared-hosting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!