1

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.

Abhishek Singh
  • 42
  • 2
  • 11

1 Answers1

1

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
Maske
  • 824
  • 2
  • 17
  • 35
  • 1
    From one monday to another monday there should be gap of 20days. I want to achieve this. I have edited my question. – Abhishek Singh Oct 09 '20 at 12:04
  • Ok... in this case, i think that you need to make a script and a auxiliar file to save the last time when the task run. The cron work every monday and ask if the task was runed more than twenty days ago, in this case stamp the date in the file and run the task. – Maske Oct 09 '20 at 13:06
  • 1
    I am not much into scripting. Figuring this would take a lot of time. Can you help me in the script. – Abhishek Singh Oct 09 '20 at 13:14
  • 1
    I edit the answer with a simple script that solve this problem, check it, I hope that it help you. – Maske Oct 09 '20 at 13:35