How can I make my Python script run after 2 hours? - python

I wrote a program that will close the hard drive with BitLocker in Windows by using libraries datetime and subprocess but I cannot get the correct result.
Here is my code:
import subprocess
import datetime
now = datetime.datetime.today()
delta = datetime.timedelta(hours=2)
time_delta = now + delta
# print(time_delta)
# print(now)
try:
if now == time_delta:
close = subprocess.check_output(['manage-bde', '-lock', 'D:', '-ForceDismount'])
close.decode('utf-8').split('\n')
print('done')
except Exception:
pass

That is because you are checking the current time now = datetime.datetime.today(), then add 2 hours to it and immediately after check, if time_delta is equal to now.
Thus the if statement always results in false and the code in it does not get executed.
You need to put something in place to wait. Something like that.
import subprocess
import datetime
import time
now = datetime.datetime.today()
delta = datetime.timedelta(hours=2)
time_delta = now + delta
while now != time_delta:
time.sleep(1) # wait for a second
now = datetime.datetime.today()
# when now == time_delta we continue here:
try:
close = subprocess.check_output(['manage-bde', '-lock', 'D:', '-ForceDismount'])
close.decode('utf-8').split('\n')
print('done')
except Exception:
pass
This is not the best way to schedule a task, but it should be working.

Related

Strange failure while logging CPU-Temp with Python to a .csv-File (Rasbperri-Pi)

i Try to set up my Raspberry pi with a fan. It should log the Temperature every 5 minutes and write it to a .csv-file with a Python Script. If the Temperature is hotter than 52 Degrees Celsius, it shoud turn on a USB-Hub, and turn it of if it is below that value. For now, i created a little website, to see my logged data. And here is my question: as you can see in the screenshot, it tells me sometimes [HOT] and sometimes [OK] for the same Values? Also it is not sepperating the Data like a want it to seperate (over/under 52 Degrees Celsius).
Screenshot from my little website (This only displays my .csv-File)
My .py-code:
from gpiozero import CPUTemperature
from time import sleep, strftime, time
from datetime import datetime, timedelta
from threading import Timer
import matplotlib.pyplot as plt
v=datetime.today()
w = v.replace(day=v.day, hour=1, minute=0, second=0, microsecond=0) + timedelta(days=1)
delta_t=w-v
secs=delta_t.total_seconds()
cpu = CPUTemperature()
plt.ion()
x = []
y = []
def write_tempHot(temp):
with open("/var/www/html/cpuTemp.csv", "a") as log:
log.write("{0}{1}\n".format(strftime("%Y-%m-%d %H:%M:%S = [HOT] "),str(temp)))
def write_tempLow(temp):
with open("/var/www/html/cpuTemp.csv", "a") as log:
log.write("{0}{1}\n".format(strftime("%Y-%m-%d %H:%M:%S = [OK] "),str(temp)))
def clearTemp():
filename = "/var/www/html/cpuTemp.csv"
# opening the file with w+ mode truncates the file
f = open(filename, "w+")
f.close()
while True:
temp = cpu.temperature
if temp > 52:
temp = cpu.temperature
write_tempHot(temp)
plt.pause(300)
t = Timer(secs, clearTemp)
t.start()
else:
temp = cpu.temperature
write_tempLow(temp)
plt.pause(300)
t = Timer(secs, clearTemp)
t.start()
(Also dont mind for the clearTemp()-function, i want to clear the .csv-file once a day)
Does anybody have n idea, why the results are that kind of strange?
Greetings & Thanks a lot!
Your comparison is
if temp > 52
when it should be
if temp >= 52.0
because otherwise your temperature Hi will only match if it is 53C or above.
I'd also use time.sleep() instead of plt.pause().
For your logfile, you've got two functions to write to your logfile, I'd use one instead:
def write_log(temp):
fmt = """{when} = [{option}] {temp}"""
datefmt = """%Y-%m-%d %H:%M:%S"""
option = "OK"
with open("/var/www/html/cpuTemp.csv", "a") as log:
when = datetime.now().strftime(datefmt)
if temp >= 52.0:
option = "HOT"
log.write(fmt.format(when=when, option=option, temp=temp))
Finally, I do not understand why you want to truncate your logfile every 5 minutes.

Run a python script based on schedule time using croniter

I have a function to do some work. The code should repeat based on schedule. I used Croniter module. the schedule should be in cron type syntax. It works with every minutes. But it doesn't work with seconds. I added seconds at last in schedule but it says croniter syntax is not acceptable. How do I add seconds in syntax to make it work for seconds also?
Here is my code.
import yaml
from croniter import croniter
from datetime import datetime, timedelta
import time
def fun():
for i in items:
print (i)
def roundDownTime(dt=None, dateDelta=timedelta(minutes=1)):
roundTo = dateDelta.total_seconds()
if dt == None : dt = datetime.now()
seconds = (dt - dt.min).seconds
rounding = (seconds+roundTo/2) // roundTo * roundTo
return dt + timedelta(0,rounding-seconds,-dt.microsecond)
def getNextCronRunTime(schedule):
return croniter(schedule, datetime.now()).get_next(datetime)
def sleepTillTopOfNextMinute():
t = datetime.utcnow()
sleeptime = 60 - (t.second + t.microsecond/1000000.0)
time.sleep(sleeptime)
items =[2,4,6,7]
schedule = "*/1 * 7 2 * "
nextRunTime = getNextCronRunTime(schedule)
while True:
roundedDownTime = roundDownTime()
if (roundedDownTime == nextRunTime):
fun()
nextRunTime = getNextCronRunTime(schedule)
elif (roundedDownTime > nextRunTime):
# We missed an execution. Error. Re initialize.
nextRunTime = getNextCronRunTime(schedule)
sleepTillTopOfNextMinute()

Is there any issue with this Python script i am using it through sikuli? its not giving me the correct time

When I measure the time manually, it is less than the time that I got through this script:
import time import os
def getTimes():
try:
times = []
if(exists("1472205483589.png",60)):
click("1472192774056.png")
wait("1472040968178.png",10)
click("1472036591623.png")
click("1472036834091.png")
click("1472036868986.png")
if(exists("1472192829443.png",5)):
click("1472192829443.png")
u = time.time()
click("1472539655695.png")
wait("1472042542247.png",120)
v = time.time()
print("Open File to when views list appear  (sec) : " , int(v-u))
times.append(int(v-u))
u = time.time()
click("1472042542247.png")
wait("1472108424071.png",120)
mouseMove("1472108424071.png")
wait("1472108486171.png",120)
v = time.time()
print("Opening view (sec) : ",int(v-u))
times.append(int(v-u))
u = time.time()
click("1472109163884.png")
wait("1472042181291.png",120)
v = time.time()
print("Clicking element (sec) : ", float(v-u))
times.append(int(v-u))
return times
except FindFailed as ex:
print("Failed. Navigator might have stopped working")
if(exists("1472204045678.png",10)):
click("1472204045678.png")
return -1
file = open(r"C:\BSW\SikulixScripts\NavigatorAutoTesting\log.txt",'w') ret = getTimes() if (ret == -1):
file.write("-1")
exit() str = " ".join(str(x) for x in ret) file.write(str) file.close()
By using time.time(), you are actually returning a number of seconds--the difference between "the epoch" and now. (The epoch is the same as gmtime(0)). Instead, try using datetime.now() which will give you a datetime object. You can add and subtract datetime objects freely, resulting in a timedelta object as per the Python docs
u = datetime.now()
click("1472539655695.png")
wait("1472042542247.png",120)
v = datetime.now()
tdelta = v-u
seconds = tdelta.total_seconds() #if you want the number of seconds as a floating point number... (available in Python 2.7 and up)
times.append(seconds)
This should yield more accuracy for you.

rSimple python schedule two timed events

I am having trouble with the following scenario using python schedule module. Essentially I want to run a login event at time A, and then run the action at time B.
The code does not run as the intended behaviour describes and this is where I need help.
import sched
import datetime
today = datetime.datetime.today()
log = today.replace(hour=11, minute=59, second = 0)
action= today.replace(hour=12, minute=0, second = 0)
scheduler = sched.scheduler(datetime.datetime.today(), time.sleep)
def login_event(name):
print 'EVENT:', datetime.datetime.today(), name
def action_event(name):
print 'EVENT:' datetime.datetime.today(),name
print 'START:', time.time()
scheduler.enter(log, login_event, ('Login'))
scheduler.enter(action, login_event, ('Action'))
scheduler.run()
EDIT I have altered the code to the following but it still doesn't seem right in terms of how best to implement this behaviour.
import sched
import datetime
from datetime import timedelta
import datetime
import time
today = datetime.datetime.today()
log = datetime.datetime.now() + timedelta(minutes=1)# today.replace(hour=12, minute=46, second = 0)
action= log + timedelta(minutes=2)
scheduler = sched.scheduler(time.time, time.sleep)
print datetime.datetime.now
def login_event(name):
print 'Login:', datetime.datetime.now(), name
def action_event(name):
print 'Action:', datetime.datetime.now(), name
print 'Start:', datetime.datetime.now()
scheduler.enter(1, 1, login_event, ('first',))
scheduler.enter(60, 1, action_event, ('second',))
scheduler.run()
The following code hasn't been tested but should be work.
I've put your original code into comment so you can see where you got wrong.
You will probably need to refer the doc: https://docs.python.org/2/library/sched.html
import sched, time
import datetime
today = datetime.datetime.today()
log = today.replace(hour=11, minute=59, second = 0)
action= today.replace(hour=12, minute=0, second = 0)
#scheduler = sched.scheduler(datetime.datetime.today(), time.sleep)
#The first argument of sched.scheduler should be a function that return a number.
scheduler = sched.scheduler(time.time, time.sleep)
def login_event(name):
print 'EVENT:', datetime.datetime.today(), name
def action_event(name):
print 'EVENT:', datetime.datetime.today(),name
print 'START:', time.time()
scheduler.enter is used for relative delay. The correct function to use is scheduler.enterabs
You will need a function to convert datetime to POSIX timestamp.
This can be tricky in python 2.x due to timezone issue.
Refer to this question: Convert datetime to Unix timestamp and convert it back in python
Also, the function takes 4 arguments.
#scheduler.enter(log, login_event, ('Login'))
#scheduler.enter(action, login_event, ('Action'))
scheduler.enterabs(timestamp(log), 0, login_event, ('Login'))
scheduler.enterabs(timestamp(action), 0, action_event, ('Action'))
scheduler.run()
https://github.com/dbader/schedule
By following the pattern linked above I was able to create the desired behaviour using a slightly different schedule module
import schedule
import time
def job():
print("I'm working on job one...")
def job2():
print("I'm working on job two..")
schedule.every().day.at("10:30").do(job)
schedule.every().day.at("10:35").do(job2)
while True:
schedule.run_pending()
time.sleep(1)

Module object not callable in sleep if conditional ( python )

I have imported the following modules at the top of my python script:
import os
import sys
import time
import datetime
import random
import pprint
from random import randint
from time import sleep
from datetime import datetime, timedelta
But, I am still getting a module object error for this part of my code:
def check(low,high):
with open('done.txt', 'r+') as done:
ts = time.time()
sttime = datetime.fromtimestamp(ts).strftime('%Y%m%d_%H:%M:%S - ')
done_completed = open('done_completed.txt', "a")
for line in done:
now = datetime.now()
now_time = now.time()
if now_time >= time(23,30) and now_time <= time(06,30):
print "sleeping"
sleep(5000)
else:
done_id = line.strip()[20:]
then = datetime.strptime(line.strip()[:17], '%Y%m%d_%H:%M:%S')
(There's another if elif elif under all that but I figured it isn't relevant to the error)
(Yes, I am a python beginner)
The error is:
File "/home/joe/Desktop/follomatic/follomatic.py", line 85, in check
if now_time >= time(23,30) and now_time <= time(06,30):
TypeError: 'module' object is not callable
Does anyone see what is wrong? Have I not specified to call the module in the right way?
Thanks :)
Python's datetime module is a bit of a rat's nest. :) And it can get even more confusing if you use from imports.
This code should clarify things a bit. Note that this is not a great way to do things. It's generally better to work with datetime.datetime objects rather than datetime.time objects, otherwise things get messy if you need to handle time intervals that include midnight (or multiple days). But I wrote this code to roughly correspond with the code in your question, and I wanted to make it easy to test at any time of day.
#!/usr/bin/env python
''' datetime.time manipulation demo
From http://stackoverflow.com/q/28605732/4014959
Written by PM 2Ring 2015.02.19
'''
from time import sleep
import datetime
now = datetime.datetime.now()
now_time = now.time()
#A datetime.time example
t1 = datetime.time(5, 30)
print 't1 is', t1
now = datetime.datetime.now()
now_time = now.time()
print 'now is %s, now_time is %s' % (now, now_time)
begin_datetime = now + datetime.timedelta(seconds=5)
end_datetime = now + datetime.timedelta(seconds=10)
begin_time = begin_datetime.time()
end_time = end_datetime.time()
print 'begin', begin_time
print 'end', end_time
for i in xrange(15):
now_time = datetime.datetime.now().time()
print now_time, begin_time <= now_time <= end_time
sleep(1)
typical output
t1 is 05:30:00
now is 2015-02-19 23:44:39.152786, now_time is 23:44:39.152786
begin 23:44:44.152786
end 23:44:49.152786
23:44:39.152905 False
23:44:40.153999 False
23:44:41.155191 False
23:44:42.156398 False
23:44:43.156614 False
23:44:44.157810 True
23:44:45.159028 True
23:44:46.160231 True
23:44:47.161444 True
23:44:48.162660 True
23:44:49.163869 False
23:44:50.165076 False
23:44:51.166650 False
23:44:52.167842 False
23:44:53.169053 False

Categories

Resources