I was wondering what would be the easiest way of executing a line at a specific time? For example, if I wanted to print a string at 3:15 PM what would be the easiest way of doing so?
Executing a line at a specific time
Page 1 of 17 Replies - 1155 Views - Last Post: 16 June 2014 - 03:34 PM
Replies To: Executing a line at a specific time
#2
Re: Executing a line at a specific time
Posted 14 June 2014 - 10:20 AM
You could use the sched.scheduler object from the standard library. One of its methods is enterabs() which is used to run commands at an exact time. Here is the Python page and then a page with some more examples...
https://docs.python....ml#module-sched - Scheduler (pick your version if working with 2.7.x)
http://pymotw.com/2/sched/ - More examples (check out the subtopic "Event priorities" for an example of exact time)
Hope this helps.
https://docs.python....ml#module-sched - Scheduler (pick your version if working with 2.7.x)
http://pymotw.com/2/sched/ - More examples (check out the subtopic "Event priorities" for an example of exact time)
Hope this helps.
#3
Re: Executing a line at a specific time
Posted 14 June 2014 - 11:03 AM
so I've attempted to try it out in the following way:
The examples may not have been as helpful as I previously thought. Any advice?
import sched, time
def cmd():
os.system('shutdown -s')
scheduler.enterabs('3:00',
cmd())
The examples may not have been as helpful as I previously thought. Any advice?
#4
Re: Executing a line at a specific time
Posted 16 June 2014 - 12:58 AM
well, you could do a
import datetime.datetime as dt
import subprocess as sub
schedtime="1551"
command=["shutdown","/s"]
if dt.now().strftime("%H%M") == schedtime: sub.call(command)
This post has been edited by andrewsw: 16 June 2014 - 11:07 AM
Reason for edit:: Removed unnecessary previous quote
#5
Re: Executing a line at a specific time
Posted 16 June 2014 - 11:05 AM
I'm using Python version 3 and I've attempted to follow that example however I get no response or error messages? Any suggestions?
from datetime import datetime
import subprocess as sub
schedtime=("14:52")
command=["shutdown","/s"]
if datetime.now().strftime("%H%M") == schedtime: sub.call(command)
This post has been edited by andrewsw: 16 June 2014 - 11:07 AM
Reason for edit:: Removed unnecessary previous quote, press Reply
#6
Re: Executing a line at a specific time
Posted 16 June 2014 - 03:00 PM
Code-tastic, on 16 June 2014 - 11:05 AM, said:
I'm using Python version 3 and I've attempted to follow that example however I get no response or error messages? Any suggestions?
from datetime import datetime
import subprocess as sub
schedtime=("14:52")
command=["shutdown","/s"]
if datetime.now().strftime("%H%M") == schedtime: sub.call(command)
...
%H%M returns the time in the form of HHMM not HH:MM, so you should use %H:%M for this, since the input has a semicolon.
#7
Re: Executing a line at a specific time
Posted 16 June 2014 - 03:12 PM
Please note that there is no need to quote the previous post every time, there is a Reply button further down the page. Quoting each time makes the topic longer and more difficult to follow.
#8
Re: Executing a line at a specific time
Posted 16 June 2014 - 03:34 PM
Looking at the posted code, there is nothing I can see that prevents the application from terminating. In the linked scheduler page there is an example that uses sleep to keep the application alive.
I also suggest that you start by simply printing something after a brief delay (as mentioned in your first post); then attempt to print at a specific time, then finally to shutdown. That is, get the easier version(s) to work first.
I also suggest that you start by simply printing something after a brief delay (as mentioned in your first post); then attempt to print at a specific time, then finally to shutdown. That is, get the easier version(s) to work first.
Page 1 of 1

New Topic/Question
Reply


MultiQuote



|