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

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

1,227 views
Skip to first unread message

John Salerno

unread,
Jul 9, 2011, 8:26:50 PM7/9/11
to
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?

Ben Finney

unread,
Jul 9, 2011, 8:40:17 PM7/9/11
to
John Salerno <john...@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

Andrew Berg

unread,
Jul 9, 2011, 8:49:14 PM7/9/11
to comp.lang.python
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).

Alexander Kapps

unread,
Jul 9, 2011, 9:00:57 PM7/9/11
to pytho...@python.org

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

Cameron Simpson

unread,
Jul 9, 2011, 9:58:59 PM7/9/11
to Alexander Kapps, pytho...@python.org

Yep. Macs are UNIX, BSD derived.
--
Cameron Simpson <c...@zip.com.au> DoD#743
http://www.cskk.ezoshosting.com/cs/

USENET: Post to exotic, distant machines. Meet exciting, unusual people.
And flame them. - Dan Sorenson, z1...@exnet.iastate.edu, DoD #1066

John Salerno

unread,
Jul 9, 2011, 10:01:27 PM7/9/11
to
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!

Rafael Durán Castañeda

unread,
Jul 10, 2011, 4:49:44 AM7/10/11
to pytho...@python.org

Paul Rudin

unread,
Jul 10, 2011, 6:21:40 AM7/10/11
to
John Salerno <john...@gmail.com> writes:

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.

monkeys paw

unread,
Jul 14, 2011, 1:00:07 PM7/14/11
to

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


Ian Kelly

unread,
Jul 14, 2011, 1:13:57 PM7/14/11
to monkeys paw, pytho...@python.org
On Thu, Jul 14, 2011 at 11:00 AM, monkeys paw <mon...@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.

John Salerno

unread,
Jul 26, 2011, 9:05:09 PM7/26/11
to

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.

Ethan Furman

unread,
Jul 26, 2011, 10:02:12 PM7/26/11
to John Salerno, pytho...@python.org

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~

Andrew Berg

unread,
Jul 26, 2011, 10:22:18 PM7/26/11
to comp.lang.python
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

John Salerno

unread,
Jul 27, 2011, 12:09:53 AM7/27/11
to
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

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.

Chris Angelico

unread,
Jul 27, 2011, 2:55:05 AM7/27/11
to pytho...@python.org
On Wed, Jul 27, 2011 at 2:09 PM, John Salerno <john...@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

baloan

unread,
Jul 27, 2011, 4:41:25 AM7/27/11
to

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
bal...@gmail.com

Dave Angel

unread,
Jul 27, 2011, 8:27:29 AM7/27/11
to John Salerno, pytho...@python.org
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

Chris Angelico

unread,
Jul 27, 2011, 8:35:11 AM7/27/11
to pytho...@python.org
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

Billy Mays

unread,
Jul 27, 2011, 8:58:57 AM7/27/11
to

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

John Salerno

unread,
Jul 27, 2011, 12:03:59 PM7/27/11
to
On Jul 27, 7:58 am, Billy Mays

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.

0 new messages