Running CodeIgniter cron on localhost - php

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

Related

Setting up a Cron Job on CPanel to executes a PHP script

As implied in the title, the Cron Job is supposed to execute a php file (update.php, to be specific). The php file then writes to a csv file stored in the same directory.
I have the time set to * * * * * so that it executes every minute. The command is written as follows:
php -q /home//public_html/wallboard/update.php
I don't believe this is causing any errors, though it also doesn't seem to write to the CSV file. When I visit update.php in a browser, however, it executes and writes to the CSV file immediately. I'm not experienced with Cron Jobs and I'm sure there's an issue, but I don't know what exactly that issue is. Let me know if you have suggestions/questions. Any help is appreciated!
Current Command:
* * * * * usr/bin/php -q /home/<user>/public_html/wallboard/update.php
update.php:
<?php
include('lib/HelpDeskView.php');
include('lib/WallboardDisplay.php');
include('helpdesk.csv');
$helpdesk = new HelpDeskView();
$text="\r\ntest,test,test";
file_put_contents( "helpdesk.csv" , $text, FILE_APPEND);
Since your script resides in your public_html directory you can use wget for your Cron Job
wget -O - -q https://yoursite.com/wallboard/update.php
-O - output is written to the standard output in this case it will go to the email address you specify in CPanel
-q quiet mode
IMHO the best way is to contact support and ask them about command line syntax.
This is how I'm doing it at my linux server using cPanel.
This runs script.php which is stored in public root. Course, replace <username> in command line with your username.
At another server I'm using same command line with /usr/bin/php instead of php at the beginning of line, but I'm aware that not all servers use same command line. Some require php-cli in command line instead of php, some don't "like" -f argument, etc. So try various combinations.
To find more suggestions check out this SO topic too: Run a PHP file in a cron job using CPanel
Important thing: When trying different commands wait at least a minute (this case) to see if it works because Cron doesn't fire your script immediately.
Try to execute the same command in PHP CLI and check if it gives you any error, you might be missing some libraries or references required for CLI execution.
/usr/bin/php -d register_argc_argv=On /home/USERNAME/public_html/DOMAIN/artisan AMIR:HOME

Cron job with parameters in command line

Trying to set up a couple of cron jobs in cPanel but since the system uses commandline for crons I can´t use parameters - this is a wordpress cron and relies on parameters in url.
How can i get this to work through commandline?
php -q /home/facebnzc/public_html/folkeuniversitetet/wp-cron.php?import_key=NAbeK7X&import_id=1&action=processing
This constant just enables or disables the check for events that are ready to fire. You still
add and manage events as we did up above. We just need to manually hit the wp-cron.php file in our WordPress install often enough to fire our scripts when needed.
If all you have are daily scripts, you can add a cron job like this via the crontab -e
command:
0 0 * * * wget -O - -q -t 1
http://yoursite.com/wp-cron.php?doing_wp_cron=1
Information on how to use cron can be found at its Wikipedia entry. Information on
how to use wget can be found at the wget manual.
The 0 0 * * * part of the preceding entry tells cron to execute this script at 0 minutes
on the 0th hour (midnight) every day of the week.
The wget -O - -q -t 1 http://yoursite.com/wp-cron.php?doing_wp_cron=1 part
uses the wget command to load up the wp-cron.php page in your WordPress install. The
-O - tells wget to send output to devnull, and the -q enables quiet mode. This will keep
cron from adding files to your server or emailing you the outputs of each cron run. The
-t 1 tells cron to try once. This will keep wget from hitting your server multiple times
if the first try fails. If the call to wp-cron.php is failing, the rest of your website is probably failing too; hopefully you’ve already been notified.
Be sure to change yoursite.com to your actual site URL. And finally, the ?doing_wp_cron=1 on the end of the URL is needed since wp-cron.php will check for that
$_GET parameter before running.

call a php page using cron jobs in direct admin

I have a php file that sends an email and it works fine when I open the page with browser. (test.php located in root of my website) But I want the page runs automatically once a day. I found that this is done using cron jobs. I searched a lot and tested a lot of commands and configurations but none of them worked.
I was using * for all time fields assuming that it will run every minute (I didn't like to wait hours to test each configuration)
I tested following commands and many others that I don't remember ):
/usr/bin/php -q http://mysite.com/test.php
/usr/bin/home/php -q http://mysite.com/test.php
/usr/local/bin/php -q http://mysite.com/test.php
/home/myID/php -q http://mysite.com/test.php
#!/usr/local/bin/php -q http://mysite.com/test.php
#!/usr/local/bin/php -q /home/myID/mysite.com/public_html/test.php
Finally, I couldn't figure out what I am doing wrong.
the host is a shared linux host running Direct Admin.
Please tell me a simple step by step guide to set the cron job to run my php file.
thank you
You don't want to call the PHP binary if you're going to run the script via http. You want to use wget (or curl). Example:
*/5 * * * * user /usr/bin/wget http://yoursite.com/cron.html -q -O /dev/null
Where user is the OS user that runs the command.
If you don't have the privileges (shared host) to do something like that, change to use a VPS instance from some provider or use an online cronjob service like https://www.setcronjob.com/.
You could open the page using lynx the command line browser:
lynx -dump http://www.google.com/ > /dev/null
Or use a service like http://cronless.com
Update:
If you setup a cron job in your control panel and it didn't run then most probably your web host disabled it.
My advice is to use a cron job service like cronless.

Crontab not running

I've been having some issues with crontab recently. After switching servers, I realized none of my cronjobs are being run. After looking at PHP info, I realized php was run with CGI, so I realized I had to switch lynx -dump URL_HERE to php -q PATH_HERE.
In the actual PHP file, I stared it out like #!/usr/bin/php -q to define where php is located on my server. However, it's not getting run. I've even set up crontab to send me an email once anything runs. No email. I've checked my junk, trash, spam, and I've even tried switching emails. Nothing.
Here's what I have now: * * * * * php -q /home/USER/public_html/file.php.
If I copy & paste it into the command line, it works wonderfully. If I run it through crontab, it doesn't get run.
You are missing envrionment variables. Try a simple test like this
add this to your crontab
* * * * * set > /tmp/vars
wait 2 -3 minutes, go back and remove the crontab entry you just created.
Next,
From the shell command line you normally use
set > myvars
diff myvars /tmp/vars
This will show you the differEnce in envIrionment. Modify your cron job environment. Just add what is needed.
You can do a couple things in order to debug this.
1) In your crontab change your entry to:
* * * * * php -q /home/USER/public_html/file.php > /tmp/filecron 2>&1
Make sure you edit the entry by typing:
crontab -e
Then run:
tail -f /tmp/filecron
To debug the output as it runs.
2) As sudo user or root, tail the cron log to make sure your cron is executing properly:
sudo tail -f /var/log/cron
The first step will give you information related to the php file itself (syntax errors etc) if that is what is failing. The second step will help you out if the crontab itself is not configured properly.
You are intending this job to run every 5 minutes right ? This is what it will do with your stated line.

Crontab in Plesk

I'm trying to run a test script using crontab within Plesk. The php file simply emails me a message
mail('me#somewhere.com','Cron Test','Test');
My path to php is /user/bin/php
I have entered * in every field, to run the script every minute with the following command:
/usr/bin/php -q /usr/httpdocs/crontest.php
However, the script is not being run.
Can anyone help?
I'm probably missing something simple, I've never used cron before.
Any advice appreciated.
Thanks.
I would start by getting it to write to a log file. eg:
* * * * * /usr/bin/php -q /usr/httpdocs/crontest.php >> /a-location/crontest.log 2>&1
This will at least give you any obvious errors like not being able to find php etc.
I found that when using the user based cron in plesk, there are a number of issues:
first I found that you should reference the script from the virtual domain. If your script has an absolute address of /var/www/vhosts/domain.com/httpdocs/email-this.php, you should reference it as httpdocs/email-this.php in the crontab.
Second, the script has to have very particular permissions, but not sure what they "must be." apache:apache is all that ever worked for me. Even with the group write permission set, user still had to be apache... weird.
Third, the easiest way to do the testing was to edit the crontab directly instead of going back into plesk every time I needed to make a change... Edit your crontab like this:
crontab -u [filesystem-username] -e
Fourth, I could never get the crontab to write to a log file outside of httpdocs (I tried statistics/logs/cron_log every way I could think of... lol... no dice). I ended up just adding the MAILTO directive at the top of the crontab file during testing:
eg:
MAILTO=you#domain.com
## * * * * * php -q httpdocs/cron.php
Also see this if you have Plesk 10 or above: http://shaun.net/2011/09/solving-plesk-10-3-1-cron-issues/
I had to do this
/usr/local/psa/bin/server_pref -u -crontab-secure-shell "/bin/sh"
to get this (example) working: wget -O - http://www.yourdomain.com/cron.php

Categories