Happy Coder
Happy Coder

Reputation: 4702

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. 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?

For example: if I need to run update_cron.php from '2013-10-05' then it needs to run

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.

Upvotes: 0

Views: 295

Answers (1)

damienfrancois
damienfrancois

Reputation: 59350

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. 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. 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

Upvotes: 1

Related Questions