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

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

Related

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

PHP Cron job on MAMP / OSX

I've tried following the instructions here but I still can't get my cron job working on my mac using crontab.
I've saved the following cron job:
*/2 * * * * /usr/bin/php http://localhost/social-api-examples/cron.php
And when I type crontab -l it shows me it's saved. But my PHP page isn't being hit. Is there a way to check if it's working?
Also, when I saved the cron job, it was to a temporary file location. Is that ok?
The solution was just to use "php" instead of the path to php:
*/2 * * * * php http://localhost/social-api-examples/cron.php
/usr/bin/php is not an HTTP client: you need to pass it a file path not a URL.
Alternatively, if it is important to involve a web server, you can replace /usr/bin/php with an HTTP client such as curl or wget.
You should at least test the command you are trying to execute
You cannot execute a URL with php http://somesite.com/index.php
instead use wget if it's installed on your system:
wget http://somesite.com/index.php

Run a php script with cron in mac

I am trying to run in mac a php script using cron. I want this php script to run every one minute. I read several sources online and from my understanding is better if I use launchd. In any case, I try to make it work with cron and then if it works fine I might try to use launchd.
So here is what I do:
I wrote this command in a txt file:
* * * * * /usr/bin/php /Applications/MAMP/htdocs/php_test/main_script.php
There I have the path to php (I found it with the "which php" command) and the path to my php script.
I named the txt file: crontasks.txt and I run it through terminal with this command:
crontab /Users/dkar/Desktop/crontasks.txt
When I list the crons (crontab -l) I see that this job is listed. But nothing comes as output every one minute. The output is supposed to be an image stored in a specified folder. What am I missing here?
Thanks in advance
D.
You might have a slight miscomprehension how crond processes the "crontab" files. It would suffice to run the command crontab -e, which would let you edit your personal crontab or you edit the system crontab.
If you use crontab -e it will open the default editor (vi/vim) and you can enter the line:
* * * * * /usr/bin/php /Applications/MAMP/htdocs/php_test/main_script.php
Then you simply save your file. If you want to use the system crontab you will have to edit it directly and enter the line. Additionally the system crontab has one more field for the username. Like this:
* * * * * /usr/bin/php root /Applications/MAMP/htdocs/php_test/main_script.php
On the next full minute your script will be executed by crond.

PHP Cronjob does not execute

I am running Ubunutu Linux server, PHP5, Apache2 and am having trouble getting any sort of cronjob to run through the crontab.
I edit the crontab using
crontab -e
I save the file I want to run:
*/5 * * * * php /home/user/public_html/crx/cronx.php
it saves fine. I can run the file from the console and goes through fine. I can't even find any existing logs for the file. I checked cron was running, stopped and started... no change.
The current php file is just a simple test script that inserts a single line into a database.
I checked the permissions for the file and has read and write. Am absolutely stumped. I can't seem to get ANYTHING to run through cron. Is there something I can run to test permissions?
EDIT
I have also tried the following command
/usr/bin/php /home/user/public_html/crx/cronx.php
I used whereis php and which php to locate and confirm it is all running in the right area
You have too many * values for your times.
Also, cron may not have a PATH set up correctly to use PHP.
Instead try:
*/5 * * * * /usr/bin/php /home/user/public_html/crx/cronx.php
Where /usr/bin/php is the actual path to PHP. From the console you can run which php to see the path to the PHP binary you should use.
EDIT: Here are a couple of more things to try in order to troubleshoot:
# see if cron is running just by having it create a file
*/5 * * * * touch /tmp/crontab-$(date +%s)
Another option:
Set the permissions of your PHP script to 755, and change the beginning to:
#!/usr/bin/php -q
<?php
// rest of script
Then change your cron tab to:
*/5 * * * * /home/user/public_html/crx/cronx.php
I'm still not sure if cron is the issue or the running of the PHP script.

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