Maybe I'm completely nuts, and if so - that's fine. But how would I schedule a job to on-some-random-schedule broadcast a goofy message, like from fortune?

yes - this is my personal workstation - not planning to do something wonky on a production server :)

4 Answers 4


at has a simpler interface for this type of purpose if at is installed, the machine is running atd and the user is allowed to use the command.

For example (check the exact syntax using man at or info at),

at -f file now + 53 minutes


at -f file now + 2 hours

will run the commands in the specified file in 53 minutes or 2 hours, respectively.

at can then be re-run at the start of the scheduled job with suitable (random or otherwise) start, count, and time-units.


As Arjan points out helpfully below, if you are using this for other than a toy application you need to think about issues such as what happens

  1. if the next run starts before the previous one finishes (e.g. is the script re-entrant?) or

  2. if a run fails to complete correctly or

  3. if the next run fails to start at all or on time (e.g. what happens if the machine is off when the next run is due to start) and

  4. about the logging and reporting of failed or successful runs.

  • that's an approach I had not considered - and could be made recursive, if file contains the next at run, correct?
    – warren
    Sep 17, 2009 at 8:15
  • great - just tried it out, and works great :)
    – warren
    Sep 17, 2009 at 8:27
  • 2
    +1, but a few things to consider: if the at-command is the first line, and if the script runs for a long time (or if the intervals are short), then beware of running multiple instances simultaneously. But when at is at the last line of the script, then obviously no re-scheduling will be done if the script fails. Also, running the script from the command line will make it (re-)schedule itself as well (and: multiple schedules could then exist simultaneously). And cron supports sending any errors (or: output in general) through email; I don't know if at has something like that as well?
    – Arjan
    Sep 17, 2009 at 8:33
  • @Arjan - I'm not too worried about it failing and not sending me an email.. since (at this point) I'm just using it to have fun on my workstation :) ..however - that IS something to think about if this were expanded to, say, a random check for intrusion
    – warren
    Sep 18, 2009 at 5:21
  • the script I'm envisioning won't care how many copies of itself are running at once
    – warren
    Sep 19, 2009 at 12:42

I have many commands in /etc/crontab doing all kinds of things and some need precision in seconds. Now cron can have finer resolution while keeping all the times visible and centralised.

This example gets and parses a web page between 10 and 50 seconds before every 5 minutes:

4-59/5 * * * * root (sleep $(($RANDOM\%40+10))) && /etc/munin/plugins/someplugin prefetch

It doesn't have to have an exact time as the data is slow moving, but it helps to keep munin-node execution time as low as possible by not having to wait for webpages. (The plugin further caches the page for 30 mins to reduce unnecessary hits, but needs to update a database every 5 mins).

Note the \% — cron substitutes % for "\n" — useful for keeping mail text on one line.

  • if you're caching the webpages, you're not necessarily going to be getting accurate load results
    – warren
    Dec 24, 2011 at 16:05
  • 1
    yes, I know - 'data is slow moving' - the plugin gets currency exchange rates from a site and I don't want to be banned. (I'm not a trader but I spend a lot of time having to watch munin graphs) I made a similar plugin for local weather. Endless applications! Dec 24, 2011 at 17:06
  • 1
    aha! .. very cool approach :)
    – warren
    Dec 24, 2011 at 19:01

Internally to cron, I don't think such a feature exists. I think the best solution would be to place your command into a shell script, and place a random sleep interval at the top of the script and use the $RANDOM variable. Then run the script once a day in a regular cron job.

sleep $(($RANDOM%5))
/path/to/command -param1 -param2
  • 2
    sleep() is an almost guarantee it will be killed by the service provider. Unless of course, he IS the service provider.
    – A Dwarf
    Sep 17, 2009 at 3:44
  • 1
    I assumed it was on his own Linux machine or something if hes presenting fortune at random to someone
    – John T
    Sep 17, 2009 at 3:46
  • well, that's true.
    – A Dwarf
    Sep 17, 2009 at 3:58
  • Why sleep is a bad idea? Just asking... Sep 17, 2009 at 5:56
  • clarified the question for @John T
    – warren
    Sep 17, 2009 at 6:29

It will depend on what type of interval you are after. but let us assume you would like for the script to run randomly with a probability of 1:30 (once every 30 minutes)

  • Build your cron script for a peridiocity of, say 10 minutes. It will fire 3 times every 30 minutes.

  • Inside the script use rand as the condition for an if statement. You want rand to return a number between 0 and 2. If it is 0 execute the contents of the if statement.

Essentially your script always fires at a set interval. But will only execute the command you are interested randomly. Your service provider will thank you.

  • @A Dwarf - thanks for this idea, too - this would be a slightly more useful route if I wanted to check for intrusions etc on some frequent but quasi-random basis
    – warren
    Sep 18, 2009 at 5:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .