简体   繁体   中英

How do I modify this cron script to give date 2 days back

In the crontab, after a script I see a parameter: date -d "($(date +\\%Y\\%m)-15) day ago" '+\\%Y\\%m\\%d'

This generates a date - "20190822" if ran on 23-Aug-2019 ie a day back.

My questions is:

1) What is the purpose of "- 15" and "$(date +\\%Y\\%m\\%d)" here?

2) If I want to generate 2 days back, what do I do?

I have tried: date -d "2 days ago" '+%Y%m%d' This works on the bash screen but this doesn't run the job in the crontab.

This looks like you try a fixed day of the month with incorrect date format. The purpose of "-15" here is to set the day to 15th day of the month, then with "day ago" you go back one day. Also with %Y%m you only get Year and Month, If you get 20190822 there is somewhere a date +%Y%m%d in your script.

To go back two days:

    date -d "-2 days" +%Y%m%d

I've tried this script:

    date_test=$(date -d "2 days ago" +%Y%m%d)
    echo $date_test > ~/test/date_test.out

And got 20190821 in my file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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