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
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
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
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)