0

I'm trying to get a past date inside a cron job using something like this. But the DATE variable is always empty.

DATE=date -d "$date -1 days -1 months" +%Y-%m
* * * * * /bin/echo "Date: $($DATE) Test" >> /tmp/crontab.log 2>&1

I know how tricky crontab is thanks to this question with good answers, but the problem here doesn't seem to be the % sign, because the code above works fine:

DATE=date -d @2147483647 +%Y-%m
* * * * * /bin/echo "Date: $($DATE) Test" >> /tmp/crontab.log 2>&1

But if I use quotes, then the job again can't get the date.

DATE=date -d"@2147483647" +%Y-%m
* * * * * /bin/echo "Date: $($DATE) Test" >> /tmp/crontab.log 2>&1

I tried replacing quotes with single quotes, double quotes, escaping the quotes, but none of this options solved the problem. And I need quotes to specify the "$date -1 days -1 months" part. Is there a way to do this in crontab without creating an external script?

CentOS 7 and crontab (cronie-1.4.11-23.el7.x86_64)

0

1 Answer 1

0

The issue has nothing to do with cron but with variable expansion within $(). The date command called in the subshell $() doesn't see a parameter -d "$date -1 days -1 months" but multiple params instead: -d "$date -1 days -1 months. You probably want to use $(eval $DATE) instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.