Currently I'm using /usr/bin/ifNotNearMidnight as:
#!/bin/bash
t=$(date +\%H\%M)
if [[ "$t" > 2350 ]] || [[ "$t" < 0010 ]]
then
exit 1
fi
And then in cron:
* * * * * ifNotNearMidnight && ( cd /blah/ ; doTheThing )
Currently I'm using /usr/bin/ifNotNearMidnight as:
#!/bin/bash
t=$(date +\%H\%M)
if [[ "$t" > 2350 ]] || [[ "$t" < 0010 ]]
then
exit 1
fi
And then in cron:
* * * * * ifNotNearMidnight && ( cd /blah/ ; doTheThing )
@artfulrobot since you're using bash you can skip the subshell and the variable type conversion from string to number:
```
#!/bin/bash
declare -i t
printf -v t '%(%H%M)T' -1
if (((t > 2350) || (t < 10))); then
exit 1
fi
```
Sorta. The "<" does string comparison rather than integer comparison (which would use -lt and have octal issues…good catch on that possibility), but since the date(1) command produces 0-padded output like "0009", it should be fine:
$ [[ 9 < 0010 ]] && echo yep || echo nope
nope
$ [[ 0009 < 0010 ]] && echo yep || echo nope
yep
$ [[ 9 -lt 0010 ]] && echo yep || echo nope
nope
$ [[ 9 -lt 10 ]] && echo yep || echo nope
yep
@artfulrobot FWIW, I find your current method cleaner and more readable, rather than trying to browbeat a crontab/systemd-timer into doing it.
The alternative might be something like (untested)
*/2 1-22 * * * do_thing
12-58/2 0 * * * do_thing
0-48/2 23 * * * do_thing
The duplication makes it harder to read/understand, the edge-cases are more prone to be off-by-one, the meaning isn't as clear, and updating any one of them involves possibly messing with the others.
@artfulrobot the only thing I'd change would be the first "*" since you said every 2 minutes, so it would be
*/2 * * * * do_thing
but I suspect that was just a typo/mistranscription and your crontab is already otherwise correct ☺
Additively.
*/2 1-22 * * * #every 2 min from 0100 through 22:59
2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48 23 * * * # every 2 min from 2300-2348
12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56,58 0 * * * # every 2 min from 0:12 through 0:58
@dashdsrdash
Your approach is good, but can be done more compactly:
*/2 1-22 * * * # 0100 to 22:59
10-59/2 0 * * * # 0010 to 0059
0-49/2 23 * * * # 2300 to 23:49
@momo @dashdsrdash
Yeah, that's a possibility. I think I'll stick with my workaround though to keep crontab simple to look at!
@artfulrobot
if i'm not mistaken you can do something like this:
# run every 2 minutes from 1 to 22:59
*/2 1-22 * * * foo
# run every 2 minutes between 23:00 and 23:50
0-50/2 23 * * * foo
# run every 2 minutes between 0:10 and 0:59
10-59/2 0 * * * foo
not exactly the prettiest but it should do the trick (i hope).