Welcome Guest, Not a member yet? Register   Sign In
Accessing Controller with CRON
#1

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'); 
and configure the CRON: /home/my-app/mydomain.co.uk/runcron I get Status:  404 Not Found
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?
Reply
#2

(This post was last modified: 11-17-2021, 02:21 PM by Secux.)

https://codeigniter4.github.io/userguide...ly-routing
Reply
#3

See https://codeigniter4.github.io/userguide...ello-world
Reply
#4

(This post was last modified: 11-18-2021, 12:51 PM by 68thorby68.)

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'); 
and used in conjubction with the command above.

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'); 
and my command to /usr/bin/php /home/my-app/mydomain.co.uk/index.php dosomething >> /home/my-app/app/Controllers/logfile.php
output = Status: 404 Not Found
Any thoughts?
Reply
#5

(This post was last modified: 11-18-2021, 04:06 PM by kenjis.)

Check your letter case of filename, folder name and classname.
Tools.php
class Tools
Reply
#6

(11-18-2021, 04:06 PM)kenjis Wrote: Check your letter case of filename, folder name and classname.
Tools.php
class Tools

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

echo "I am at /home/my-app/app/Controllers/test.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;

use 
CodeIgniter\Controller;
use 
CodeIgniter\Database\Query;
use 
App\Controllers\BaseController;

class 
test extends BaseController {
 
     public function index() {
     echo "I am at /home/my-app/app/Controllers/test.php";
     die();
     }


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".
Reply
#7

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
Reply
#8

(11-21-2021, 05:41 AM)kenjis Wrote: 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


Thank you Kenjis as recommend it tried:

my controller
PHP Code:
<?php namespace App\Controllers;

use 
CodeIgniter\Controller;
use 
CodeIgniter\Database\Query;
use 
App\Controllers\BaseController;

class 
Test extends BaseController {
 
     public function index() {
         echo "I am at jobtracker/app/Controller/Quotes";
         die();
     }
 

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.
Reply
#9

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.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB