简体   繁体   中英

Running a cron job with CodeIgniter

I need to run a cron job on a file using CodeIgniter but CodeIgniter won't let me run a file directly. ie

I can run the controller in the browser with:

api.example.com/index.php/cron

My hosting only lets me run PHP files and I can't do it via the command line because it won't let me run wget or curl.

How can I make CodeIgniter run a file like below:

api.example.com/index.php/application/controller/cron.php

I can call that file with my hosting.

Updated

Thought i would just update none of the options worked for me i ended up changing hosting and doing the following.

http://devsforrest.com/116/setup-a-cronjob-to-run-every-5-minute-on-media-temple

Not sure if this is what you're looking for, or if I'm answering you wrong, but the way I run my CRON jobs with CodeIgniter is simply doing this as the cron:

php /path/to/index.php controller function

Then in the controller I have this at the top of it

if( PHP_SAPI != 'cli') exit('My Custom Error Message');

Uset wget command along with your url to run cron in codeigniter file in your case it should be:

wget api.example.com/index.php/controller/function

and be careful do not use path of file use the url which invokes your function.

class Cron extends CI_Controller {
    function index($dummy) {

        // Crony command

    }
}

Line: api.example.com/index.php/cron/index/dummy.php

Basicly given example uses the 3rd uri segment to pass a value ending on .php that you don't use.

Or else you can use good ol' Routing

Use .htaccess to append .php to the requested URL, i did not test the code below, but i hope it helps.

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [R=301]

Use CURL and call your cronjob by URL. If CURL is enabled in your hosting that will be a good choice.

Create a PHP file name it cronjob.php in the root folder of your website and add the following script in a file.

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://www.mysite.com/index.php/controller/method");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
curl_close ($ch);
?>

Now in the cronjob call the php file cronjob.php and it should work.

simply

/usr/local/bin/php /home/cpanel_username/public_html/sub_folder/index.php param1 param2 optional_param3 optional_param3

Here

cpanel_username - username of your domain cpanel,

sub_folder(optional) - if your web application is any folder

Param(parameters) - refers to controller and their methods(functions)

Codeigniter API Cronjob for godaddy hosting

CodeIgniter cron job API based daily 10 o clock

0 10 * * * /usr/local/bin/php public_html/folder_name/index.php controller_name funcation_name

For Me Working Fine

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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