Run a cron job on a controller method |
I'm trying to run a cron job on a controller method, where the installation or application folder is above the root. How can I do this? I've not had any success so far??
This is what I've been trying Code: /usr/bin/php -q /home/DOMAIN/application/controllers/check.php reccuring_cron I've tried all sorts of variations including the below Code: /usr/bin/php -q /home/DOMAIN/public_html/index.php check reccuring_cron Where am i going wrong? I know the first part is correct Code: /usr/bin/php -q and I know path is correct?
05-25-2016, 06:18 AM
(This post was last modified: 05-25-2016, 06:31 AM by abhijeetgk. Edit Reason: more description added )
may be you can have a look at
https://codeigniter.com/user_guide/general/cli.html Another hack is Create a "cli.php" file at the root of your CodeIgniter folder: if (isset($_SERVER['REMOTE_ADDR'])) { die('Command Line Only!'); } set_time_limit(0); $_SERVER['PATH_INFO'] = $_SERVER['REQUEST_URI'] = $argv[1]; require dirname(__FILE__) . '/index.php'; If you are on a Linux environment and want to make this script self executable, you can add this as the first line in cli.php: #!/usr/bin/php Run as on command line php cli.php "controller/method/param" ![]()
My application folder is above root though? As recommended by the security pages of CI. All documentation includes a reference to index.php, and this is irrelevant to me and means it doesnt work???
05-25-2016, 06:59 AM
(This post was last modified: 05-25-2016, 07:29 AM by frobak. Edit Reason: amended )
Ok, so I managed to get the controller/method to run via terminal using your above help, so thank you.
It worked via: Code: php cli.php cron/reccuring_checks But when I try and get the script to run via a cron job, set up via cli Code: * * * * * php cli.php cron/reccuring_checks i get errors: Code: <h4>A PHP Error was encountered</h4> (05-25-2016, 06:26 AM)frobak Wrote: My application folder is above root though? As recommended by the security pages of CI. All documentation includes a reference to index.php, and this is irrelevant to me and means it doesnt work??? It's not irrelevant, you're supposed to run it through index.php and the whole thing is designed to mimic regular URIs, like this: Code: /path/to/php index.php /controller/method/param1/param2/etc That "cli.php hack" is redundant ... (05-25-2016, 06:59 AM)frobak Wrote: Ok, so I managed to get the controller/method to run via terminal using your above help, so thank you. You're not supposed to use sessions under CLI. And CodeIgniter actually has provisions to ignore them in that case, but only when it can detect that you are running it from the command line. You're probably using the wrong php binary, which is why CI can't properly detect it ...
I have also tried numerous variations of the CLI command line, but to no avail. I've given up and wrote my cronjobs in old style php, which really feels as a step back.
Edited: Tried again, with the tips in this discussion. Controller: Cronjobs.php Method: test() All that the test() method should do is echo "This is a test". Result (in the mail I get when the cronjob has been executed): the complete html-code (including css) of my welcome page, and at the end of the output I find this error: Code: <h1>A Database Error Occurred</h1> It never occurred to me that sessions had something to do with this faulty behaviour, but CI doesn't seem te recognize that I'm running a cronjob and loads the sessions driver. How can I avoid that? And why is it loading my complete homepage, not the cronjobs/test controller/method? My cronjob command is: /bin/php ./mysite/index.php /cronjobs/test
What you should probably doing is not using the 'sessions' by default, instead you should have your 'web' controllers extending a MY_Controller which has the session/authentication code, and then the CLI controllers just extend the vanilla CI_Controller.
I use a lot of controllers run as CLI scripts, the syntax in my crontab looks like e.g: /usr/bin/php /full/path/to/index.php controller method The above all works for me in 2.X. I'm migrating my code to 3.X at the moment but haven't got to the CLIs so apologies if the above is not entirely true for 3.x. (05-25-2016, 10:38 AM)Wouter60 Wrote: I have also tried numerous variations of the CLI command line, but to no avail. I've given up and wrote my cronjobs in old style php, which really feels as a step back.I'm using php-cli instead of php and using php v5.5(but not remember mode im currently use)? Try using common function is_cli() limit the access to cli req only, link https://www.codeigniter.com/userguide3/g...tml#is_cli
Keep calm.
|
Welcome Guest, Not a member yet? Register Sign In |