2

For a project I want to use wget in a cron to download a data file. In the wget statement, a start- and enddate have to be defined in the following format:

wget --post-data="stns=235&vars=TEMP&start=YYYYMMDDHH&end=YYYYMMDDHH"

Since I want it to be done by a cron job, I would like the start- and enddate to be set automatically. More specific, I would like the startdate to be set to '1 hour ago' and the enddate to 'now'.

There has been a similar question in the post Using date -1day with wget. Here the suggested solution was to put the variables between single quotes, but this did not work. E.g.:

"[...]start='`date -d yesterday +%Y%m%d%H'&end=`date +%Y%m%d%H`"

I simply got the error "Error 400: Bad request" when trying to execute the wget-statement in the terminal.

Thank you.

1 Answers1

2

Within a cron job, % is special and must be escaped. Also, backquote syntax is best avoided. I would suggest something like the following:

wget --post-data="start=$(date ... +\%Y\%m\%d\%H)&end=$(date ... +\%Y\%m\%d\%H)&..."
dhag
  • 15,736
  • 4
  • 55
  • 65