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


Groups > comp.lang.python > #9122 > unrolled thread

How can I make a program automatically run once per day?

Started byJohn Salerno <johnjsal@gmail.com>
First post2011-07-09 17:26 -0700
Last post2011-07-10 11:21 +0100
Articles 19 — 13 participants

Back to article view | Back to comp.lang.python


Contents

  How can I make a program automatically run once per day? John Salerno <johnjsal@gmail.com> - 2011-07-09 17:26 -0700
    Re: How can I make a program automatically run once per day? Ben Finney <ben+python@benfinney.id.au> - 2011-07-10 10:40 +1000
    Re: How can I make a program automatically run once per day? Andrew Berg <bahamutzero8825@gmail.com> - 2011-07-09 19:49 -0500
    Re: How can I make a program automatically run once per day? Alexander Kapps <alex.kapps@web.de> - 2011-07-10 03:00 +0200
      Re: How can I make a program automatically run once per day? John Salerno <johnjsal@gmail.com> - 2011-07-09 19:01 -0700
        Re: How can I make a program automatically run once per day? Rafael Durán Castañeda <rafadurancastaneda@gmail.com> - 2011-07-10 10:49 +0200
        Re: How can I make a program automatically run once per day? monkeys paw <monkey@joemoney.net> - 2011-07-14 13:00 -0400
          Re: How can I make a program automatically run once per day? Ian Kelly <ian.g.kelly@gmail.com> - 2011-07-14 11:13 -0600
          Re: How can I make a program automatically run once per day? baloan <baloand@googlemail.com> - 2011-07-27 01:41 -0700
        Re: How can I make a program automatically run once per day? John Salerno <johnjsal@gmail.com> - 2011-07-26 18:05 -0700
          Re: How can I make a program automatically run once per day? Ethan Furman <ethan@stoneleaf.us> - 2011-07-26 19:02 -0700
          Re: How can I make a program automatically run once per day? Andrew Berg <bahamutzero8825@gmail.com> - 2011-07-26 21:22 -0500
            Re: How can I make a program automatically run once per day? John Salerno <johnjsal@gmail.com> - 2011-07-26 21:09 -0700
              Re: How can I make a program automatically run once per day? Chris Angelico <rosuav@gmail.com> - 2011-07-27 16:55 +1000
              Re: How can I make a program automatically run once per day? Dave Angel <davea@ieee.org> - 2011-07-27 08:27 -0400
              Re: How can I make a program automatically run once per day? Chris Angelico <rosuav@gmail.com> - 2011-07-27 22:35 +1000
                Re: How can I make a program automatically run once per day? Billy Mays <81282ed9a88799d21e77957df2d84bd6514d9af6@myhashismyemail.com> - 2011-07-27 08:58 -0400
                  Re: How can I make a program automatically run once per day? John Salerno <johnjsal@gmail.com> - 2011-07-27 09:03 -0700
    Re: How can I make a program automatically run once per day? Paul Rudin <paul.nospam@rudin.co.uk> - 2011-07-10 11:21 +0100

#9122 — How can I make a program automatically run once per day?

FromJohn Salerno <johnjsal@gmail.com>
Date2011-07-09 17:26 -0700
SubjectHow can I make a program automatically run once per day?
Message-ID<c626a2fe-3379-41cb-88d8-4f7495d537e6@d1g2000yqm.googlegroups.com>
I have a script that does some stuff that I want to run every day for
maybe a week, or a month. So far I've been good about running it every
night, but is there some way (using Python, of course) that I can make
it automatically run at a set time each night?

[toc] | [next] | [standalone]


#9124

FromBen Finney <ben+python@benfinney.id.au>
Date2011-07-10 10:40 +1000
Message-ID<87iprbge8e.fsf@benfinney.id.au>
In reply to#9122
John Salerno <johnjsal@gmail.com> writes:

> is there some way (using Python, of course) that I can make it
> automatically run at a set time each night?

You need to use whatever facilities your operating system has for
scheduled events. That's unrelated to the language you use for
implementing the program.

On a Unix-like system (e.g. GNU+Linux), you could create a ‘cron’ job
entry.

-- 
 \      “The most common way people give up their power is by thinking |
  `\                               they don't have any.” —Alice Walker |
_o__)                                                                  |
Ben Finney

[toc] | [prev] | [next] | [standalone]


#9125

FromAndrew Berg <bahamutzero8825@gmail.com>
Date2011-07-09 19:49 -0500
Message-ID<mailman.809.1310258964.1164.python-list@python.org>
In reply to#9122
On 2011.07.09 07:26 PM, John Salerno wrote:
> I have a script that does some stuff that I want to run every day for
> maybe a week, or a month. So far I've been good about running it every
> night, but is there some way (using Python, of course) that I can make
> it automatically run at a set time each night?
I would use the OS to worry about scheduling (cron/Windows Task
Scheduler/whatever), but in Python, you could probably use a while True
loop, time.sleep() (to specify how often to check the time) and a
datetime.time or datetime.now object (e.g. datetime.now().hour).

[toc] | [prev] | [next] | [standalone]


#9126

FromAlexander Kapps <alex.kapps@web.de>
Date2011-07-10 03:00 +0200
Message-ID<mailman.810.1310259664.1164.python-list@python.org>
In reply to#9122
On 10.07.2011 02:26, John Salerno wrote:
> I have a script that does some stuff that I want to run every day for
> maybe a week, or a month. So far I've been good about running it every
> night, but is there some way (using Python, of course) that I can make
> it automatically run at a set time each night?

Use your operating system's facilities to run timed jobs.

Unix/Linux: Cron jobs
Windows: Scheduled Tasks
Mac: don't know, but probably Cron too

[toc] | [prev] | [next] | [standalone]


#9134

FromJohn Salerno <johnjsal@gmail.com>
Date2011-07-09 19:01 -0700
Message-ID<54735121-6a94-4dcb-9f6b-e4fca4aad652@u42g2000yqm.googlegroups.com>
In reply to#9126
Thanks everyone! I probably should have said something like "Python,
if possible and efficient, otherwise any other method" ! :)

I'll look into the Task Scheduler. Thanks again!

[toc] | [prev] | [next] | [standalone]


#9158

FromRafael Durán Castañeda <rafadurancastaneda@gmail.com>
Date2011-07-10 10:49 +0200
Message-ID<mailman.833.1310287808.1164.python-list@python.org>
In reply to#9134
On 10/07/11 04:01, John Salerno wrote:
> Thanks everyone! I probably should have said something like "Python,
> if possible and efficient, otherwise any other method" ! :)
>
> I'll look into the Task Scheduler. Thanks again!
You may use Celery 
http://docs.celeryproject.org/en/latest/userguide/periodic-tasks.html

[toc] | [prev] | [next] | [standalone]


#9477

Frommonkeys paw <monkey@joemoney.net>
Date2011-07-14 13:00 -0400
Message-ID<6uidnYUoaNs-vYLTnZ2dnUVZ_sidnZ2d@insightbb.com>
In reply to#9134
On 7/9/2011 10:01 PM, John Salerno wrote:
> Thanks everyone! I probably should have said something like "Python,
> if possible and efficient, otherwise any other method" ! :)
>
> I'll look into the Task Scheduler. Thanks again!

You could use the below code. time.sleep(# seconds in a day)
where i == 30 would run once a day for a month

import time
i=0
while (1):
	print 'hello'
	time.sleep(2)   # Change this to number of seconds in a day
	if (i == 3):    # make this 30 for a month
		break
	i = i + 1

[toc] | [prev] | [next] | [standalone]


#9478

FromIan Kelly <ian.g.kelly@gmail.com>
Date2011-07-14 11:13 -0600
Message-ID<mailman.1030.1310663668.1164.python-list@python.org>
In reply to#9477
On Thu, Jul 14, 2011 at 11:00 AM, monkeys paw <monkey@joemoney.net> wrote:
> You could use the below code. time.sleep(# seconds in a day)
> where i == 30 would run once a day for a month
>
> import time
> i=0
> while (1):
>        print 'hello'
>        time.sleep(2)   # Change this to number of seconds in a day
>        if (i == 3):    # make this 30 for a month
>                break
>        i = i + 1

If the system ever gets rebooted during that month, then you would
need to remember to manually restart the script.  Or if the effective
part of the script raises an exception, it could crash the whole
script without some defensive coding.  That's why it's better just to
use the system scheduler service.

[toc] | [prev] | [next] | [standalone]


#10377

Frombaloan <baloand@googlemail.com>
Date2011-07-27 01:41 -0700
Message-ID<8cf53b89-07bf-4a94-8dc2-e07014052c73@f39g2000prb.googlegroups.com>
In reply to#9477
On Jul 14, 7:00 pm, monkeys paw <mon...@joemoney.net> wrote:
> On 7/9/2011 10:01 PM, John Salerno wrote:
>
> > Thanks everyone! I probably should have said something like "Python,
> > if possible and efficient, otherwise any other method" ! :)
>
> > I'll look into the Task Scheduler. Thanks again!
>
> You could use the below code. time.sleep(# seconds in a day)
> where i == 30 would run once a day for a month
>
> import time
> i=0
> while (1):
>         print 'hello'
>         time.sleep(2)   # Change this to number of seconds in a day
>         if (i == 3):    # make this 30 for a month
>                 break
>         i = i + 1

This looks like a bad idea to me: to make it work throughout the year
you need to adjust for daylight savings start and end; this is where a
day does not have 24 hours - and, of course, daylight savings depends
on country settings of your host system...

Andreas
baloan@gmail.com

[toc] | [prev] | [next] | [standalone]


#10360

FromJohn Salerno <johnjsal@gmail.com>
Date2011-07-26 18:05 -0700
Message-ID<965707ba-5fe2-43cb-853d-f4041a3ba262@e40g2000yqn.googlegroups.com>
In reply to#9134
On Jul 9, 9:01 pm, John Salerno <johnj...@gmail.com> wrote:
> Thanks everyone! I probably should have said something like "Python,
> if possible and efficient, otherwise any other method" ! :)
>
> I'll look into the Task Scheduler. Thanks again!

Hmm, okay I'm finally trying Task Scheduler, but how do I set it to
run a Python script? It seems to not work, I suppose because it's
running the script but doesn't know how to find Python to run it
properly.

Thanks.

[toc] | [prev] | [next] | [standalone]


#10363

FromEthan Furman <ethan@stoneleaf.us>
Date2011-07-26 19:02 -0700
Message-ID<mailman.1521.1311732175.1164.python-list@python.org>
In reply to#10360
John Salerno wrote:
> On Jul 9, 9:01 pm, John Salerno <johnj...@gmail.com> wrote:
>> Thanks everyone! I probably should have said something like "Python,
>> if possible and efficient, otherwise any other method" ! :)
>>
>> I'll look into the Task Scheduler. Thanks again!
> 
> Hmm, okay I'm finally trying Task Scheduler, but how do I set it to
> run a Python script? It seems to not work, I suppose because it's
> running the script but doesn't know how to find Python to run it
> properly.

You need to have Python be the command, then add the script as the 
parameter (you may need to right-click and go to properties... or 
something like that ;) .

~Ethan~

[toc] | [prev] | [next] | [standalone]


#10365

FromAndrew Berg <bahamutzero8825@gmail.com>
Date2011-07-26 21:22 -0500
Message-ID<mailman.1522.1311733346.1164.python-list@python.org>
In reply to#10360
On 2011.07.26 08:05 PM, John Salerno wrote:
> Hmm, okay I'm finally trying Task Scheduler, but how do I set it to
> run a Python script? It seems to not work, I suppose because it's
> running the script but doesn't know how to find Python to run it
> properly.
Tell it to run the Python interpreter and pass the script as an argument.

-- 
CPython 3.2.1 | Windows NT 6.1.7601.17592 | Thunderbird 5.0
PGP/GPG Public Key ID: 0xF88E034060A78FCB

[toc] | [prev] | [next] | [standalone]


#10369

FromJohn Salerno <johnjsal@gmail.com>
Date2011-07-26 21:09 -0700
Message-ID<cca6b625-9f05-41c8-aaee-2c9a67403438@bl1g2000vbb.googlegroups.com>
In reply to#10365
On Jul 26, 9:22 pm, Andrew Berg <bahamutzero8...@gmail.com> wrote:
> On 2011.07.26 08:05 PM,JohnSalernowrote:> Hmm, okay I'm finally trying Task Scheduler, but how do I set it to
> > run a Python script? It seems to not work, I suppose because it's
> > running the script but doesn't know how to find Python to run it
> > properly.
>
> Tell it to run the Python interpreter and pass the script as an argument.
>
> --
> CPython 3.2.1 | Windows NT 6.1.7601.17592 | Thunderbird 5.0
> PGP/GPG Public Key ID: 0xF88E034060A78FCB

Thank you. I changed it as suggested so that now it runs C:
\Python32\python.exe extract_songs.py but it still isn't working. A
DOS prompt flashes real quick as it runs, but when I check the output
file that is supposed to be written to, nothing new has been added.
I'm not sure what the problem is now. I know the script itself works
because I just ran it manually and the output was fine.

[toc] | [prev] | [next] | [standalone]


#10373

FromChris Angelico <rosuav@gmail.com>
Date2011-07-27 16:55 +1000
Message-ID<mailman.1527.1311749715.1164.python-list@python.org>
In reply to#10369
On Wed, Jul 27, 2011 at 2:09 PM, John Salerno <johnjsal@gmail.com> wrote:
> Thank you. I changed it as suggested so that now it runs C:
> \Python32\python.exe extract_songs.py but it still isn't working.

Have you confirmed that the job's working directory is set correctly?
Naming the script without a path depends on the task being run from
the script's directory.

ChrisA

[toc] | [prev] | [next] | [standalone]


#10385

FromDave Angel <davea@ieee.org>
Date2011-07-27 08:27 -0400
Message-ID<mailman.1535.1311769675.1164.python-list@python.org>
In reply to#10369
On 01/-10/-28163 02:59 PM, John Salerno wrote:
> On Jul 26, 9:22 pm, Andrew Berg<bahamutzero8...@gmail.com>  wrote:
>> On 2011.07.26 08:05 PM,JohnSalernowrote:>  Hmm, okay I'm finally trying Task Scheduler, but how do I set it to
>>> run a Python script? It seems to not work, I suppose because it's
>>> running the script but doesn't know how to find Python to run it
>>> properly.
>> Tell it to run the Python interpreter and pass the script as an argument.
>>
>> --
>> CPython 3.2.1 | Windows NT 6.1.7601.17592 | Thunderbird 5.0
>> PGP/GPG Public Key ID: 0xF88E034060A78FCB
> Thank you. I changed it as suggested so that now it runs C:
> \Python32\python.exe extract_songs.py but it still isn't working. A
> DOS prompt flashes real quick as it runs, but when I check the output
> file that is supposed to be written to, nothing new has been added.
> I'm not sure what the problem is now. I know the script itself works
> because I just ran it manually and the output was fine.
>
As Chris pointed out, you probably aren't getting the script's directory 
right.  After all, how can the scheduler guess where you put it?  The 
obvious answer is to use a full path for the script's filename.  Another 
alternative is to fill in the current directory in the appropriate field 
of the scheduler's entry.

I find it useful to only add batch files to the scheduler.  Those batch 
files can do any setup and cleanup necessary.  In this case, the batch 
file might simply set the current directory to the location of the 
script. But it can also pause at the end, so you can read the console 
before it disappears.   Or it could create another file, so you could 
check the timestamp to figure out when it was triggered.

DaveA

[toc] | [prev] | [next] | [standalone]


#10386

FromChris Angelico <rosuav@gmail.com>
Date2011-07-27 22:35 +1000
Message-ID<mailman.1536.1311770120.1164.python-list@python.org>
In reply to#10369
On Wed, Jul 27, 2011 at 10:27 PM, Dave Angel <davea@ieee.org> wrote:
> As Chris pointed out, you probably aren't getting the script's directory
> right.  After all, how can the scheduler guess where you put it?  The
> obvious answer is to use a full path for the script's filename.  Another
> alternative is to fill in the current directory in the appropriate field of
> the scheduler's entry.

I would prefer setting the current directory, as that allows the
script to find any data files it needs, but either works.

> I find it useful to only add batch files to the scheduler.  Those batch
> files can do any setup and cleanup necessary.  In this case, the batch file
> might simply set the current directory to the location of the script.

And that is an excellent idea. Definitely recommended.

ChrisA

[toc] | [prev] | [next] | [standalone]


#10387

FromBilly Mays <81282ed9a88799d21e77957df2d84bd6514d9af6@myhashismyemail.com>
Date2011-07-27 08:58 -0400
Message-ID<j0p22f$4qs$1@speranza.aioe.org>
In reply to#10386
On 07/27/2011 08:35 AM, Chris Angelico wrote:
> On Wed, Jul 27, 2011 at 10:27 PM, Dave Angel<davea@ieee.org>  wrote:
>> As Chris pointed out, you probably aren't getting the script's directory
>> right.  After all, how can the scheduler guess where you put it?  The
>> obvious answer is to use a full path for the script's filename.  Another
>> alternative is to fill in the current directory in the appropriate field of
>> the scheduler's entry.
>
> I would prefer setting the current directory, as that allows the
> script to find any data files it needs, but either works.
>
>> I find it useful to only add batch files to the scheduler.  Those batch
>> files can do any setup and cleanup necessary.  In this case, the batch file
>> might simply set the current directory to the location of the script.
>
> And that is an excellent idea. Definitely recommended.
>
> ChrisA

If it hasn't been mentioned already:

import time

while True:
     t1 = time.time()

     #your code here

     t2 = time.time()
     time.sleep( 86400 - (t2 - t1) )



This doesn't take into account leap seconds, but it doesn't depend on a 
task scheduler.  It is also independent of the time your code takes to 
execute.

This is simpler, but it might drift slightly over time.

--
Bill

[toc] | [prev] | [next] | [standalone]


#10391

FromJohn Salerno <johnjsal@gmail.com>
Date2011-07-27 09:03 -0700
Message-ID<55290f47-b545-4a31-bc15-84a77274f6c2@z7g2000vbp.googlegroups.com>
In reply to#10387
On Jul 27, 7:58 am, Billy Mays
<81282ed9a88799d21e77957df2d84bd6514d9...@myhashismyemail.com> wrote:
> On 07/27/2011 08:35 AM, Chris Angelico wrote:
>
>
>
>
>
>
>
>
>
> > On Wed, Jul 27, 2011 at 10:27 PM, Dave Angel<da...@ieee.org>  wrote:
> >> As Chris pointed out, you probably aren't getting the script's directory
> >> right.  After all, how can the scheduler guess where you put it?  The
> >> obvious answer is to use a full path for the script's filename.  Another
> >> alternative is to fill in the current directory in the appropriate field of
> >> the scheduler's entry.
>
> > I would prefer setting the current directory, as that allows the
> > script to find any data files it needs, but either works.
>
> >> I find it useful to only add batch files to the scheduler.  Those batch
> >> files can do any setup and cleanup necessary.  In this case, the batch file
> >> might simply set the current directory to the location of the script.
>
> > And that is an excellent idea. Definitely recommended.
>
> > ChrisA
>
> If it hasn't been mentioned already:
>
> import time
>
> while True:
>      t1 = time.time()
>
>      #your code here
>
>      t2 = time.time()
>      time.sleep( 86400 - (t2 - t1) )
>
> This doesn't take into account leap seconds, but it doesn't depend on a
> task scheduler.  It is also independent of the time your code takes to
> execute.
>
> This is simpler, but it might drift slightly over time.
>
> --
> Bill

Well, I specified the full path name but it still doesn't seem to
work. A DOS prompt flashes for about a second that says "taskeng.exe"
in the title bar, but the script itself still isn't being run.

I don't know about batch files but I'll read up on them and see if
that will be a better solution.

[toc] | [prev] | [next] | [standalone]


#9161

FromPaul Rudin <paul.nospam@rudin.co.uk>
Date2011-07-10 11:21 +0100
Message-ID<87r55y4erv.fsf@no-fixed-abode.cable.virginmedia.net>
In reply to#9122
John Salerno <johnjsal@gmail.com> writes:

> I have a script that does some stuff that I want to run every day for
> maybe a week, or a month. So far I've been good about running it every
> night, but is there some way (using Python, of course) that I can make
> it automatically run at a set time each night?

Well - you can make a long lived python process that puts itself to
sleep for 24 hours and then wakes up and does stuff, but the normal
approach to this kind of thing is to use cron. On windows there's also
some kind of scheduler.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web