-2

Following up on,

With python: intervals at x:00 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

Community
  • 1
  • 1
Merlin
  • 24,552
  • 41
  • 131
  • 206

2 Answers2

3

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
  • 110,290
  • 27
  • 149
  • 241
  • This is as portable as it gets, considering cron isn't an option for the OP. – Sean Sep 21 '10 at 23:01
  • 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. – Merlin Sep 22 '10 at 15:57
  • with "(hour<17 and minute<1)", I can figure out...8:30 or 7:59 thanks again...... – Merlin Sep 22 '10 at 16:38
0

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)

Yuda Prawira
  • 12,075
  • 10
  • 46
  • 54