How to run a cron job on a CodeIgniter controller that all the URL does is run a query from an API, update the DB & send emails (from within CI)? How to run a cron job on a CodeIgniter controller that all the URL does is run a query from an API, update the DB & send emails (from within CI)? codeigniter codeigniter

How to run a cron job on a CodeIgniter controller that all the URL does is run a query from an API, update the DB & send emails (from within CI)?


curl -O - http://10.0.1.666/tools/Cli_only/cron_job >/dev/null 2>&1 is not cli.Curl makes normal http request to your webserver, whilst cli stands for php command line script.

To run it as cli:

$ cd /path/to/your/web/root$ php index.php tools Cli_only cron_job

For your conjob it must be:

14 16 * * *  cd /path/to/project; php tools Cli_only cron_job >/dev/null 2>&1

EDIT

It is quite common practice to run curl requests from cron job, which have it's pros. For example it allows to run a cron job as any user, and guarantee the job is executed by webserver user. Using cli, it is your responsibility to set the cron job for correct user, e.g. crontab -u www-data -e if your webserver user is www-data. Otherwise you are at risk to mess with permissions for temporary files like caches, logs etc.

If you are not sure about internals, it may be better to keep using curl in your cron job, but limit access to the controller for certain IP addresses. For example instead of

is_cli() OR show_404();

use something like

$this->input->ip_address() == '127.0.0.1' OR show_404();


First I don't understand why you running a crotab through a http request. You can absolutely run a crontab on your server which is 10.0.1.666,which can also proceed if there is something wrong with the internet. Besides you said 'This works fine when not having is_cli() OR show_404();' ,that because In the crontab you are writing send an http request. Suppose you are the client ,And the server is 10.0.1.666. EACH TIME as the crontab is running ,the 'client' send a http request. And the server 666 receives the request then excute the controller you wrote. It's not a cli script. If you insist doing this by sending http request. I have 2 adivices(not best but may working)

  1. Add some GET parameter which only you know like http://10.0.1.666/tools/Cli_only/cron_job?running=no_one_knows_this
  2. change your controller get the para running ,if the para isnot 'no_one_knows_this' , exit();

Second ,I strongly suggest you run the bash on the server .