Skip to main content
added 67 characters in body
Source Link
Sotto Voce
  • 7.3k
  • 1
  • 14
  • 29

You have it right. If you want it to run every half hour from 13:00 until 17:00 but not at 17:30, I would write the cron entries like this:

# foobar.sh runs weekdays at the top of the hour from 13:00 through 17:00
# it also runs weekdays at the bottom of the hour from 13:30 through 16:30 (not 17:30)
0   13-17  *  *  1-5  foobar.sh
30  13-16  *  *  1-5  foobar.sh

IMO comments are key to preventing someone from misunderstanding the crontab entries. (that someone is often yourself a few months after the implementation, when many of the details are forgotten)

 

With a(I originally had example shell script, having it test for code that would detect being invoked at 17:30 and exiting can be pretty simple:exit. However the question specifically excluded this approach, and a number of criticisms were also made in comments, so I've removed the example as not relevant to the question)

# the cron entry will run this script at 17:30, but that's not desired
hhmm=`date +%H:%M`
if [ "$hhmm" = "17:30" ]; then
  # output something about the time being the cause of the exit
  exit 0
fi

You have it right. If you want it to run every half hour from 13:00 until 17:00 but not at 17:30, I would write the cron entries like this:

# foobar.sh runs weekdays at the top of the hour from 13:00 through 17:00
# it also runs weekdays at the bottom of the hour from 13:30 through 16:30 (not 17:30)
0   13-17  *  *  1-5  foobar.sh
30  13-16  *  *  1-5  foobar.sh

IMO comments are key to preventing someone from misunderstanding the crontab entries. (that someone is often yourself)

With a shell script, having it test for 17:30 and exiting can be pretty simple:

# the cron entry will run this script at 17:30, but that's not desired
hhmm=`date +%H:%M`
if [ "$hhmm" = "17:30" ]; then
  # output something about the time being the cause of the exit
  exit 0
fi

You have it right. If you want it to run every half hour from 13:00 until 17:00 but not at 17:30, I would write the cron entries like this:

# foobar.sh runs weekdays at the top of the hour from 13:00 through 17:00
# it also runs weekdays at the bottom of the hour from 13:30 through 16:30 (not 17:30)
0   13-17  *  *  1-5  foobar.sh
30  13-16  *  *  1-5  foobar.sh

IMO comments are key to preventing someone from misunderstanding the crontab entries. (that someone is often yourself a few months after the implementation, when many of the details are forgotten)

 

(I originally had example shell script code that would detect being invoked at 17:30 and exit. However the question specifically excluded this approach, and a number of criticisms were also made in comments, so I've removed the example as not relevant to the question)

Changed to standard equals operator
Source Link
Sotto Voce
  • 7.3k
  • 1
  • 14
  • 29

You have it right. If you want it to run every half hour from 13:00 until 17:00 but not at 17:30, I would write the cron entries like this:

# foobar.sh runs weekdays at the top of the hour from 13:00 through 17:00
# it also runs weekdays at the bottom of the hour from 13:30 through 16:30 (not 17:30)
0   13-17  *  *  1-5  foobar.sh
30  13-16  *  *  1-5  foobar.sh

IMO comments are key to preventing someone from misunderstanding the crontab entries. (that someone is often yourself)

With a shell script, having it test for 17:30 and exiting can be pretty simple:

# the cron entry will run this script at 17:30, but that's not desired
hhmm=`date +%H:%M`
if [ "$hhmm" === "17:30" ]; then
  # output something about the time being the cause of the exit
  exit 0
fi

You have it right. If you want it to run every half hour from 13:00 until 17:00 but not at 17:30, I would write the cron entries like this:

# foobar.sh runs weekdays at the top of the hour from 13:00 through 17:00
# it also runs weekdays at the bottom of the hour from 13:30 through 16:30 (not 17:30)
0   13-17  *  *  1-5  foobar.sh
30  13-16  *  *  1-5  foobar.sh

IMO comments are key to preventing someone from misunderstanding the crontab entries. (that someone is often yourself)

With a shell script, having it test for 17:30 and exiting can be pretty simple:

# the cron entry will run this script at 17:30, but that's not desired
hhmm=`date +%H:%M`
if [ "$hhmm" == "17:30" ]; then
  # output something about the time being the cause of the exit
  exit 0
fi

You have it right. If you want it to run every half hour from 13:00 until 17:00 but not at 17:30, I would write the cron entries like this:

# foobar.sh runs weekdays at the top of the hour from 13:00 through 17:00
# it also runs weekdays at the bottom of the hour from 13:30 through 16:30 (not 17:30)
0   13-17  *  *  1-5  foobar.sh
30  13-16  *  *  1-5  foobar.sh

IMO comments are key to preventing someone from misunderstanding the crontab entries. (that someone is often yourself)

With a shell script, having it test for 17:30 and exiting can be pretty simple:

# the cron entry will run this script at 17:30, but that's not desired
hhmm=`date +%H:%M`
if [ "$hhmm" = "17:30" ]; then
  # output something about the time being the cause of the exit
  exit 0
fi
Rollback to Revision 1
Source Link
Sotto Voce
  • 7.3k
  • 1
  • 14
  • 29

You have it right. If you want it to run every half hour from 13:00 until 17:00 but not at 17:30, I would write the cron entries like this:

# foobar.sh runs weekdays at the top of the hour from 13:00 through 17:00
# it also runs weekdays at the bottom of the hour from 13:30 through 16:30 (not 17:30)
0   13-17  *  *  1-5  foobar.sh
30  13-16  *  *  1-5  foobar.sh

Or:

# foobar.sh runs on week days at top and bottom of the 13 to 16 hours 
# and at 17:00
0,30  13-16  *  *  1-5  foobar.sh
0     17     *  *  1-5  foobar.sh

IMO comments are key to preventing someone from misunderstanding the crontab entries. (that someone is often yourself)

With a shell script, having it test for 17:30 and exiting can be pretty simple:

# the cron entry will run this script at 17:30, but that's not desired
case $(datehhmm=`date +%H:%M) in
  (17:[23]?) # also account for the script taking over one minute to
             # start or the clock being readjusted back a few%M`
            if #[ milliseconds"$hhmm" at== the"17:30" wrong]; timethen
  # output something about the time being the cause of the exit
  exit 0
fi

Anecdotally, the script might still end up running when scheduled at 17:30 if the system is suspended at 17:30:00.01 for instance just before date is run and resumed after 17:40. Or not being run when scheduled at 16:00 if suspended at 16:00:00.01 and resumed between 17:20 and 17:40.

You have it right. If you want it to run every half hour from 13:00 until 17:00 but not at 17:30, I would write the cron entries like this:

# foobar.sh runs weekdays at the top of the hour from 13:00 through 17:00
# it also runs weekdays at the bottom of the hour from 13:30 through 16:30 (not 17:30)
0   13-17  *  *  1-5  foobar.sh
30  13-16  *  *  1-5  foobar.sh

Or:

# foobar.sh runs on week days at top and bottom of the 13 to 16 hours 
# and at 17:00
0,30  13-16  *  *  1-5  foobar.sh
0     17     *  *  1-5  foobar.sh

IMO comments are key to preventing someone from misunderstanding the crontab entries. (that someone is often yourself)

With a shell script, having it test for 17:30 and exiting can be pretty simple:

# the cron entry will run this script at 17:30, but that's not desired
case $(date +%H:%M) in
  (17:[23]?) # also account for the script taking over one minute to
             # start or the clock being readjusted back a few
             # milliseconds at the wrong time
  # output something about the time being the cause of the exit
  exit 0
fi

Anecdotally, the script might still end up running when scheduled at 17:30 if the system is suspended at 17:30:00.01 for instance just before date is run and resumed after 17:40. Or not being run when scheduled at 16:00 if suspended at 16:00:00.01 and resumed between 17:20 and 17:40.

You have it right. If you want it to run every half hour from 13:00 until 17:00 but not at 17:30, I would write the cron entries like this:

# foobar.sh runs weekdays at the top of the hour from 13:00 through 17:00
# it also runs weekdays at the bottom of the hour from 13:30 through 16:30 (not 17:30)
0   13-17  *  *  1-5  foobar.sh
30  13-16  *  *  1-5  foobar.sh

IMO comments are key to preventing someone from misunderstanding the crontab entries. (that someone is often yourself)

With a shell script, having it test for 17:30 and exiting can be pretty simple:

# the cron entry will run this script at 17:30, but that's not desired
hhmm=`date +%H:%M`
if [ "$hhmm" == "17:30" ]; then
  # output something about the time being the cause of the exit
  exit 0
fi
Avoid deprecated backticks, non-standard == and adjust to account for potential time drifts.
Source Link
Stéphane Chazelas
  • 586.8k
  • 96
  • 1.1k
  • 1.7k
Loading
Source Link
Sotto Voce
  • 7.3k
  • 1
  • 14
  • 29
Loading