> 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
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
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
I'll look into the Task Scheduler. Thanks again!
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.
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.
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.
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~
--
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.
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
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
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
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.