Set cron job in Codeigniter - php

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

Related

sending data with attachment doesn't work Cronjob

I dont know alot about cron job but i have a php file which sending data with attachment when i run it using the browser it's working fine and attachment is sent ,but if i use cron job to run it ,it never goes with the attachment.
here is the cron job command-line :
/opt/php54/bin/php /home/username/public_html/path/to/myscript.php
and here is the place of error in the php file ,as the cron job sending me email with "Some how file :hhshd.jpg is not exist!" , however the data with attachment is sent successfully when i use the browser.
if (!file_exists("uploads/" . $fileName))
{
die("Some how file :$fileName is not exist!");
}
Note: i am using the cronjob of Hostgator.
Problem solved i have used this cron job command:
curl http://yoursite/path/to/publish.php
One possible reason cold be when you run the script through browser, the Apache user is able to create the attachment file and send it.
But when cronjob is not able to create attachment file due to permission issue.
You can try using 'sudo' in your cronjob or try changing the permission of the directory where you are creating the attachment.
It is most likely to be an ownership & permissions problem. What you have to put in your cron config depends on where you've put it and what user your web server runs as. If you want it to run at 5am each day, it's in /etc/crontab and www-data is your web server user, you would need an entry like this:
0 5 * * * www-data /opt/php54/bin/php /home/username/public_html/path/to/myscript.php
If it's in the web server user's own crontab file, it would skip the user name field. You would edit it using crontab -u www-data -e, and add a line like this:
0 5 * * * /opt/php54/bin/php /home/username/public_html/path/to/myscript.php
One other issue may be that the current working directory may be different, so you may need to set the command to include a cd to the directory your script is in first, like this:
cd /home/username/public_html/path/to && /opt/php54/bin/php myscript.php
If you want to receive any error output from a cron job, set a MAILTO environment variable by adding a line like this to the cron file before you run the command:
MAILTO=user#example.com
Example:
* * * * * curl http://localhost/Android/SMS/p1.php
type above line to execute every second p1.php email sending file with attachment.
p1.php file is in my htdocs/Android/SMS folder.

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>

PHP CRON Job within CodeIgniter framework

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.

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 :)

Cron job is not giving required result, but accessing same file through browser does

I need to run a php script to generate snapshots using CutyCapt of some websites using crone job, i get websites' addressess from database and then delete this record after generating screenshots.
i used */5 * * * * /usr/bin/php -f /path/generate.php
it didn't worked for crone job but if i access the same file using browser it works fine, and if run this script using command php from command line it also works fine.
then i just created another file and accessed the url using file_get_contents; added this file to crone job it worked fine for some days but now this approach is also not working. i don't know what happened. i didn't change any of these files.
I also tried wget command to access that url but failed to get required out put.
my crontab is now looks like this
*/5 * * * * wget "http://www.mysite.com/generate.php" -O /dev/null
Strange thing is that crone job executes fine it fetches data from database and deletes record as well but does not update images.
Is there any problem with rights or something similar that prevents it to generate images using crone job but not when accessed using browser.
Please help i am stuck.
I don't know what your script is doing internally, but keep in mind that a user's cron jobs do not inherit the users environment. So, you may be running into a PATH issue if your php script is running any shell commands.
If you are running shell commands from the script, try setting the PATH environment variable from within your php script and see if that helps.
is there any user credintials on this page , such as Basic authentication ?
if so , you have to define the user name and password in wget request like
wget --http-user=user --http-password=password "http://url" ?
and try another solution by running yor script from php command line
so your crontab could look like
*/5 * * * * /usr/bin/php -f /path/to/generate.php
try this solution it will work and it is better than hitting the server to execute background operations on your data
and I hope this helps

Categories