views:

103

answers:

3

Following up on,

http://stackoverflow.com/questions/3553340/with-python-intervals-at-x00-repeat

Using threading, How can I get a script to run starting at 8:00 am stop running at 5:00 pm

The solution should be coded within python, and be portable

tiA

+2  A: 

use cron. If on windows use windows task scheduler.

nosklo
solution not portable....thanks
how's that not portable? Cron works on all platforms.
nosklo
+2  A: 

The time module has a function called asctime, which might be useful for you:

>>> from time import asctime
>>> asctime()
'Tue Sep 21 17:49:42 2010'

So, you could incorporate something like the following into your code:

sysTime = asctime()
timestamp = systime.split()[3]
separator = timestamp[2]
hour = timestamp.split(separator)[0]
while hour < 8:
    # just wait
    sysTime = asctime()
    timestamp = systime.split()[3]
    separator = timestamp[2]
    hour = timestamp.split(separator)[0]

# now, it's just become 8:00 AM
while hour < 17: # until 5:00 PM
    sysTime = asctime()
    timestamp = systime.split()[3]
    separator = timestamp[2]
    hour = timestamp.split(separator)[0]

    # start your thread to do whatever needs to be done

Start this script off once and let it keep running forever.

This is in response to @user428862's question asking if this can be run with "hour > 8 and hour <17". This is how the code would need to be adapted for that purpose:

while 1:
    sysTime = asctime()
    timestamp = systime.split()[3]
    separator = timestamp[2]
    hour = timestamp.split(separator)[0]
    minute = timestamp.split(separator)[1]

    if (hour > 8) and (hour<17 and minute<1):
        # start your thread to do whatever needs to be done

Also , it just occurs to me that I have been imploying string splitting and that returns strings, so hour should be int(timestamp.split(separator)[0]) and so forth

inspectorG4dget
This is as portable as it gets, considering cron isn't an option for the OP.
Sean
same kind of logic in VBA and Sql, thx. how often does the script run top to bottom. Would " hour > 8 and hour <17:" work? also, how would I modify for 8:30, if I need it.
@inspector thnx, for update...
with "(hour<17 and minute<1)", I can figure out...8:30 or 7:59 thanks again......
A: 

in the cron, and you need to run script starting at 8:00 am stop running at 5:00 pm use crontab -e command in linux . and add this line code for

* 8 * * * /YOUR/PATH/SCRIPT

and you stop it in 5 pm, in this example we will kill all python process in 5 pm

* 17 * * * killall -9 /usr/bin/python

and you can check crontab with crontab -l and crontab -r for reset to default (no any command will be executed)

Gunslinger_