Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; '(using': 0.07; '21,': 0.07; 'interpreter.': 0.07; 'bash': 0.09; 'executable': 0.09; 'module)': 0.09; 'present,': 0.09; 'run,': 0.09; 'skip:/ 10': 0.09; 'subject:script': 0.09; 'testing,': 0.09; 'runs': 0.10; 'subject:How': 0.10; 'cc:addr:python-list': 0.11; 'python': 0.11; 'random': 0.14; '"python"': 0.16; "'a',": 0.16; '(it': 0.16; 'compute': 0.16; 'discussion.': 0.16; 'fine.': 0.16; 'informal': 0.16; 'jobs.': 0.16; 'manner,': 0.16; 'personally,': 0.16; 'picks': 0.16; 'quoted': 0.16; 'runs,': 0.16; 'script,': 0.16; 'secret"': 0.16; 'secret"': 0.16; 'simpson': 0.16; 'subject:run': 0.16; 'subject:python': 0.16; '(you': 0.16; 'wrote:': 0.18; 'looked': 0.18; 'bit': 0.19; 'value.': 0.19; 'command': 0.22; 'import': 0.22; 'email addr:gmail.com>': 0.22; 'minutes.': 0.22; 'shell': 0.22; 'cc:addr:python.org': 0.22; 'error': 0.23; 'module,': 0.24; 'script.': 0.24; 'specify': 0.24; 'cheers,': 0.24; '(or': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'script': 0.25; 'order.': 0.26; 'skip:" 30': 0.26; 'this:': 0.26; 'second': 0.26; 'post': 0.26; 'least': 0.26; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'unix': 0.29; 'related': 0.29; "doesn't": 0.30; 'css': 0.30; 'start,': 0.30; 'message-id:@mail.gmail.com': 0.30; 'went': 0.31; 'code': 0.31; 'lines': 0.31; 'that.': 0.31; 'are.': 0.31; 'invoke': 0.31; 'lot.': 0.31; 'material.': 0.31; 'minor': 0.31; 'post.': 0.31; 'schedules': 0.31; 'anyone': 0.31; 'file': 0.32; 'run': 0.32; 'quite': 0.32; 'text': 0.33; 'says': 0.33; 'skip:# 10': 0.33; 'third': 0.33; 'there,': 0.34; 'created': 0.35; 'something': 0.35; 'good.': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'add': 0.35; 'picking': 0.36; 'doing': 0.36; 'next': 0.36; "didn't": 0.36; 'thanks': 0.36; 'subject:?': 0.36; 'should': 0.36; 'seconds': 0.37; 'two': 0.37; 'skip:& 10': 0.38; 'requiring': 0.38; 'list,': 0.38; 'does': 0.39; "couldn't": 0.39; 'how': 0.40; 'even': 0.60; 'range': 0.61; "you're": 0.61; 'first': 0.61; 'times': 0.62; 'act': 0.63; 'choose': 0.64; 'pick': 0.64; 'more': 0.64; 'hours': 0.66; 'subject:day': 0.69; 'day': 0.76; 'topic,': 0.81; 'back?': 0.84; 'now...': 0.84; 'once-off': 0.84; 'piping': 0.84; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=iGz6f5KfY+/qH9kXk1PbiUsYof9YeHy6DhePhaLq2GE=; b=vIlhTVIeu8akan1IQiat7lg37+CXVVAD9GQuSjVOx8CqTVCtNBu3jiMaLIWxpnBHFy BkKQNlXfNzZcelBvUus6z8Oq5TRfsDx0CWToZolxHPyN+JaZgSeHiVby9RVOEf23ZLTa uQ/UOj9v6zAmJTZACwc4mbGsF+SwOR6sviQbP1428M0BSIR1pekKfgqf1yT5G89iS8GV QG25wKz0Q26+brsjJUpYqesik8nIIQdYwIkgpobfYEE14z9MT7KXb1fEhF4055WlFGgf gGhiVEn8iHMKBshlt2S/YUO4/5ARJnN02CzrVocTthJ3bJxeBpk43NOShSc/ana5dhc8 DEgA== MIME-Version: 1.0 X-Received: by 10.50.73.37 with SMTP id i5mr7186645igv.88.1369110960418; Mon, 20 May 2013 21:36:00 -0700 (PDT) In-Reply-To: <20130521011220.GA62488@cskk.homeip.net> References: <20130521011220.GA62488@cskk.homeip.net> Date: Tue, 21 May 2013 10:06:00 +0530 Subject: Re: How to run a python script twice randomly in a day? From: Avnesh Shakya To: Cameron Simpson Content-Type: multipart/alternative; boundary=089e013a0e0874366604dd32fc18 Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 324 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1369110969 news.xs4all.nl 15865 [2001:888:2000:d::a6]:54122 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:45641 --089e013a0e0874366604dd32fc18 Content-Type: text/plain; charset=ISO-8859-1 Thanks a lot. I got it. On Tue, May 21, 2013 at 6:42 AM, Cameron Simpson wrote: > On 20May2013 15:05, Avnesh Shakya 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 > > 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 > --089e013a0e0874366604dd32fc18 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
Thanks a lot. I got it.


On Tue, May 21, 2013 at 6:42 AM, Camer= on Simpson <cs@zip.com.au> wrote:
On 20May2013 15:05, Avnesh Shakya <avnesh.nitk@gmail.com> wrote:
| =A0 =A0 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:

=A0 #!/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-
| =A0 */2 * * * * bash /home/avin/cronJob/test.sh
| and saved it.

IIRC, this runs every two minutes. Good for testing, but not your ori= ginal 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:

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

and then your cron line can look like this:

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

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

=A0 #!/usr/bin/python

Make it executable:

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

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

=A0 ./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 =3D 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 t= wo 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:

=A0 #!/usr/bin/python
=A0 import random
=A0 a =3D random.randrange(0, 59)
=A0 cronline =3D '*/%d * * * * /home/avin/cronJob/test.sh' % = (a,)
=A0 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 valu= e.
=A0 So your call picks a number from 0..58, not 0..59.
=A0 Say randrange(0,60). Think "start, length".

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

- If you're picking random run times you want to schedule a once-off =A0 job for each to run at a particular times. Cron schedules repeating
=A0 jobs. =A0To run at a particular time you want an "at" job.
- You need to do one of two things in the pick-a-time script:
=A0 =A0 - pick a time, then sleep until that time and then directly
=A0 =A0 =A0 invoke the try.py script
=A0 or
=A0 =A0 - pick a time, then use the "at" command to schedule the = try.py
=A0 =A0 =A0 (or test.sh) script.

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

=A0 #!/usr/bin/python
=A0 import random
=A0 import subporcess
=A0 import time
=A0 # choose range time in the next 24 hours
=A0 when =3D random.randrange(0, 24 * 3600)
=A0 # sleep that many seconds
=A0 time.sleep(when)
=A0 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, comput= e
(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@zip.com.au><= br>
On a related topic, has anyone looked at doing a clean-room copy of C= SS
a la RC2 and RC4 a few years back? =A0I 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<= br> a lot for the status of their "trade secret" that we couldn't= actually
find anyone who didn't already know =A0it).
=A0 =A0 =A0 =A0 - Peter Gutmann <pgut001@cs.auckland.ac.nz>

--089e013a0e0874366604dd32fc18--