1
votes

I want run a cron job which will run every 20 day of every month and on monday.

From one monday to another monday there should be gap of 20 days.

For example -

If the cron job run on this month monday means on 12th of oct then the next job will be running on 2nd of nov and on monday.

Help me out. I trying but not able to achieve this.

1

1 Answers

1
votes

May be this tool can help you: crontab.guru

if I understand your question is something like this:

#+--------------------------- Minute (0-59)
#|    +---------------------- Hour   (0-23)
#|    |     +---------------- Day    (1-31)
#|    |     |   +------------ Month  (1-12)
#|    |     |   |   +-------- Day of week (0-6, 0=Sunday)
#|    |     |   |   |    +--- Command to be run
#|    |     |   |   |    |
#v    v     v   v   v    v
#====================================================================
# run task At 00:00 on Monday.
 0    0     *  *   1    run_task.sh

In run_task.sh you can put the condition.

# script to check if the task run 20 days ago.
aux_file="/tmp/auxdate.txt"

# this compare the actual date with the date in the file
# if the diference is >= 20 the task run
if [[ ! -e "$aux_file" || $(( ($(date '+%s') - $(cat auxdate.txt))/(60*60*24) )) -ge 20 ]]; then
    echo $(date '+%s') > "$aux_file"
    echo "RUN TASK"
    # run task...
else
    echo "NOT RUN TASK"
fi