-1

I am looking for a cron expression which will help in setting the cron job for current date + 14 days. This is a repeated exercise which will take place whenever a new code is deployed into dev environment and then use the current date of dev will used to schedule the trigger for prod after 14 days.

Example : This a BAMBOO CLI Command to set scheduled triggers in a Bamboo Plan. --action addEnvironmentTrigger --deploymentProject "Deploy ZDEPLOY4774565-BASE" --environment "QA" --type "Scheduled" --description "scheduled trigger" --schedule "1 0 0 ? * *"

Now once the dev deployment is successfully completed , then it will create a scheduled trigger for prod to be deployed after 14 days .so every time dev is deployed . I need to use the current date of dev .

Navraj Saini
  • 1
  • 1
  • 4

2 Answers2

0

Following cron expression will run 14 days once

0 5 */14 * * /your/command/

If you like to run only once from current date on 20th September 2018

0 0 20 9 ? 2018  /command

WARNING: The year column is not supported in standard/default implementations of cron.

Jana
  • 11
  • 2
  • this is a repeated exercise which will take place whenever e new code into dev environment and then the current date of dev will used to trigger the prod after 14 days – Navraj Saini Sep 06 '18 at 09:39
0

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)
kvantour
  • 25,269
  • 4
  • 47
  • 72