how to run php file using cron jobs
Cron runs commands as they would be ran via the shell, so running PHP would use local paths.
You need to use a command like:
php /home/USER/public_html/cron.php
Or if including the query string is necessary, use cURL instead (if it's installed):
curl http://www.wetube.org/cron.php?id=01001
You might want to look at not exposing your cron scripts to the internet - move them to outside your web directory because if someone finds it they can constantly reload it to spam your cron scripts (i.e. sending lots of emails)
Execute PHP script in cron job
Automated Tasks: Cron
Cron is a time-based scheduling service in Linux / Unix-like computer operating systems. Cron job are used to schedule commands to be executed periodically.You can setup commands or scripts, which will repeatedly run at a set time. Cron is one of the most useful tool in Linux or UNIX like operating systems. The cron service (daemon) runs in the background and constantly checks the /etc/crontab file, /etc/cron./* directories. It also checks the /var/spool/cron/ directory.
Configuring Cron Tasks
In the following example, the crontab command shown below will activate the cron tasks automatically every ten minutes:*/10 * * * * /usr/bin/php /opt/test.php
In the above sample, the */10 * * * * represents when the task should happen. The first figure represents minutes – in this case, on every "ten" minute. The other figures represent, respectively, hour, day, month and day of the week.*
is a wildcard, meaning "every time".
Start with finding out your PHP binary by typing in command line:
whereis php
The output should be something like:php: /usr/bin/php /etc/php.ini /etc/php.d /usr/lib64/php /usr/include/php /usr/share/php /usr/share/man/man1/php.1.gzSpecify correctly the full path in your command.
Type the following command to enter cronjob:
crontab -e
To see what you got in crontab.EDIT 1:
To exit from vim editor without saving just click:Shift+:
And then type q!
How to run a PHP script using a Cron job
try
0/2 * * * * curl http://[your_id:port]/kyrill/filetest.php
Running PHP file using crontab
To execute devicecheck.php every 1 hour try the following:
Method A :: Execute the script using php from the crontab
# crontab -e
00 * * * * /usr/bin/php/var/www/devicecheck.php
Method B: Run the php script using URL from the crontabIf your php script can be invoked using an URL, you can lynx, or curl, or wget to setup your crontab as shown below.
The following script executes the php script (every hour) by calling the URL using the lynx text browser. Lynx text browser by default opens a URL in the interactive mode. However, as shown below, the -dump option in lynx command, dumps the output of the URL to the standard output.
00 * * * * lynx -dump http://www.yourwebsite.com/yourscript.php
The following script executes the php script (every 5 minutes) by calling the URL using CURL. Curl by default displays the output in the standard output. Using the “curl -o” option, you can also dump the output of your script to a temporary file as shown below.*/5 * * * * /usr/bin/curl -o temp.txt http://www.yourwebsite.com/yourscript.php
The following script executes the php script (every 10 minutes) by calling the URL using WGET. The -q option indicates quite mode. The “-O temp.txt” indicates that the output will be send to the temporary file.*/10 * * * * /usr/bin/wget -q -O temp.txt http://www.yourwebsite.com/yourscript.php
UPDATE::# chmod a+x /home/username/yourscript.php
# crontab -e
00 * * * * /home/username/yourscript.php
Setting up a Cron Job on CPanel to executes a PHP script
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
Run Cron Job on PHP Script, on localhost in Windows
recently I had sort of problems to run a cron job on a php script on localhost (WAMP server) in windows 7, when I was on a test to chronically fetch some links from www out there.
By the way I am sharing this for anyone that is on the same thing.
You will need a shellscript to run chronically, using Windows Task Scheduler. Also you will need a batch script (script.bat) to call the php.exe and run your php script (here called as my_process.php
)
shellscript.vbs
Set WinScriptHost = CreateObject("WScript.Shell")
WinScriptHost.Run Chr(34) & "C:\path\to\script\script.bat" & Chr(34), 0
Set WinScriptHost = Nothing
script.bat"C:\wamp\bin\php\php5.4.12\php.exe" -f "C:\wamp\www\website\my_process.php"
Now, we are ready to set the Windows Task Scheduler to run shellscript.vbs at the required time interval:- Open Task Scheduler from windows Start menu
- Go to Action menu and hit Create Task...
- in General tab, fill the Name and Description fields as you want
- in Triggers tab, hit New button.
- from Begin the Task dropdown, select On a schedule and choose Daily
- from Advanced settings section, select Repeat task every as you want and set for a duration on Indefinitely.
- on Actions tab, from Action dropdown, select Start a program.
- on the Program\script box, enter path to shellscript.vbs like C:\path\to\shellscript.vbs.
- leave Add argumentts (optional) section empty.
- in Start in (optional) box, enter parent directory of shellscript.vbs like
C:\path\to\
. - Hit upvote on this tutorial :) Have fun.
Related Topics
A Better Way to Replace Emoticons in PHP
Typecasting VS Function to Convert Variable Type in PHP
Getting Hour and Minute in PHP
Denormalize Nested Structure in Objects with Symfony 2 Serializer
PHP - Regex - How to Extract a Number with Decimal (Dot and Comma) from a String (E.G. 1,120.01)
MySQL into Outfile Override Existing File
PHP Dom Textcontent VS Nodevalue
Php:Capturing the Command Output
Difference Between Directoryiterator and Filesystemiterator
How to Increase by 1 All Keys in an Array
Laravel Errno 150 Foreign Key Constraint Is Incorrectly Formed
Change Mime Type of Output in PHP
Are There Any PHP Docblock Parser Tools Available
Best Way to Get Hostname with PHP
How to Sort an Array by Similarity in Relation to an Inputted Word