3

As the question is fairly self-explanatory, I would like to know how to set a cronjob to execute any random weekday without using any external script.

EDIT: In case people have not understood this question, We have 5 weekdays, viz., Monday, Tuesday, Wednesday, Thursday, Friday. I have a server running 24/7. I want to set a cron that will run on any 1 weekday per week.

Note: I highly doubt this is a duplicate question, as this question is not pertaining to daily/weekly/monthly cronjobs.

Varun Chandak
  • 157
  • 11
  • Computers are quite bad at true randomness, *"execute any random weekday"* is that *"run **at least** once a week"* or *"run **exactly** once a week" or it is also okay if it will at times *"run **on all 5 workdays** in a single a week", skip a week, to run twice the next... As I mentioned in the [linked duplicate](https://serverfault.com/a/815541/376810) *"Cron is all things considered a very basic scheduler and the syntax does not easily allow an administrator to formulate slightly more uncommon schedule"* - One option is to start a batch with `sleep $RANDOM` statement to delay execution – HBruijn Dec 12 '18 at 10:33
  • 1
    Let it run daily, but check for the existence of a file first (which you create after the first complete, successful run). – gxx Dec 12 '18 at 10:40
  • Cron has no support for that. You can do it by wrapping the script. The wrapper should run the script with 20% probability on Monday, with 25% probability on Tuesday, with 33% on Wednesday, and so on. It should also disable running if it has already run on this week. It is a trivial unix script. Note, doing things *randomly* is typically a bad idea, you need to make your systems so deterministic as you only can! – peterh Dec 12 '18 at 12:06
  • Btw, also I disagree that your question would be a dupe. However I would suggest you to think twice, if you really needs a random behavior. If it doesn't work, how will you debug it? If you need to think on, what caused what a system, the complexity of a deterministic system grows linearly with its size, a non-deterministic grows exponentially. Of course I can imagine an enough strong reason for this cron, but I think it is much more likely, that you only want to fix/avoid something with it. Do that on a deterministic way! – peterh Dec 12 '18 at 12:07
  • 2
    Btw, such interesting problems having more connection to shellscripting than to professional system administration, may have a better welcome on the https://unix.stackexchange.com . – peterh Dec 12 '18 at 12:12

1 Answers1

3

Would something like this work for you? It selects a random day from a bash array days (note the SHELL prefix) and then writes a cronjob (random_runner) under /etc/cron.d to run the script /home/foobar/myscript.

You could place this in /etc/cron.d/random_generator and generate a new "runner" every day.

SHELL=/bin/bash
27 8 * * * root  days=(Mon Tue Wed Thu Fri Sat Sun); rd="$(( RANDOM \% 7 ))"; day="${days[$rd]}"; echo "45 15 * * $day root /home/foobar/myscript" > /etc/cron.d/random_runner
Server Fault
  • 3,454
  • 7
  • 48
  • 88