How to write a auto executable script in php?
From the basic of php i know that php needs to have some action/request to execute so i am little confused about how to do it. I know it can be done but don't know how.
I want to write a php script which will run开发者_如何学运维 in server every 6 hours and update the database info from an api.
More Info:
The server i am currently working is in linux. But i want to know how i can do it in both linux and windows.
UPDATE:
Cron does not find my script. I don't know where is the problem is. I have used this command in my cpanel
0 */6 * * * php public_html/path_to_dir/file_to_run.php
I have setup the cron so cPanel send me email. The email i am getting is showing some error.
/bin/sh: 0: command not found
Looking forward to your help.
You need to have something run the script on a timer. This is typically going to be cron (on UNIX based systems such as Linux, OS X, BSD, etc) or Windows Task Schedular (on Windows).
You can use crontab to schedule a process in Unix.
I assume that you're using a Linux based S.O.
- Install the php5-cli package as
root with
apt-get install php5-cli
(or your pkg manager). - Write and test your script
with the PHP CLI,
php filename.php
. - Login as selected
user and set up a crontab using
crontab -e
- Write the crontab line:
* */6 * * * php /full-path/filename.php
/var/log/messages
should log the crontab activities.
精彩评论