Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #45641

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

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 <avnesh.nitk@gmail.com>
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&quot;': 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&gt;': 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 <CAEA17E+UpccfENQJEZv_Pp+5P00PUnhHvdztZ0ofMJZF67fdVA@mail.gmail.com> <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 <avnesh.nitk@gmail.com>
To Cameron Simpson <cs@zip.com.au>
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 <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1901.1369110969.3114.python-list@python.org> (permalink)
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

Show key headers only | View raw


[Multipart message — attachments visible in raw view] - view raw

Thanks a lot. I got it.


On Tue, May 21, 2013 at 6:42 AM, Cameron Simpson <cs@zip.com.au> wrote:

> On 20May2013 15:05, Avnesh Shakya <avnesh.nitk@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@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@cs.auckland.ac.nz>
>

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: How to run a python script twice randomly in a day? Avnesh Shakya <avnesh.nitk@gmail.com> - 2013-05-21 10:06 +0530

csiph-web