Cron expression to run every day starting from a date

…衆ロ難τιáo~ 提交于 2019-12-02 06:06:17

Assuming that after January 25th you want to run this process forever after (i.e. 2032, when probably the server will be already substituted), I would do it with three expressions:

0 0 12 25-31 1 * 2016  command # Will run the last days of Jan 2016 after the 25th
0 0 12 * 2-12 * 2016   command # Will run the rest of the months of 2016
0 0 12 * * * 2017-2032 command # will run for every day of years 2017 and after.

I hope this helps.

There are multiple ways to accomplish this task, One could be running a script with cron job and testing conditions and if true run actually required scripts otherwise skip.

Here is an example,

20 0 * * * home/hacks/myscript.sh

and in myscript.sh put your code to test conditions and run actual command/script

Here is an example for such a script,

#!/bin/bash

if( ( $(date) <= "31-01-2016" ) || ( $(date) >= "25-02-2017" ) ){

   // execute your command/script
}else {
     // do Nothing
}

You can write a date expression which only matches dates after a particular point in time; or you can create a wrapper for your script which aborts if the current date is before the time when the main script should run

#!/bin/bash
# This is GNU date, adapt as required for *BSD and other variants
[[ $(date +%s -d 2018-02-25\ 00:00:00) > $(date +%s) ]] && exit
exec /path/to/your/real/script "$@"

... or you can schedule the addition of this cron job with at.

at -t 201802242300 <<\:
schedule='0 0 12 25/1 * ? *'   # update to add your command, obviously
crontab=$(crontab -l)
case $crontab in
    *"$schedule"*) ;;          # already there, do nothing
    *) printf "%s\n" "$crontab" "$schedule" | crontab - ;;
esac
:

(Untested, but you get the idea. I just copy/pasted your time expression, I guess it's not really valid for crontab. I assume Quartz has a way to do something similar.)

The time specification to at is weird, I managed to get this to work on a Mac, but it might be different on Linux. Notice I set it to run at 23:00 on the previous night, i.e. an hour before the planned first execution.

This is a brief copy from my answer here. The easiest way is to use an extra script which does the test. Your cron would look like :

# 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  12  *  *  *  daytestcmd 1 20160125 && command1
  0  12 *  *  *   daytestcmd 2 20160125 && command2

Here, command1 will execute every day from 2016-01-25 onwards. command2 will execute every second day from 2016-01-25 onwards.

with daytestcmd defined as

#!/usr/bin/env bash
# get start time in seconds
start=$(date -d "${2:-@0}" '+%s')
# get current time in seconds
now=$(date '+%s')
# get the amount of days (86400 seconds per day)
days=$(( (now-start) /86400 ))
# set the modulo
modulo=$1
# do the test
(( days >= 0 )) && (( days % modulo == 0))
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!