Run a cron-job every 14 days starting from day X
If you want to do this, have a look at this answer.
Run a cron-job only ones 14 days after day X
If you want to do this, there are a few options you have. Assume that day X is today (2018-09-06), then I give you the following options:
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7)
# | | | | |
# * * * * * command to be executed
0 0 6 9 * [[ `date '+\%Y'` == "2018" ]] && sleep 1209600 && command1
0 0 20 9 * [[ `date '+\%Y'` == "2018" ]] && command2
0 0 * * * /path/to/daytestcmd 14 && command3
- The first option here is simply checking if the year is correct. If so, sleep for 14 days and then execute
command1
- The second option is by computing the day itself and execute the script on that day. Again, you have to check manually for the correct year.
The third option is much more flexible for your purpose. The idea is to create a script called daytestcmd
which does the check for you. The advantage of this script is that you only need to update one line in the script to change the test and never change the cronjob. The script would look something like:
#/usr/bin/env bash
# Give reference day
reference_day="20180906"
# Give the number of days to await execution (defaults to zero)
delta_days="${1:-0}"
# compute execution day
execution_day=$(date -d "${reference_day} + $delta_days days" "+%F")
[[ $(date "+%F") == "$execution_day" ]]
So, the next time you want to wait 14 days, just update the script and change the variable reference_day
.
note: the OP updated the question. The above script can also be adjusted where reference_day
is obtained via a set of commands, for example to retrieve the day of dev deployment. Imagine get_dev_deployment_day
is a command which returns that day, then you just need to update
reference_day=$(get_dev_deployment_day)