Random Cron Job Possible?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • roly
    Confirmed User
    • Aug 2002
    • 1844

    #1

    Random Cron Job Possible?

    i want to execute a script once per day randomly in a 6 hour period e.g. execute a script between 5am and 11am every day. obviously if i'm using a cronjob it's got to be a regular time every day, anyone got a suggestion of how i can execute it randomly?

    thanks in advance
    Last edited by roly; 07-09-2008, 08:37 AM.
  • xenilk
    Confirmed User
    • Jan 2006
    • 921

    #2
    My idea of doing this would be to run the cronjob multiple time over the 6 hour span and have a random variable inside the script to make it execute or not. You will also need a value stored somewhere to see if you've already executed the script within the last 6 hours.

    Good luck!
    40$/Page PSD->XHTML/CSS Transformations

    Comment

    • Babaganoosh
      ♥♥♥ Likes Hugs ♥♥♥
      • Nov 2001
      • 15841

      #3
      Originally posted by xenilk
      My idea of doing this would be to run the cronjob multiple time over the 6 hour span and have a random variable inside the script to make it execute or not. You will also need a value stored somewhere to see if you've already executed the script within the last 6 hours.

      Good luck!
      Exactly.

      Generate a random number between 1-10, then if the random number is < or = a variable then execute the script. Otherwise exit with no action taken. If you set the variable to 2 for instance your script would have a 20% chance of executing.
      I like pie.

      Comment

      • Dido
        Confirmed User
        • Sep 2006
        • 217

        #4
        Actually, that still leaves the chance that your script will never, ever execute.
        Try just cronning your script once and having your script sleep for a randomized number of seconds.
        That way, it will run, you just won't be certain when

        (By the way, shells by default don't have a random number generator as far as I know. You might have to find yourself a random number "program" for that)
        Dido

        ADAMO Advertising - Your ULTIMATE traffic partner!

        If you need traffic or have traffic, we'd love to help you make the best out of it!

        ICQ:24209500 - Skype:diederikvanschaik

        Comment

        • Dido
          Confirmed User
          • Sep 2006
          • 217

          #5
          Oh, actually. It seems that bash has something of a $RANDOM enviroment variable which is .. probably pseudo-random enough for what you want.
          Dido

          ADAMO Advertising - Your ULTIMATE traffic partner!

          If you need traffic or have traffic, we'd love to help you make the best out of it!

          ICQ:24209500 - Skype:diederikvanschaik

          Comment

          • Babaganoosh
            ♥♥♥ Likes Hugs ♥♥♥
            • Nov 2001
            • 15841

            #6
            Originally posted by Dido AskJolene
            Actually, that still leaves the chance that your script will never, ever execute.
            Try just cronning your script once and having your script sleep for a randomized number of seconds.
            That way, it will run, you just won't be certain when

            (By the way, shells by default don't have a random number generator as far as I know. You might have to find yourself a random number "program" for that)
            That's kinda true. I didn't really read his post. There's still a simpler way to do this.

            Generate a random number between 1-6. Store that along with the date in a flat file. When the script runs, have it choose a number between 1-6. If that = the random number then execute the script.

            You'd have to keep track of the numbers already picked for the day as well as increment the date after the script has already run for the day. Make sure the script doesn't execute again if the date in the text file == today.
            I like pie.

            Comment

            • roly
              Confirmed User
              • Aug 2002
              • 1844

              #7
              thanks guys for all the responses, there's some good sugestions there, i'll go away and have a play with it and see if i can come up with something using your suggestions. i'll post back if i come up with something usable. too be honest i assumed there would already be a utility or something that did this already.

              thanks again

              Comment

              • Babaganoosh
                ♥♥♥ Likes Hugs ♥♥♥
                • Nov 2001
                • 15841

                #8
                Originally posted by roly
                thanks guys for all the responses, there's some good sugestions there, i'll go away and have a play with it and see if i can come up with something using your suggestions. i'll post back if i come up with something usable. too be honest i assumed there would already be a utility or something that did this already.

                thanks again
                There may be something out there already. I'm a programmer by trade so it's in my nature to over-complicate everything.
                I like pie.

                Comment

                • roly
                  Confirmed User
                  • Aug 2002
                  • 1844

                  #9
                  Originally posted by Babaganoosh
                  There may be something out there already. I'm a programmer by trade so it's in my nature to over-complicate everything.
                  i'm not a programmer as you can probably tell

                  i'm just having a quick search on google and found the php sleep() function (its a php script i'm trying to run). which maybe a solution but has anyone used this before and if so can i use this for a period of hours (2 hours would do)? and would this function be affected by my php.ini settings e.g. max_exectution_time
                  etc?
                  Last edited by roly; 07-09-2008, 09:51 AM.

                  Comment

                  • d-null
                    . . .
                    • Apr 2007
                    • 13724

                    #10
                    Originally posted by Babaganoosh
                    That's kinda true. I didn't really read his post. There's still a simpler way to do this.

                    Generate a random number between 1-6. Store that along with the date in a flat file. When the script runs, have it choose a number between 1-6. If that = the random number then execute the script.

                    You'd have to keep track of the numbers already picked for the day as well as increment the date after the script has already run for the day. Make sure the script doesn't execute again if the date in the text file == today.

                    why 1-6?

                    __________________

                    Looking for a custom TUBE SCRIPT that supports massive traffic, load balancing, billing support, and h264 encoding? Hit up Konrad!
                    Looking for designs for your websites or custom tubesite design? Hit up Zuzana Designs
                    Check out the #1 WordPress SEO Plugin: CyberSEO Suite

                    Comment

                    • ADL Colin
                      Too lazy to set a custom title
                      • Feb 2001
                      • 11929

                      #11
                      Originally posted by jetjet
                      why 1-6?
                      6 hours. You could use 7 and include both end points.

                      5am 6am 7am 8am 9am 10am 11am


                      Adult Date Link - $50 PPS starting NOW! -- good and JUICY!

                      skype = "adultdatelink"

                      Comment

                      • buyandsell
                        Confirmed User
                        • May 2008
                        • 692

                        #12
                        just make a wrapper in perl or something

                        Comment

                        • gornyhuy
                          Chafed.
                          • May 2002
                          • 18041

                          #13
                          A simpler approach:
                          1)Have 1 daily cron job run a script that generates a random time tag for the day and store it in a db or text file
                          2)have a second cron running once ever 30 minutes or whatever and check to see if it has passed the randomly generated time. If it has, fire off the task and reset the stored time so it only does it once.

                          icq:159548293

                          Comment

                          • roly
                            Confirmed User
                            • Aug 2002
                            • 1844

                            #14
                            Originally posted by gornyhuy
                            A simpler approach:
                            1)Have 1 daily cron job run a script that generates a random time tag for the day and store it in a db or text file
                            2)have a second cron running once ever 30 minutes or whatever and check to see if it has passed the randomly generated time. If it has, fire off the task and reset the stored time so it only does it once.
                            this looks like the way i'll make it work, thanks

                            Comment

                            • GrouchyAdmin
                              Now choke yourself!
                              • Apr 2006
                              • 12085

                              #15
                              Why not just use cron to do a daily 'at'? I fail to see the need for abusing cron for two jobs that cron, and at, would do better.

                              Comment

                              • teg0
                                Confirmed User
                                • Jan 2006
                                • 4204

                                #16
                                run the cron job at a set time, but have the code that it runs, php or other hand the randomization. A simple example would be to have it generate a random number between 1 and 10 and if the number if 7 then run execute the rest of the job, if not exit.... then it'll try it again next time the cron job runs. You'd be bound to have it be 7 now and then.

                                Comment

                                • roly
                                  Confirmed User
                                  • Aug 2002
                                  • 1844

                                  #17
                                  Originally posted by GrouchyAdmin
                                  Why not just use cron to do a daily 'at'? I fail to see the need for abusing cron for two jobs that cron, and at, would do better.

                                  i've never heard of 'at' before with regards to cron, will that achieve some sort of randomization?

                                  Comment

                                  Working...