6

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?

tereško
  • 58,060
  • 25
  • 98
  • 150
Zabs
  • 13,852
  • 45
  • 173
  • 297

5 Answers5

8

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.

fire
  • 21,383
  • 17
  • 79
  • 114
  • Would this be correct for my purpose do you think? php /home/username/public_html/index.php/sites/publish >> /dev/null – Zabs Sep 06 '11 at 15:52
  • 2
    +1 from me but I'll just add based on the question and the path given you'd use `php /path/to/index.php sites publish >> /dev/null` (realise you've just given an example just thought I'd clarify) – simnom Sep 06 '11 at 15:54
  • Hi again - just tried and the cron is running but I get the following Cron Daemon message sent to me. NB The controller is called sites & the function is called cron /bin/sh: /home/username/public_html/index.php/sites/publish: Not a directory – Zabs Sep 06 '11 at 15:59
  • Does the command line stuff work in the legacy stuff? The site I have is very old using around version 1.6 of Codeigniter I believe :( – Zabs Sep 06 '11 at 16:07
  • 2
    No your command is wrong it should be `/home/username/public_html/index.php sites publish` after the index.php you send the controller as the parameters. – fire Sep 06 '11 at 16:11
  • @fire kinda gets there but I now get this - i may try to try a non codeigniter file (with sum good ol' mysql) /home/username/public_html/index.php: line 1: ?php : No such file or directory /home/username/public_html/index.php: line 2: /* : No such file or directory /home/username/public_html/index.php: line 3: syntax error near unexpected token `|' /home/username/public_html/index.php: line 3: `|--------------------------------------------------------------- – Zabs Sep 06 '11 at 16:21
  • 1
    I didn't see the path to PHP in your command, also you can try putting the parameters in quotes e.g..... `php /home/username/public_html/index.php "sites publish"` – fire Sep 07 '11 at 08:36
  • ok - so i presume this would be right :- /usr/bin php /home/username/public_html/index.php "sites publish" – Zabs Sep 07 '11 at 11:07
  • Only you can tell me that! If it works then the answer is yes ;-) – fire Sep 07 '11 at 11:53
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/3240/discussion-between-fire-and-gaz) – fire Sep 07 '11 at 11:53
  • just updating the URL provided in this already wonderful answer :P http://ellislab.com/codeigniter/user-guide/general/cli.html – Jon B Mar 17 '14 at 23:10
5

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.

juanrossi
  • 195
  • 2
  • 8
2

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
san4o
  • 196
  • 1
  • 7
1

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
Muhammad Fahad
  • 1,352
  • 15
  • 15
0

just use this command and paste it.

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