Find the answer to your Linux question:
Results 1 to 10 of 10
Does anyone here knows how to make a random sleep of a maximum desired duration?. for example i want execute wget after a time that may vary from 0 to ...
  1. #1
    Just Joined!
    Join Date
    Jun 2007
    Posts
    13

    random sleep

    Does anyone here knows how to make a random sleep of a maximum desired duration?.
    for example i want execute wget after a time that may vary from 0 to 2 hours
    thanks a lot!

  2. #2
    Just Joined!
    Join Date
    Jun 2007
    Posts
    13
    When i do this in a terminal window it works fine:
    value=$RANDOM
    echo $value

    but in a script it does not works WHYYYYY!!!!???????

    #!/bin/bash

    value=$RANDOM
    echo $value


    PLEASE HELP!!!

  3. #3
    Linux User
    Join Date
    Jun 2007
    Posts
    318
    It doesn't work in a script? Sure you don't have a typo because it works for me. This is what I came up with:

    value=$RANDOM
    ct=`echo "scale=0; $value / 4.5509722" | bc`
    sleep $ct

    RANDOM returns a no. between 0 and 32767. The 4.5509722 scales it down to 0 and 7200 (0 to 2 hours).

  4. #4
    Just Joined!
    Join Date
    Jun 2007
    Posts
    13

    huge problem

    Please i've tried what you suggested, in a term window it works but in a .sh file it does not.
    Whats going on????

  5. #5
    Just Joined!
    Join Date
    Jun 2007
    Posts
    13

    there are no typing errors, still.....

    Please help im desperate!

  6. #6
    Linux User
    Join Date
    Jun 2007
    Posts
    318
    Post your script as an attachment.

  7. #7
    Just Joined!
    Join Date
    Jun 2007
    Posts
    13

    Here It Is

    Its a copy paste of your reply

  8. #8
    Just Joined!
    Join Date
    Jun 2007
    Posts
    13

    here it goes

    sorry didn't send it in last reply here it is
    Attached Files Attached Files

  9. #9
    Linux User
    Join Date
    Jun 2007
    Posts
    318
    Don't know what's happening. What the bash manpage says is:

    If RANDOM is unset, it loses its special properties, even if it is subsequently
    reset.

    Sounds like RANDOM is being unset for some reason but I don't know why.


    Another way without using RANDOM is this:

    ct=`dd bs=200 count=1 < /dev/urandom | tr -cd '[:digit:]' | head -c4`
    # Produce a random number from 0 to 9999.

    if [ "$ct" = "" ]; then ct=0; fi
    # Assume 0 if urandom didn't return any digits.

    ct=`echo "scale=0; $ct / 1.38875" | bc`
    # Scale down to 7200 seconds (2 hours).

    Gotta go. Hope this helps.

  10. #10
    Just Joined!
    Join Date
    Jun 2007
    Posts
    13

    really strange

    This works but im really surprised, the previous commands do work but only in the terminal window and don't inside a bash (.sh) file.
    Thanks a lot for all your help this is thing was crucial for me.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...