Release date "7 days ago" in crontab

I am running a cron that includes curl including values ​​for startDate and endDate, which should be in yyyy-mm-dd format. endDate today and startDate 7 days ago. General format:

curl -o ~/location/filename.xml "http://url.asmx/do_this?&startDate=x&endDate=y"

      

Using a fixed launch date, this works via terminal on macosx:

&startDate=2014-10-01&endDate=`date +\%Y-\%m-\%d`"

      

However, remembering that startDate must be 7 days before the system date, both of the methods below result in the error "The date specified was not in the correct format. Dates must be in the format: YYYY-MM-DD, for example, 2011-12- 24 "

startDate=`date -d '7 days ago' +\%Y-\%m-\%d`&endDate=`date +\%Y-\%m-\%d`
startDate=`date --date="7 days ago"  +\%Y-\%m-\%d`&endDate=`date +\%Y-\%m-\%d`

      

Is this a problem with using "and"?

What am I doing wrong here? BTW I'm trying to keep just one command line line and not use a script in a file.

EDIT: From Google Googling and copying and pasting many suggestions, I found that the following works on the Mac forum:

$(date -v-7d +%Y-%m-%d)

      

However, I have not met -in before. I am wondering what it is, is this Mac specific and will these problems be on other servers?

+3


source to share


2 answers


Is this a problem with using "and"?

Maybe. Replacing `` with $ () works your command:



curl -o ~/location/filename.xml "http://url.asmx/do_this?&startDate=$(date -d '7 days ago' +\%Y-\%m-\%d)&endDate=$(date +\%Y-\%m-\%d)"

      

+1


source


Unquoted &

is a command terminator. It will set the definition of the startDate variable in the background (subshell) so that it doesn't exist in the current shell. Demo video:

$ startDate=`date -d '7 days ago' +\%Y-\%m-\%d`&endDate=`date +\%Y-\%m-\%d`
[1] 25595
[1]+  Done                    startDate=`date -d '7 days ago' +\%Y-\%m-\%d`
$ echo $startDate--$endDate
--2014-10-14

      



Some date

recognize it +%F

as a short hand for+%Y-%m-%d

Also, you must be careful when using date

in your crontab entry: the character %

will be newline if not escaped. Read the man page crontab(5)

.

+1


source







All Articles