How to set cron job URL for CodeIgniter?
I have used below cron
php /full-path-to-cron-file/cron.php /test/index
source: http://www.asim.pk/2009/05/14/creating-and-installing-crontabs-using-codeigniter/
This works for me.
Thanks to all
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();
How to set cron job url in codeigniter on HostGator cpanel
if you want to run it in each 5 minute on daily basis then add it like -
* /5 * * * wget www.luvkushfinserve.com/Blog/auto_cron
How to setup cron job for a codeigniter script on server
Use /usr/local/bin/php
instead of php
to get codeigniter to pick up on the URI segments.
so try this if it works
/usr/local/bin/php /home/name/public_html/index.php stockupdate index
Running a cron job with CodeIgniter
Uset wget command along with your url to run cron in codeigniter file in your case it should be:
wget api.example.com/index.php/controller/function
and be careful do not use path of file use the url which invokes your function.
how to set cron job if using codeigniter in cpanel?
STEPS
- Create a command-line route in
app/Config/Routes.php
. I.e:
$routes->setDefaultNamespace('App\Controllers');
$routes->setAutoRoute(false);
// ...
$routes->cli("cli/ship-product/(:segment)", "Services\Buy::ship/$1");
// ...
Where:
cli/ship-product/(:segment)
- Represents your route. The (:segment)
is optional depending on if your Controller method requires an argument or not.
Buy
- Represents your Controller.
ship
- Represents your Controller method.
$1
- Represents the optional (:segment)
to be forwarded to the first Controller method's argument if in case it requires one. You may omit it if your Controller method doesn't require any arguments.
Notice the use of CLI-only routing with the help of ->cli(...)
.
- Run your cron job.
/usr/local/bin/php -q /home/kuslon/kuslon/public/index.php cli ship-product "ed053cb1-29a4-42f2-a17e-3109fa4d80fe"
Where:
-q
- Represents quiet-mode. Suppress HTTP header output.
/home/kuslon/kuslon/public/index.php
- Represents the absolute path to your project's index.php
file. This assumes that the first kuslon
in the path represents your Cpanel username and the second kuslon
represents your project root folder.
cli
- Represents the first section of the command-line route you defined earlier.
ship-product
- Represents the second section of the command-line route.
"ed053cb1-29a4-42f2-a17e-3109fa4d80fe"
- Represents the third optional section of the command-line route that may be required by your Controller method.
- Sample Controller.
<?php
namespace App\Controllers\Services;
use App\Controllers\BaseController;
class Buy extends BaseController
{
public function ship(string $uuid)
{
// echo "{$uuid}";
}
}
Resource: Running via the Command Line
Addendum:
If in case all your routes pass through an Authentication filter, you may want to exclude these command-line based routes in app/Config/Filters.php
. I.e: As you may notice below, I've excluded all routes starting with cli/*
from the authfilter
.
<?php
namespace Config;
use App\Filters\AuthenticationFilter;
use CodeIgniter\Config\BaseConfig;
class Filters extends BaseConfig
{
// ...
public $aliases = [
//...
'authfilter' => AuthenticationFilter::class
];
public $globals = [
'before' => [
//...
'authfilter' => [
'except' => ['cli/*']
]
],
// ...
];
}
Codeigniter cron job not working
the steps to prepare CodeIgniter 2.x for cron-jobs via CLI (command line interface):
1st: create a copy of your root index.php
file and save it in your root as cli.php
2nd: in your cli.php replace <?php
with this code:
#!/usr/local/bin/php
<?php
/* override normal limitations */
set_time_limit(0);
ini_set('memory_limit', '256M');
/* deny direct call from web browser */
if (isset($_SERVER['REMOTE_ADDR'])) die('Permission denied.');
/* constants */
define('CMD', 1);
/* manually set the URI path based on command line arguments... */
unset($argv[0]); /* except the first one */
$_SERVER['QUERY_STRING'] = $_SERVER['PATH_INFO'] = $_SERVER['REQUEST_URI'] = '/' . implode('/', $argv) . '/';
3rd: execute your cron job like this:
/usr/bin/php /var/www/website/public_html/cli.php controller method
where /var/www/website/public_html/
is your server's home directory, the location of your index.php
and cli.php
.
notes:
for CI 3.0 you find the necessary information here
database: you'll need to provide your db config settings in your controller method, as the cron job just executes the controller's method. So it doesn't know anything about any database settings!
$config['hostname'] = "localhost";
$config['username'] = "username_admin";
$config['password'] = "password";
//etc..
$this->db = $this->load->database($config, TRUE);
debug: just add a link in your html to run your controller's method like: index.php/controller/method
(remove that once you website is live)
source: very helpful
Related Topics
Dynamically Changing Image Colours
Set Active State on Navigation Dynamically
Replace Only First Match Using Preg_Replace
How to Bind SQL Variables in PHP
How to Include External CSS and Js File in Laravel 5
PHP Cli Getting Input from User and Then Dumping into Variable Possible
Curl Error 60: Ssl Certificate in Laravel 5.4
Create New Xml File and Write Data to It
Send Xml Data to Webservice Using PHP Curl
PHP Remove All Characters Before Specific String
Make a Path Work Both on Linux and Windows
Using Mx Records to Validate Email Addresses