CodeIgniter Cron Job on Shared Hosting?

前端 未结 4 715
暖寄归人
暖寄归人 2021-02-06 16:56

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

相关标签:
4条回答
  • 2021-02-06 17:14

    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.

    0 讨论(0)
  • 2021-02-06 17:18

    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.

    0 讨论(0)
  • 2021-02-06 17:19

    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.

    0 讨论(0)
  • 2021-02-06 17:28

    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/`
    
    0 讨论(0)
提交回复
热议问题