Cron jobs in codeigniter
Asked Answered
S

5

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?

Standfast answered 6/9, 2011 at 15:30 Comment(0)
T
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.

Transmundane answered 6/9, 2011 at 15:38 Comment(11)
Would this be correct for my purpose do you think? php /home/username/public_html/index.php/sites/publish >> /dev/nullStandfast
+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)Sidelong
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 directoryStandfast
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 :(Standfast
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.Transmundane
@Transmundane 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: |---------------------------------------------------------------Standfast
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"Transmundane
ok - so i presume this would be right :- /usr/bin php /home/username/public_html/index.php "sites publish"Standfast
Only you can tell me that! If it works then the answer is yes ;-)Transmundane
let us continue this discussion in chatTransmundane
just updating the URL provided in this already wonderful answer :P ellislab.com/codeigniter/user-guide/general/cli.htmlRosebay
D
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.

Discus answered 6/9, 2011 at 22:25 Comment(2)
using this, you expose your cron jobs to the whole worldProcarp
You could protect them using a htpasswd. I know it's safer using php instead of curl, but for some silly stuff it's easier this way.Discus
S
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
Sora answered 11/3, 2013 at 8:28 Comment(0)
S
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
Shae answered 1/2, 2016 at 10:21 Comment(0)
F
0

just use this command and paste it.

wget www.example.com/index.php/controller/function
Favata answered 2/11, 2018 at 16:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.