Accessing Controller with CRON |
Hi All,
My Ci4 project is in a hosted environment where my Ci4 application is physically located in /home/my-app/ and served from my domain /home/my-app/mydomain.co.uk I am trying to run a CRON job to access a controller physically located /home/my-app/app/Controllers/Crons/mycronjob.php If I configure CRON to execute the file directly i.e. /home/my-app/app/Controllers/Crons/mycronjob.php I get Status: 500 Internal Server Error if set up a route PHP Code: $routes->add('runcron', 'Crons\mycronjob'); I saw a previous post so tried /home/my-app/mydomain.co.uk/index.php Crons/mycronjob index and I get Status: 404 Not Found, with Cache-control: no-store, max-age=0, no-cache I can execute the script from the domain mydomain.co.uk/runcron without errors and the script executes perfectly. Now I am lost. Any Ideas, please?
Thanks for pointing me in the right direction. But the reference in the Codigniter manual doesn't seen to work.
I used the example line for line as in the manual (copy/paste the Tools controller and placed in my app/Controllers root) I then modified my cron command /usr/bin/php /home/my-app/mydomain.co.uk/index.php tools message to >> /home/my-app/app/Controllers/logfile.php output = Status: 404 Not Found Then I amended the Controller name in the command to match the controller name in the filesystem (Tools - capitalised) /usr/bin/php /home/my-app/mydomain.co.uk/index.php tools message to >> /home/my-app/app/Controllers/logfile.php output = Status: 404 Not Found I then add the cli route as described: PHP Code: $routes->cli('tools/message/(:segment)', 'Tools::message/$1'); output = Status: 404 Not Found and, as per the documentation "Everything else works exactly like a normal route definition:" I set the route to PHP Code: $routes->cli('dosomething', 'Tools::message'); output = Status: 404 Not Found Any thoughts?
Check your letter case of filename, folder name and classname.
Tools.php class Tools
(11-18-2021, 04:06 PM)kenjis Wrote: Check your letter case of filename, folder name and classname. Thank you all so much for you help. To ensure I have my paths, folder and file names correct, even the correct case, I have run the followingb tests. I created a very simple php file: test.php PHP Code: <?php I updated my Cron command to /usr/bin/php /home/my-app/app/Controllers/test.php >> /home/my-app/app/Controllers/logfile.php As expected this worked well, the cron job run correctly output =I am at /home/my-app/app/Controllers/test.php Next I amended test.php using the structure of a file that works in the application. PHP Code: <?php namespace App\Controllers; I left my Cron command unchanged /usr/bin/php /home/my-app/app/Controllers/test.php >> /home/my-app/app/Controllers/logfile.php As expected this worked well, the cron job run correctly output = 500 Internal Server Error The Cron found the file, but as expected the file did not execute correctly Then I changed my Cron command to (as described in the Ci4 user manual /usr/bin/php /home/my-app/mydonain.co.uk/index.php test >> /home/my-app/app/Controllers/logfile.php This did NOT work well, the cron job run could not find the file and output = Status: 404 Not Found I also tried the following commands /usr/bin/php /home/my-app/mydonain.co.uk/index.php Controllers test >> /home/my-app/app/Controllers/logfile.php /usr/bin/php /home/my-app/mydonain.co.uk/index.php app Controllers test >> /home/my-app/app/Controllers/logfile.php /usr/bin/php /home/my-app/mydonain.co.uk/index.php my-app app Controllers test >> /home/my-app/app/Controllers/logfile.php /usr/bin/php /home/my-app/mydonain.co.uk/index.php home my-app app Controllers test >> /home/my-app/app/Controllers/logfile.php /usr/bin/php /home/my-app/mydonain.co.uk/index.php /home my-app app Controllers test >> /home/my-app/app/Controllers/logfile.php All failed with output = Status: 404 Not Found So as per the manual, I set up a route (in common with all the other routes in my application) PHP Code: $routes->add('testing', 'test::index'); This time my Cron command read /usr/bin/php /home/my-app/mydonain.co.uk/index.php testing index >> /home/my-app/app/Controllers/logfile.php Again this failed with output = Status: 404 Not Found It would appear that the Cron is expecting the target file to be in the /home/my-app/mydonain.co.uk/ folder ??? Hmmmmmmm..... To use a sporting parlance, I feel "snookered".
It should be /home/my-app/app/Controllers/Test.php.
And its class name should be Test. And run: /usr/bin/php /home/my-app/mydonain.co.uk/index.php test >> /home/my-app/app/Controllers/logfile.php
(11-21-2021, 05:41 AM)kenjis Wrote: It should be /home/my-app/app/Controllers/Test.php.My Cron command /usr/bin/php /home/my-app/mydomain.co.uk/index.php test >> /home/my-app/app/Controllers/logfile.php Output = Status: 404 Not Found I believe the issue is the Ci4 app/Controllers folder sits outside of the folder where the domain index.php file sits app/Controllers sit in /home/my-app/app/Controllers/ domain index.php sits in /home/my-app/mydomain.co.uk/ I really appreciate you time.
Thank you everyone for your input and help, but I found the issue was nothing to do with Codeigniter at all. It is all to do with the setup on the hosting servoice and access the correct php.
Codeigniter requires php CLI and NOT php CGI !!! When testing, ensure Ci4 is set to development (.env) for more meaningful error reporting. In my instance I needed to contact the hosting support team to establish the correct php path. I was using /usr/bin/php home/my-app/mydomain.co.uk/index.php test >> /home/my-app/app/Controllers/logfile.php (where test is the named route to my controller) this was causing the cron to fail because /usr/bin/php was using the php CGI engine. My CLI engine is located at /usr/local/bin/php so by running /usr/local/bin/php home/my-app/mydomain.co.uk/index.php test >> /home/my-app/app/Controllers/logfile.php Cron was able to find the desired controller and run my routine. I hope this helps. |
Welcome Guest, Not a member yet? Register Sign In |