Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I need to randomize the time before a given command starts. I realize it's relatively trivial to do this within a script, or to write a "wrapper" script to do this, but I'm wondering if anyone knows a lightweight binary program that's already out there that will accomplish this without requiring an interpreter to be loaded.

EDIT: More specifically, I do not want to involve bash in any way. Assume for the sake of argument that no shell is available and I'm invoking this from a non-interactive program.

share|improve this question
add comment

3 Answers 3

up vote 4 down vote accepted

If you want finer-grained control than maxschlepzig's nice bash incantations, it's a reasonably easy thing to just code up:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>

int main(int argc, char**argv){
  useconds_t mdelay=0, delay;
  if (argc<3){
    fprintf(stderr,"%s <delay (in milli-seconds)> <command> <args>* :\n\trun commands with a random delay\n",argv[0]);
    exit(1);
  }
  mdelay=atol(argv[1]);
  /* seed random number generator with the time */
  srand(((unsigned int)time(NULL))%RAND_MAX); 
  delay = mdelay * (rand() / (1.0 + RAND_MAX));
  usleep(delay*1000);
  execvP(argv[2],getenv("PATH"),argv+2);

  return 0;
}

Compile with something like gcc randomdelay.c -o randomdelay and invoke it like

$ randomdelay 10000 echo Hi!

If you are doing this is a programming context you might be better off just grabbing the part of the code that picks the random delay for you and calls an exec family function (which one you want depends on exactly how you have it specified internally).

Issue here:

  • This assumes your systems rand/srand are sane (not good PRNGs mind you just that they do what the man page says they do). I've been having some trouble with it on my Mac OS X box, where those functions are deprecated in favor of random/srandom.
  • Historically, many implementation of rand have had poor numeric characteristics. That shouldn't be a problem in this application, but if it is, replace it with a better PRNG.
  • The command line argument handling in this toy is a bit primitive.
  • The delay is chosen uniformly from some range. For some applications you might prefer an unbounded distribution like an exponential. Stack Overflow has many questions on how to get non-uniform distributions from a uniform PRNGs.
share|improve this answer
    
This is precisely what I was talking about! Thanks!! –  ultrasawblade Jun 11 '11 at 13:54
    
you could use *rand48() family instead of rand(). –  J.F. Sebastian Oct 13 '11 at 18:01
add comment

In bash you can do it like this:

sleep $(($RANDOM%3)) && some_command

thus waiting between 0 and 2 seconds before executing the command. Or choose another interval.

Or if $RANDOM is not available you can try jot:

sleep `jot -r 1 0 3` && some_command

Sure, the random waiting plus executing is not provided by a single binary, but close. Well, having a single binary would be less orthogonal, it would kind of be against the one-tool-for-one-job-principle (and the keep-it-simple-principle (KISS)).

share|improve this answer
add comment

The simplest is the sleep command. To run your program ten seconds from nowdo something like this:

sleep 10 && your_command

Next up if you need a little more sophistication would be the at scheduler. You could schedule a task to run in an hour like so:

echo "your_command" | at +1 hour
share|improve this answer
    
This doesn't randomize the time though (I imagine you could pull it off easily enough with $RANDOM). It also sounds like the "script" method he wants to avoid -- he wants a binary that does it for some reason –  Michael Mrozek Jun 8 '11 at 20:43
    
I was blind to the word "random" when I red the question. Sleep would would still work if you feed it a random number. –  Caleb Jun 9 '11 at 8:27
add 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.