Zabs
Zabs

Reputation: 14142

Cron jobs in codeigniter

I am trying to do a cron job with a site built in CodeIgniter - I've got access to the CPanel cron feature can anyone suggest the best way to setup a cron job using CPanel?

I am using CodIgniter so cannot be sure how to call a controller within a cron job?

E.g http://admin.com/sites/publish/

How would I access this publish function within the sites controllers using a cron job?

Upvotes: 6

Views: 13692

Answers (5)

Rizwan Ali Khan
Rizwan Ali Khan

Reputation: 59

just use this command and paste it.

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

Upvotes: 0

Muhammad Fahad
Muhammad Fahad

Reputation: 1412

For Cronjob try this to access command line controller, functions and params:

php index.php/controller/function/param1/param2/param3 etc

or

php index.php controller function param1 param2 param3 etc

Upvotes: 1

fire
fire

Reputation: 21531

Best way is to call from the command line in the cron job...

php /path/to/index.php controller >> /dev/null

You can run controllers via the command line in CI, see here.

Upvotes: 8

san4o
san4o

Reputation: 196

I do this such way, create folder cron

/application
/cron
   my_task.php
/public

make script for each cron job /cron/my_task.php with content

<?  $_SERVER["SCRIPT_URL"] = "/controllerName/MethodName"; // you can set url in routes if you want
    $_SERVER["HTTP_HOST"] = "your_site_address.com"; // without http://www
    require(dirname(__FILE__) . "/../public/index.php");  // path to index.php
 ?>  

make controller Cron like others, but add validation on IP in __construct

and finaly run like

1 10 * * * cd /path_to_site_folder/cron/ && usr/local/bin/php /path_to_site_folder/cron/my_task.php >> path_to_log/some.log

Upvotes: 2

juanrossi
juanrossi

Reputation: 305

For me the easier way of doing this is using cURL and executing the url in the cron:

curl http://admin.com/sites/publish/

If you need to secure the url, you could send data via post using:

curl -X POST -d "apikey=yourapikey&another=variable" http://admin.com/sites/publish/

This way you don't have to fight with php parameters and different configurations.

Upvotes: 5

Related Questions