CodeIgniter Cron Job on Shared Hosting?

前端 未结 4 716
暖寄归人
暖寄归人 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
    
    

    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.

提交回复
热议问题