PHP CRON Job within CodeIgniter framework - php

I am attempting to create a CRON job which points to a function inside a file in a Code Igniter system I have built.
I have created the CRON job on my CPanel and I have tested it using a simple mail php function inside a file at the root and this works correctly, however, I wish to point the CRON job to a location within my MVC framework and for some reason this does not seem to be working.
Here is the CRON job set up on my CPanel:
0 0 * * 1 wget -q -O /dev/null http://www.urlhere.co.uk/index.php/cron_event/send_reminders
and here is the controller I wish to run. The location of it is in the system/controllers/cron_event.php:
<?php
class Cron extends Controller {
function Cron_event()
{
parent::Controller();
}
/**
* The index method just displays an access denied message, as we don't support viewing this module in the browser.
*/
function index()
{
$this->send_reminders();
$this->load->view('themes/base/header', array('title'=>"Access Denied"));
$this->load->view('cron/access_denied');
$this->load->view('themes/base/footer');
}
/**
* Updates the PR Online Calendar by sending out email notifications for events that have not yet had them sent out.
*/
public function send_reminders() {
$to = 'jamesholman#urlgoeshere.co.uk';
$from = 'bigwavetest';
$message = 'test';
mail($to, $from, $message);
}
}
?>
When I point to this controller the CRON job stops working.
I have a feeling it is because I am not including and requiring the Code Igniter framework files but am unsure.
Does anybody have any ideas as to why this isn't working?
Thanks in advance!

In your cron call you put:
wget -q -O /dev/null http://www.urlgoeshere.co.uk/index.php cron_event
I think it must be:
wget -q -O /dev/null http://www.urlgoeshere.co.uk/index.php/cron_event
Or:
wget -q -O /dev/null http://www.urlgoeshere.co.uk/cron_event
If you removed index.php from your URL.

Related

Cake PHP run multiple Console/cake shell commands in different processes

I've written a Console/cake shell for my Cake PHP 2 project. My shell is called a Queue Manager, and it contains two tasks, a ProcessTask.php and a ScheduleRunTask.php, it's my ProcessTask that once ran will grab entries from a database table, loop over them and do something. I cannot use Supervisor for the project I'm working on as this is a strict prerequisite.
The issue I face is that I need it to run automatically, and ideally need it to automatically scale up and down based on a settings table setting called something like "max workers" so that it's not always running 10 commands at once when it doesn't need to.
I've put together a crontab which runs my scheduler once a minute:
* * * * * cd /path-to-project-app && Console/cake queue_manager schedule_run >> /dev/null 2>&1
When this runs the schedule_run task, this in turn fires up the process task and runs these:
<?php
class ScheduleRunTask extends AppShell {
/**
* Main execute task
*/
public function execute() {
$maxWorkers = 5;
$currentWorker = 0
while ($currentWorker < $maxWorkers) {
exec("cd /path-ro-my-project-app && Console/cake queue_manager process", $output, $retval);
$this->out("<info>Worker is running.</info>");
}
}
}
But is this the best way to do this and do these run in their own processes? Which is important.
How do I prevent it from starting up another 5 exec commands on the next minute the cron runs, I'd end up with hundreds right?

Set cron job in Codeigniter

I am trying to run a script via cron job in cpanel. Maybe I entered a wrong path that's why I received a mail:
could not open input file.
Here is my code
class Cron extends CI_Controller
{
public function run()
{
$this->load->library('email');
$this->email->to('to#mail.com');
$this->email->from('from#mail.com','From');
$this->email->subject('Cron');
$this->email->message('Hello);
$this->email->send();
}
}
This code available in
public_html/folder/myproject/application/controller/Cron.php
But I dont know how to set this path in cron url
Please try the following in cpanel command input
wget -q -O - http://www.yourdomain.com/cron/run >/dev/null 2>&1
Check the Screenshot
If you want to do the same thing by file then please use proper path
/home/youruserdirectory/public_html/folder/myproject/application/controller/Cron.php
replace youruserdirectory to your current user directory.
Hope it will helpful.
Judging by your code the Cron controller has to be called from the web, not from command line, but that's fine, you don't necessarily have to provide cron job with the path on the server. You can run a command to make a request to your site like this:
*/5 * * * * /usr/bin/wget -qO- https://example.com/cron
The command in the example will make a request to your site every 5 minutes effectively running the Cron controller every time (in case you have not prevented access to it by its name with routing).

How to run console command of Yii2 using cron job in Godaddy web hosting server

From localhost am using this command to run and its working succesfully
c:/wamp/www/yii2_advanced>yii test/pending
While am using below command in cron job of godaddy server. its not working
php public_html/yii2_advanced/ yii test/pending >/dev/null 2>&1
So how can I run console command of Yii2 using cron job in Godaddy web hosting server?
Thanks in Advance...
I know this is an old post but I have also waste my whole day to figure out this problem.that is why I am posting my answer.Hope this will help some one.
1> Enter an email id in above section. It will help you to test.
2> Do not choose time less then 5 minutes.Sometimes it is not working on godaddy.
Note: email could take up to 20 minutes to trigger, so be patient.
3>Enter This command after selecting time 5 minutes to test.
/usr/local/bin/php -q /home/username/public_html/projectName/yii json/start
4>Check your email in order to test your job .If job is not working then here will be an error message.
5> When your cron job is up and running then you do not need any email . To remove email sending add this command
/usr/local/bin/php -q /home/username/public_html/projectName/yii json/start >/dev/null 2>&1
You can use below function in crontab
* * * * * php /var/www/html/your-project-path/yii test/index
Here yii points to your root yii file of your Yii project test is my console controller and /index is my method.
Hope this helps .
Non of these answers work for me. Im using yii2 Hope my solution help someone!
First of all Create your Cron as a new file: MyCronNameController.php
We will suppose our cron file name is HelloController.php so it codes will look like:
<?php
namespace app\commands;
use yii\console\Controller;
class HelloController extends Controller
{
public function actionIndex($message = 'hello world')
{
echo $message . "\n";
}
}
One this file is on your hosting, just go to the Cron Job manager into GoDaddy and set the command like this:
/usr/local/bin/php -q /home/<user>/public_html/<project-folder>/yii hello
Take a look that on the command i just run the command without the "Controller" word.
One more observation is that i try this with a cron name like "HelloWordController" and run it with "helloword", and it didnt work, with only one word name it will work ok.
cronjobs in yii2 ubuntu
#create cronjob every minutes
php /var/www/html/yiitest2.0.39/yii test/makefile
https://youtu.be/5KUMNPKgvnU
/usr/local/bin/php -q /public_html/<YOUR_PROJECT_FOLDER_NAME> yii <CONTROLLER_NAME>/<ACTION_NAME>

How to run a cron job in codeigniter without using wget?

I want to run a cron job without using wget in CodeIgniter.
I am using it like this:
*/1 * * * * wget http://assurance.com/controller/function
It works successfully, but I do not want to use wget.
Is there any another way to run this CodeIgniter script?
You can try and use something like this:
* * * * * /usr/bin/php /pathToTheApp/controller/function
But of course the /usr/bin/php should be your path to the PHP binaries and pathToTheApp should be the absolute path to your CI application.
If you have local shell access on the host, add it to your crontab there.
I needed to do this exact thing recently and couldn't find a complete solution. So I'll try to provide one here.
I used "bash shell" because I wanted to make a command line (CLI) call to my controller into of an http (note: I'm using an ubuntu/linux server.
There are three main parts:
The cron call
The shell script
The controller function
Cron:
in your server cli type this to access crontab: crontab -e
then add your cron call: * * * * * bash /path/to/script/test.sh
(note: I created a folder in my site root called cron, so my path would be:
/var/www/website/cron/test.sh
Shell script:
In that cron folder we made, create a file called " test.sh "
In the file put:
#!/bin/bash
cd /path/to/site
/usr/bin/php index.php controller function
That's it
cron sets up the timer to call the file
shell calls the controller from the CLI
you'll now be able to use $this->input->is_cli_request() in your function for added security.
public function cronTest()
{
if($this->input->is_cli_request())
{
//code goes here;
}
}
Hope this helps save you some time, this took me way longer than expected :)

Running CodeIgniter cron on localhost

I'm trying to get a cron job to run every 5 min on my localhost. Using the Cronnix app I entered the following command
0,5 * * * * root curl http://localhost:8888/site/ > /dev/null
The script runs fine when I visit http://localhost:8888/site/ in my browser. I've read some stuff about getting CI to run on Cron, using wget and various other options but none make a lot of sense.
In another SO post I found the following command
wget -O - -q -t 1 http://www.example.com/cron/run
What is the "-O - -q -t 1" syntax exactly?
Are there other options?
-O - Means the output goes to stdout (-O /dev/null) would nullify any output. -q means be quiet (don't print out any progress bars), this would screw up the look of any log files. -t 1 means to only try once. If the connection fails or times out it will not try again.
See http://linux.die.net/man/1/wget for a full manual on the wget command.
Edit: just realised you're piping all this to /dev/null anyway, you may as well either omit the -O parameter or point that to /dev/null and omit the final pipe.
What I always do is use PHP in cli mode. Seems more efficient to me.
first setup a cron entry like :
*/5 * * * * /usr/bin/php /var/www/html/cronnedscript.php
cronnedscript.php should be placed in your root www folder.
then edit cronnedscript.php with:
<?php
$_GET["/mycontroller/index"] = null;
require "index.php";
?>
where mycrontroller is the CI controller you want to fire.
if you want the controller to only be run by crond ,as opposed through public www requests, add the following line to the controller and to the cronnedscript.php :
if (isset($_SERVER['REMOTE_ADDR'])) die('Permission denied');
I realize that this is a reference to Drupal, however they do a very nice job of explaining what each and every parameter is in the wget call.
Drupal Cron Explanation
If you want the more generic explanation, you can find it here.
Try this and save it by making a folder in the C drive with a .bat extension.
Then give the path of this script to task scheduler.
Then run the same.
C:\xampp\php\php-win.exe -fC:\xampp\htdocs\folder name\index.php controllername functionname

Categories