在指定的时间范围内运行php文件


Run a php file in a specified timeframe

我有一个php脚本,假设fetch.php将从其他数据库中提取数据并将这些数据插入到另一个数据库,我如何通过命令行自动运行脚本,假设我想让它每天下午3点运行?比如cron

下面是你应该添加的cron规则:

00 15 * * * php fetch.php

阅读更多:使用cron作业每天运行一个脚本


如果你是在Windows上,你可以使用Windows任务调度程序。链接如下:http://windows.microsoft.com/en-US/windows/schedule-task#1TC=windows-7

从命令行使用php并不难。像这样启动fetch.php文件:

#!/usr/bin/env php
<?php
// You have the command line arguments available as
$scriptName = $argv[0];
$firstArg = $argv[1];
// Add PHP code here :)
// If an error occurs, exit the script with an error code:
if($someError) exit(1);

保存后,设置执行权限:

$ chmod 755 fetch.php

然后将脚本添加到crontab中。有很多方法,像Plesk和cPanel这样的控制面板有一个web界面,但这里是命令行版本:

$ crontab -e

添加到文件:

0 15 * * * /path/to/fetch.php