Skip to content

Instantly share code, notes, and snippets.

@sc250024
Last active April 29, 2017 04:18
Show Gist options
  • Save sc250024/df037d8ed48e71d2b64d16ba8fa7df89 to your computer and use it in GitHub Desktop.
Save sc250024/df037d8ed48e71d2b64d16ba8fa7df89 to your computer and use it in GitHub Desktop.

Revisions

  1. sc250024 revised this gist Jan 11, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion cron-between-times.sh
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@
    # * * * * * /path/to/some/command
    # */15 * * * * /path/to/another/command
    # * 2,15 * * * /path/to/yet/another/command
    # Or pass one via STDIN
    # Or pass text via STDIN
    CRONFILE="$1"

    # Use GNU `date` binary instead of default macOS one
  2. sc250024 revised this gist Jan 11, 2017. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions cron-between-times.sh
    Original file line number Diff line number Diff line change
    @@ -5,6 +5,7 @@
    # * * * * * /path/to/some/command
    # */15 * * * * /path/to/another/command
    # * 2,15 * * * /path/to/yet/another/command
    # Or pass one via STDIN
    CRONFILE="$1"

    # Use GNU `date` binary instead of default macOS one
    @@ -15,7 +16,7 @@ DATE="/usr/local/Cellar/coreutils/8.26/bin/gdate"
    PYTHON="/usr/local/bin/python"

    BEGINDATE="2017-01-13 23:00:00"
    ENDDATE="2017-01-14 3:00:00"
    ENDDATE="2017-01-14 03:00:00"

    BEGINYEAR=$(${DATE} --date="${BEGINDATE}" +%Y)
    BEGINMONTH=$(${DATE} --date="${BEGINDATE}" +%m)
    @@ -51,4 +52,5 @@ while read line; do

    fi

    done < <(cat ${CRONFILE})
    # done < <(cat ${CRONFILE})
    done < "${CRONFILE:-/dev/stdin}"
  3. sc250024 created this gist Jan 11, 2017.
    54 changes: 54 additions & 0 deletions cron-between-times.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,54 @@
    #!/bin/bash

    # Specify text file with cron-style schedule, and commands
    # Like the following example
    # * * * * * /path/to/some/command
    # */15 * * * * /path/to/another/command
    # * 2,15 * * * /path/to/yet/another/command
    CRONFILE="$1"

    # Use GNU `date` binary instead of default macOS one
    DATE="/usr/local/Cellar/coreutils/8.26/bin/gdate"

    # Path to Python; make sure the module
    # `croniter` is installed using `pip install croniter`
    PYTHON="/usr/local/bin/python"

    BEGINDATE="2017-01-13 23:00:00"
    ENDDATE="2017-01-14 3:00:00"

    BEGINYEAR=$(${DATE} --date="${BEGINDATE}" +%Y)
    BEGINMONTH=$(${DATE} --date="${BEGINDATE}" +%m)
    BEGINDAY=$(${DATE} --date="${BEGINDATE}" +%d)
    BEGINHOUR=$(${DATE} --date="${BEGINDATE}" +%H)
    BEGINMINUTE=$(${DATE} --date="${BEGINDATE}" +%M)
    BEGINUNIX=$(${DATE} --date="${BEGINDATE}" +%s)

    ENDYEAR=$(${DATE} --date="${ENDDATE}" +%Y)
    ENDMONTH=$(${DATE} --date="${ENDDATE}" +%m)
    ENDDAY=$(${DATE} --date="${ENDDATE}" +%d)
    ENDHOUR=$(${DATE} --date="${ENDDATE}" +%H)
    ENDMINUTE=$(${DATE} --date="${ENDDATE}" +%M)
    ENDUNIX=$(${DATE} --date="${ENDDATE}" +%s)

    OUTPUTFILE="crons-${BEGINHOUR}-${ENDHOUR}.txt"

    while read line; do

    cronminute=$(echo "${line}" | awk '{print $1}')
    cronhour=$(echo "${line}" | awk '{print $2}')
    cronday=$(echo "${line}" | awk '{print $3}')
    cronmonth=$(echo "${line}" | awk '{print $4}')
    crondow=$(echo "${line}" | awk '{print $5}')

    NEXTRUN=$(${PYTHON} -c "from croniter import croniter;from datetime import datetime;base = datetime(${BEGINYEAR}, ${BEGINMONTH}, ${BEGINDAY}, ${BEGINHOUR}, ${BEGINMINUTE});iter = croniter('${cronminute} ${cronhour} ${cronday} ${cronmonth} ${crondow}', base);print iter.get_next(datetime)")

    NEXTRUNUNIX=$(${DATE} --date="${NEXTRUN}" +%s)

    if [[ ${NEXTRUNUNIX} -ge ${BEGINUNIX} && ${NEXTRUNUNIX} -le ${ENDUNIX} ]]; then

    echo "${line}" >> "${OUTPUTFILE}"

    fi

    done < <(cat ${CRONFILE})