Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

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

71 views
Skip to first unread message

Avnesh Shakya

unread,
May 19, 2013, 11:54:11 PM5/19/13
to
hi,
How to run a python script twice randomly in a day? Actually I want to run my script randomly in a day and twice only. Please help me.. how is it possible.

Thanks

Jason Friedman

unread,
May 20, 2013, 12:27:15 AM5/20/13
to Avnesh Shakya, python-list
> How to run a python script twice randomly in a day? Actually I want to run my script randomly in a day and twice only

I can think of two basic approaches.
One, use crontab or some other similar functionality to call it exactly twice.
Two, use crontab or some other similar functionality to call it every
minute, and add code to your script to execute exactly twice.
Which are you preferring?

Cameron Simpson

unread,
May 20, 2013, 12:12:05 AM5/20/13
to Avnesh Shakya, pytho...@python.org
Do you mean "run twice a day, each at random times"?

If so, do the obvious: at midnight, pick two random times. Sleep
until the first time, run the script, sleep until the second time,
run the script.

There are various ways to do the sleeping and midnight bits; they're
up to you.

Enjoy,
--
Cameron Simpson <c...@zip.com.au>

The ZZR-1100 is not the bike for me, but the day they invent "nerf" roads
and ban radars I'll be the first in line......AMCN

Cameron Simpson

unread,
May 20, 2013, 3:40:02 AM5/20/13
to Avnesh Shakya, pytho...@python.org
On 20May2013 09:47, Avnesh Shakya <avnes...@gmail.com> wrote:
| On Mon, May 20, 2013 at 9:42 AM, Cameron Simpson <c...@zip.com.au> wrote:
| > On 19May2013 20:54, Avnesh Shakya <avnes...@gmail.com> wrote:
| > | How to run a python script twice randomly in a day? Actually
| > | I want to run my script randomly in a day and twice only. Please
| > | help me.. how is it possible.
| >
| > Do you mean "run twice a day, each at random times"?
| >
| > If so, do the obvious: at midnight, pick two random times. Sleep
| > until the first time, run the script, sleep until the second time,
| > run the script.
| >
| > There are various ways to do the sleeping and midnight bits; they're
| > up to you.
|
| Thanks, Can you mail documentation or link for it? I am totally new for it.

1: Please reply on-list; you asked the list, the discussion should
remain there. I have added the list to the CC line.

2: Please don't top-post. Quote only the relevant bits of the
previous message and reply below. If the message is long, do
that in pieces. [quote] reply [quote] reply. Like a conversation.

Now, to your questions.

A UNIX user would use "cron" to schedule the midnight job and from
the midnight job then probably use "at" to schedule the other jobs
to run at specific times. See "man 1 crontab", "man 5 crontab" to
submit the cron jobs and "man at" to submit the once off jobs.

UNIX means Linux, Solaris, AIX, any of the BSDs (includes MacOSX),
etc.

You could a small python script for the midnight job, and it would
submit the "at" jobs. See the "random" module to compute a pseudorandom
number (and thus a random time), the "datetime" module to compute
the dates and times from that number, and the "subprocess" module
to submit the "at" job for the chosen run times. The modules are
here:

http://docs.python.org/3/py-modindex.html

That presumes python 3; if you're using python 2 the docs for that
are available at the same web site.

You don't need to use "at". Your midnight job could just compute
the time in seconds to each job time, then use the "time.sleep"
function to delay until then. See the "time" module at the above
link.

Nobody on this list will write your program for you.

Attempt to write the program, then come to the list with your attempt
and any problems. People will help if you've made an initial effort,
and continue to try. This question has the "feel" of a homework
question; the point of homework is for you to learn by solving a
problem yourself. It is fine to seek help, but you must make the
effort yourself.

Come back with specific questions.

Cheers,
--
Cameron Simpson <c...@zip.com.au>

I'm not weird; I'm gifted.

Cameron Simpson

unread,
May 20, 2013, 9:12:20 PM5/20/13
to pytho...@python.org
On 20May2013 15:05, Avnesh Shakya <avnes...@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 <c...@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 <pgu...@cs.auckland.ac.nz>

Avnesh Shakya

unread,
May 21, 2013, 12:36:00 AM5/21/13
to Cameron Simpson, pytho...@python.org
Thanks a lot. I got it.

Chris Angelico

unread,
May 21, 2013, 3:56:27 AM5/21/13
to pytho...@python.org
On Tue, May 21, 2013 at 11:12 AM, Cameron Simpson <c...@zip.com.au> wrote:
> 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.

Also, on many systems, /bin/sh is a much lighter interpreter than bash
(eg Debian uses dash). It's more efficient to use that when you can,
even if you use bash for your login shell.

> On 20May2013 15:05, Avnesh Shakya <avnes...@gmail.com> wrote:
> | 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?

You put that into your crontab? I do not think this means what you
think it means; cron does not execute arbitrary Python code.

> - 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".

Nitpick: It's not start, length; it's start, stop-before. If the start
is 10 and the second argument is 20, you'll get numbers from 10 to 19.
But your conclusion is still accurate :)

ChrisA
(two Princess Bride references in as many threads, doing well!)

Jussi Piitulainen

unread,
May 21, 2013, 4:21:39 AM5/21/13
to
Chris Angelico writes:

> > On 20May2013 15:05, Avnesh Shakya wrote:
> > So your call picks a number from 0..58, not 0..59.
> > Say randrange(0,60). Think "start, length".
>
> Nitpick: It's not start, length; it's start, stop-before. If the
> start is 10 and the second argument is 20, you'll get numbers from
> 10 to 19. But your conclusion is still accurate :)

I've sometimes named the latter index "past", as in just past the
range. I'm also happy to call it just "end". The inclusive-style names
might be "first" and "last", so "past" is "last + 1".

The length of the range from "start" to "end" is "end - start" without
a "pest" term that is either -1 or +1 though I forget which; two
consecutive ranges are from b to m, then from m to e; an empty range
is from b to b.

Chris Angelico

unread,
May 21, 2013, 5:18:25 AM5/21/13
to pytho...@python.org
Agreed. The inclusive-exclusive range is by far the most useful.
There's unfortunately a massive case of lock-in here, but Scripture
references would be ever so much more convenient as inc-exc. For
instance, today in family devotions we read Galatians 2:1 - 2:21. At a
glance, do you know whether that's the entire chapter? What if it were
written as Galatians 2 to Galatians 3? Simple!

ChrisA

Cameron Simpson

unread,
May 21, 2013, 6:32:39 AM5/21/13
to Chris Angelico, pytho...@python.org
On 21May2013 17:56, Chris Angelico <ros...@gmail.com> wrote:
| On Tue, May 21, 2013 at 11:12 AM, Cameron Simpson <c...@zip.com.au> wrote:
| > - 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".
|
| Nitpick: It's not start, length; it's start, stop-before. If the start
| is 10 and the second argument is 20, you'll get numbers from 10 to 19.
| But your conclusion is still accurate :)

But it's still a useful thing to think when you're trying to reason
about ranges unless you're doing something unusual.

Cheers,
--
Cameron Simpson <c...@zip.com.au>

Life IS pain, highness... anyone who tries to tell you different is
trying to sell you something. - Wesley, The_Princess_Bride

Dave Angel

unread,
May 21, 2013, 9:54:06 AM5/21/13
to pytho...@python.org
On 05/21/2013 06:32 AM, Cameron Simpson wrote:
> On 21May2013 17:56, Chris Angelico <ros...@gmail.com> wrote:
> | On Tue, May 21, 2013 at 11:12 AM, Cameron Simpson <c...@zip.com.au> wrote:
> | > - 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".
> |
> | Nitpick: It's not start, length; it's start, stop-before. If the start
> | is 10 and the second argument is 20, you'll get numbers from 10 to 19.
> | But your conclusion is still accurate :)
>
> But it's still a useful thing to think when you're trying to reason
> about ranges unless you're doing something unusual.
>


No, it's only happens to look like length when start is zero. So as a
mnemonic, it's highly misleading.


--
DaveA

Cameron Simpson

unread,
May 21, 2013, 11:00:23 PM5/21/13
to Dave Angel, pytho...@python.org
Feh! No self respecting computer scientist would ever count from
other than zero!

Actually, yes, you're right there.

Cheers,
--
Cameron Simpson <c...@zip.com.au>

Q: How does a hacker fix a function which doesn't work for all of the elements in its domain?
A: He changes the domain.
0 new messages