简体   繁体   English

使用动态参数从命令行执行PHP文件

[英]Executing PHP file from command line with dynamic parameters

I need to run one script which will take one date as an argument. 我需要运行一个脚本,它将一个日期作为参数。 It is configured using cron to run on a daily basis, but I need to run it for a time range now. 它使用cron配置为每天运行,但是我现在需要运行一段时间。 The issue is that this will fetch all results if it is ran for one day because of the huge size of the table. 问题是,由于表很大,如果运行一天,它将获取所有结果。 So is it possible to supply the each date from the start date dynamically from a shell script? 那么可以从shell脚本动态地提供从开始日期开始的每个日期吗?

For example: if I need to run update_cron.php from '2013-10-05' then it needs to run 例如:如果我需要从'2013-10-05'运行update_cron.php ,则需要运行

php update_cron.php '2013-10-05'
php update_cron.php '2013-10-06'
php update_cron.php '2013-10-07'

until today (or the end date which is specified). 直到今天(或指定的结束日期)。 I am very new to shell scripting and so it will be helpful if someone can give an input to this. 我对Shell脚本非常陌生,因此如果有人可以对此提供输入将很有帮助。

This function should get you started 此功能应该可以帮助您入门

iterdate() {
    cur="$1"
    end="$2"
    while [ $cur != $end ] 
    do
     echo php update_cron.php $cur 
     cur=$(date -d "$cur + 1 day"  +%Y-%m-%d)
    done
    }

The you can use it like this 你可以这样使用它

$ iterdate 2013-10-05 2013-10-08
php update_cron.php 2013-10-05
php update_cron.php 2013-10-06
php update_cron.php 2013-10-07

Just remove the echo in it to make it actually run the php update command. 只需删除其中的回声即可使其实际运行php update命令。 Note that if you need to run that in a cron job, you might find it easier to make a script of it rather than a function. 请注意,如果您需要在cron作业中运行该脚本,则可能会发现编写脚本而不是函数更容易。 Then copy the following in a text file and make it executable 然后将以下内容复制到文本文件中并使其可执行

#!/bin/bash
cur="$1"
end="$2"
while [ $cur != $end ] 
do
  echo php update_cron.php $cur 
  cur=$(date -d "$cur + 1 day"  +%Y-%m-%d)
done

And then 接着

$ /path/to/iterdate.sh 2013-10-05 2013-10-08
php update_cron.php 2013-10-05
php update_cron.php 2013-10-06
php update_cron.php 2013-10-07

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM