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.
Related
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
This is my first contact with cron jobs, so I'm sorry if my question sounds dumb.
BackWPup is for making a back up automatically after some period of time, but its own cron job does not work correctly. It starts only when I sign into wp-admin. So I decided to use the server's cron jobs, but I don't know how to. It says:
If you would use the cron job of your hoster you must point it to the
url: http://example.com/wp-cron.php
Also, I want to know how to remove a job.
Note: I have only ssh access, there is no hosting control panel. OS: CentOS.
I guess you should add the command
wget http://example.com/wp-cron.php >> /path/to/my/wp-cron.log 2>&1
to the crontab. Of course you can use any other CLI http-tool instead of wget, but it's the most simple I know and I think is sufficient here.
Call
crontab -e
then add a line like
0 * * * * wget http://example.com/wp-cron.php >> /path/to/my/wp-cron.log 2>&1
This will call this command every hour. For further information see man crontab.
can you help me setup cron job on godaddy webhosting? I have php file that i need to run, it is located in cron subdirectory (so address is http://test.com/cron/file.php).
What do i need to write in command input field, so this file is runned?
NOTE: GoDaddy has been migrating all hosting packages over to cPanel. The itemized instructions below are for the older GoDaddy interface. The command is still the same.
At the time of this writing, on GoDaddy shared hosting, i could NOT use the following commands: ping, curl, nc, lynx
but i COULD use: wget
I successfully created a cron job using wget to load a PHP file containing a call to mail().
log into your GoDaddy account
click to expand the "Web Hosting" section and find the server in question
click the "Manage" button (it used to be labeled "Launch")
on the "Hosting Details" page in "Tools" section, click "Cron Job Manager" button
on the "Cron Job Manager" page, click "Create Cron Job" button
enter the title you want and select the frequency (1 hour is the most frequent allowed EDIT: GoDaddy has added 15-minute increments to the frequency choices.)
enter the command below (with your info):
wget http://YOUR_DOMAIN/YOUR_PATH/YOUR_PHP_FILE.php > /dev/null 2>&1
edit: as noted by Leandro, this is the method to make a cron job call a remote or local resource -- consult GoDaddy documentation if you want to call a resource locally only (which is also more secure if you're running more sensitive jobs)
in "YOUR_PHP_FILE.php" code all the actions you want to be performed and include a call to mail() (or whichever mail method you may want to use assuming you have configured that properly).
By using mail() the SMTP relay server will already be set properly in the "php.ini" file to: relay-hosting.secureserver.net -- which you can confirm using phpinfo().
php_path -q file_name_with_absolute_path
/usr/bin/php -q /home/[user name]/public_html/test.php
1: How to know your php_path?
echo exec('whereis php');
2: How to know absolute path of your file?
echo dirname(__FILE__);
Cron Setup for GoDaddy Shared Hosting Accounts using Cpanel.
*-->>Cron jobs run on GoDaddy's time zone in Arizona. Go Daddy doesn't publish this anywhere.
Example:
Run cron everyday at 1305 (1:05pm) pacific standard time.
5 14 * * * /usr/local/bin/php -q /home/username/public_html/scriptname.php
Your cron job command should look something like the following (unless your directory structure is different of course):
/web/cgi-bin/php5 "$HOME/html/sendy/scheduled.php" > /dev/null 2>&1
Regrads,
shahana
If you are using Godaddy this should solve your issue.
* * * * * /usr/local/bin/php /home/path/to/your/file.php > /dev/null
In Godaddy Linux hosting. I used this command to run cron job.
/usr/bin/php public_html/now your path
Use, for example CURL or wget or lynx.
lynx -s http://link.to/script.php
/usr/local/bin/php -q /home/[user name]/[path to the file]
Reference
You can setup cron jobs through the Hosting Control Center. Check out GoDaddy's official page here: https://www.godaddy.com/help/create-cron-jobs-3548 for a how-to on setting it up.
If you want to run a cron job in Godaddy. You can find following command, it will surely help you.
php -f /home/[user name]/[path to the file]
I am running a cron job every five minutes which calls a php script to check to see if users have imported any files for processing.
When I run the php script by going to the address in my web browser it runs the script and then sends the user a notification by email. When I run the script using the cron job, the script works fine, but it doesn't send the user an email. Any thoughts about why no email is sent?
I'm running Ubuntu Hardy LTS. The cron job is:
*/5 * * * * /usr/bin/wget -–delete-after http://www.mywebsite.com/import_processing.php >/dev/null 2>&1
I'm using delete-after so that I don't get copies of the script piling up in my server directory. I'm suppressing output and errors also as I don't need email confirmation myself.
The script uses the basic mail function, and as I said, works just fine when run from my browser.
Update: It looks like the issue is my php script is looking for a browser cookie to send the email. I imagine I'll have to find another way to get the user's identity.
run it like this
*/5 * * * * /usr/bin/php /var/www/htdocs/blah/blah/import_processing.php >/dev/null 2>&1
when you use wget you are downloading the file,
with php you are running the file,
test your script running
/usr/bin/php /var/www/htdocs/blah/blah/import_processing.php >/dev/null 2>&1
using the local path, just in case run
$ which php
to figure out where it is installed
The script was running properly using wget or php; the problem was that the php script looked for a browser cookie which didn't exist when the page was run outside of a web browser.
I changed the php script to do a database lookup instead and it worked just fine. Thanks for your suggestions.
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