How to run a python script twice randomly in a day?

Cameron Simpson cs at zip.com.au
Mon May 20 21:12:20 EDT 2013


On 20May2013 15:05, Avnesh Shakya <avnesh.nitk at gmail.com> wrote:
|     Thanks a lot.

No worries, but ...

AGAIN:
- please DO NOT top post. Post below, trimming the quoted material.
- please POST TO THE LIST, not just to me. This is a public discussion.

Now...

| I did something.
| I have created test.sh file in which i put-
| 
| #!/bin/bash
| cd /home/avin/cronJob
| python try.py

Ok, good. Some minor remarks:

Personally, I always use:

  #!/bin/sh

instead of requiring bash. All UNIX systems have sh, bash is only
common. And even when present, it may not be in /bin. /bin/sh is
always there, and unless you're doing something quite unusual, it
works just fine.

| then i went on terminal -
| and run crontab -e
| and wrote-
|   */2 * * * * bash /home/avin/cronJob/test.sh
| and saved it.

IIRC, this runs every two minutes. Good for testing, but not your original spec.

Also, if you make the shell script (test.sh) executable you do not
need to specify the interpreter. Treat your script like any other
command! So:

  chmod +rx /home/avin/cronJob/test.sh

and then your cron line can look like this:

  */2 * * * * /home/avin/cronJob/test.sh

Also, treat your script the same way as your shell script, start
it with a #!  like this:

  #!/usr/bin/python

Make it executable:

  chmod +rx /home/avin/cronJob/try.py

and then you don't need to say "python" in your shell script:

  ./try.py

(You need the ./ because the current directory is not in your command
search path ($PATH).)

| It's working fine.
| but when I m using like
| 
| import random
| a = random.randrange(0, 59)
| */a * * * * bash /home/avin/cronJob/test.sh
| then it's showing error becose of varable 'a', so now how can i take
| variable?

I take it that this is your python program intended to schedule the two randomly timed runs?

As a start, it must all be python. The first two lines are. The third line is
a crontab line.

So as a start, you need to look more like this:

  #!/usr/bin/python
  import random
  a = random.randrange(0, 59)
  cronline = '*/%d * * * * /home/avin/cronJob/test.sh' % (a,)
  print(cronline)

At least then you can see the cron line you're making. It still
does not add it to a cron job.

Some remarks:

- randrange() is like other python ranges: it does not include the end value.
  So your call picks a number from 0..58, not 0..59.
  Say randrange(0,60). Think "start, length".

- My recollection is that you wanted to run a script twice a day at random times.
  Your cron line doesn't do that.

- If you're picking random run times you want to schedule a once-off
  job for each to run at a particular times. Cron schedules repeating
  jobs.  To run at a particular time you want an "at" job.

- You need to do one of two things in the pick-a-time script:
    - pick a time, then sleep until that time and then directly
      invoke the try.py script
  or
    - pick a time, then use the "at" command to schedule the try.py
      (or test.sh) script.

The first approach would look a bit like this (totally untested):

  #!/usr/bin/python
  import random
  import subporcess
  import time
  # choose range time in the next 24 hours
  when = random.randrange(0, 24 * 3600)
  # sleep that many seconds
  time.sleep(when)
  subprocess.call(['/home/avin/cronJob/test.sh'])

For two runs, pick two times. Swap them into order. Sleep twice,
once until the first time and then once until the second time. Etc.

The second approach (using "at") would not sleep. instead, compute
(using the datetime module) the date and time each job should run,
and invoke "at" using the subprocess module, piping the text
"/home/avin/cronJob/test.sh\n" to it.

Cheers,
-- 
Cameron Simpson <cs at zip.com.au>

On a related topic, has anyone looked at doing a clean-room copy of CSS
a la RC2 and RC4 a few years back?  I know one or two people have
looked at this in an informal manner, but we couldn't find anyone who
hadn't already seen the DeCSS code to act as the clean person (it says
a lot for the status of their "trade secret" that we couldn't actually
find anyone who didn't already know  it).
        - Peter Gutmann <pgut001 at cs.auckland.ac.nz>



More information about the Python-list mailing list