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

J

John Salerno

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?
 
A

Andrew Berg

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).
 
A

Alexander Kapps

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
 
J

John Salerno

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!
 
P

Paul Rudin

John Salerno said:
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.
 
I

Ian Kelly

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

John Salerno

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

Ethan Furman

John said:
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~
 
A

Andrew Berg

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

John Salerno

On 2011.07.26 08:05 PM,JohnSalernowrote:> Hmm, okay I'm finally trying Task Scheduler, but how do I set it to

Tell it to run the Python interpreter and pass the script as an argument.

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

Chris Angelico

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
 
B

baloan

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 aday
        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
(e-mail address removed)
 
D

Dave Angel

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
 
C

Chris Angelico

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
 
B

Billy Mays

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


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

John Salerno

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.

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top