Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

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

share|improve this question
add comment

4 Answers

up vote 9 down vote accepted

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

or

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.

Edit

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.

share|improve this answer
    
that's an approach I had not considered - and could be made recursive, if file contains the next at run, correct? –  warren Sep 17 '09 at 8:15
    
great - just tried it out, and works great :) –  warren Sep 17 '09 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 '09 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 '09 at 5:21
    
the script I'm envisioning won't care how many copies of itself are running at once –  warren Sep 19 '09 at 12:42
add comment

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.

share|improve this answer
    
if you're caching the webpages, you're not necessarily going to be getting accurate load results –  warren Dec 24 '11 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! –  Andy Lee Robinson Dec 24 '11 at 17:06
1  
aha! .. very cool approach :) –  warren Dec 24 '11 at 19:01
add comment

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.

share|improve this answer
    
@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 '09 at 5:23
add comment

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.

#!/bin/bash
sleep $(($RANDOM%5))
/path/to/command -param1 -param2
share|improve this answer
1  
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 '09 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 '09 at 3:46
    
well, that's true. –  A Dwarf Sep 17 '09 at 3:58
1  
+1. you do give a valid answer :) –  A Dwarf Sep 17 '09 at 3:59
    
Why sleep is a bad idea? Just asking... –  Tadeusz A. Kadłubowski Sep 17 '09 at 5:56
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.