7 Replies - 1155 Views - Last Post: 16 June 2014 - 03:34 PM Rate Topic: -----

#1 Code-tastic   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 14-June 14

Executing a line at a specific time

Posted 14 June 2014 - 08:36 AM

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?
Is This A Good Question/Topic? 0
  • +

Replies To: Executing a line at a specific time

#2 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5612
  • View blog
  • Posts: 14,686
  • Joined: 18-April 07

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. :)
Was This Post Helpful? 1
  • +
  • -

#3 Code-tastic   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 14-June 14

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:

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?
Was This Post Helpful? 0
  • +
  • -

#4 Shadowys   User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 67
  • Joined: 16-May 14

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

Was This Post Helpful? 0
  • +
  • -

#5 Code-tastic   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 14-June 14

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

Was This Post Helpful? 0
  • +
  • -

#6 Shadowys   User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 67
  • Joined: 16-May 14

Re: Executing a line at a specific time

Posted 16 June 2014 - 03:00 PM

View PostCode-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.
Was This Post Helpful? 0
  • +
  • -

#7 andrewsw   User is offline

  • no more Mr Potato Head
  • member icon

Reputation: 6957
  • View blog
  • Posts: 28,696
  • Joined: 12-December 12

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.
Was This Post Helpful? 0
  • +
  • -

#8 andrewsw   User is offline

  • no more Mr Potato Head
  • member icon

Reputation: 6957
  • View blog
  • Posts: 28,696
  • Joined: 12-December 12

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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1