How can I run a script 20 times a day at random times, between 9am and 11pm?
Cron cannot pick random times. Schedule 0,30 9-21 * * * (26 slots) and call a wrapper that sleeps 0-89 minutes and skips slots so ~20 runs land in the window.
How can I run a script 20 times a day at random times, between 9am and 11pm?
Standard cron cannot choose 20 random times or keep them inside a daily window; it only matches clock minutes. The practical pattern is to give cron a set of fixed base slots that stay inside the window even after a random delay, then let a small wrapper script add the delay and decide whether this slot runs at all.
# 26 base slots, 09:00 through 21:30, every day
0,30 9-21 * * *# the crontab line calls a wrapper, not the script itself
0,30 9-21 * * * /path/to/jittered.sh#!/bin/bash
# jittered.sh: wait 0-5399 seconds (up to 89 min 59 s), then run
sleep $((RANDOM % 5400))
/path/to/script.shRead 0,30 9-21 * * * from left to right: 0,30 selects minutes 0 and 30; 9-21 selects every hour from 09:00 through 21:59; the three wildcards allow every day of the month, every month, and every day of the week. The latest slot is 21:30, and a delay of at most 5,399 seconds ends by 22:59:59, so every run lands between 9am and 11pm. A random skip such as [ $((RANDOM % 26)) -lt 20 ] || exit 0 at the top of the wrapper averages 20 runs per day but does not guarantee that count. For exactly 20, pick 20 of the 26 slots once a day, write them to a file, and have each slot check whether it is on the list.
0,30 9-21 * * * on CrontapA random delay after a clock-based trigger can cross either edge of your allowed window. The commonly copied 15,45 8-21 * * * with a delay of up to 99 minutes starts at 08:15, before the window opens, and can finish waiting at 23:24, after it closes. The wrapper above needs #!/bin/bash: $RANDOM does not exist in dash. A probabilistic gate also varies the daily count, so downstream systems may receive fewer or more than 20 runs. If the count and window are requirements rather than preferences, choose the 20 times in one daily process and record them so retries do not create duplicate work.
Read the dedicated guide: Cron business hours.
0,30 9-21 * * * — command
View future cron matches in a calendar
September 2026 · UTC
0 runs
See this cron expression on the calendar view example
Write the cron here. Run it on Crontap.
WordPress, Shopify, Railway, Cloud Run, Vercel, HubSpot, Ghost, your own box. If it answers HTTP, Crontap can drive it on a clock you can read, in the timezone that actually matters, and page you when something breaks.
1.9k+ teams · Free forever tier · No credit card
Permalink: https://tool.crontap.com/help/cron-job-run-script-at-random-times-between-two-hourly-intervals