Time condition in python - python

I have a python script monitor.py that analyzes text log. The log get generated by 'logging' module inside each python script from Scheduled Job and all results get logged to e.g. C:\log.txt file. I scheduled to run monitor.py script every hour. I don't want this c:\log.txt get growing and accumulate hence I think it would be a good idea to delete it sometime after midnight. Note: I don't have other scheduled jobs at night hence it will not have impact.
I want to check the current time, if the time is between 12:00 AM and 1:00 AM i.e. between midnight and 1 AM I will delete C:\log.txt and immidiately generate a new c:\log.txt file. I noticed that Scheduled Job on windows starts not exactly time it was scheduled but a few seconds before hence my prototype would be:
1. check if current time is between 23:59 PM and 1:00 AM
2. in case 1. is 'true' -> delete c:\log.txt and create a new c:\log.txt
My only problem is that I don't know how could create condition like:
1:00 AM < current time > 23:59 PM
Could someone help me on it?
Thanks

I don't want this c:\log.txt get growing
You can use https://docs.python.org/2.7/library/logging.handlers.html#rotatingfilehandler or https://docs.python.org/2.7/library/logging.handlers.html#timedrotatingfilehandler to limit a logfile size.

Related

Time of system startup in python

I'm working on a Windows application that will show time when my system was started. I tried to do something with WMI, I took SystemUpTime, but it gave me time since last startup. I am looking for first startup per day, so for example if user will turn on computer at 7:00 and later will do restart at 9:00 it should still show 7:00. Is there any library which can be helpful?
I'm not aware of a library that would do this.
One option would be to:
Make your program run on startup.
Save the boot time each time your program runs
Display the earliest boot time for the current day.
Save the boot times in sqlite3, its built in to Python these days.

Terminate thread and/or scheduler

I'm more or less a Python beginner, so I might be using the wrong tools for the job. Or maybe I'm using the correct tools but not correctly.
Here's what I'm trying to do.
def stand_by(self, evt_time, number):
"""Waiting for scheduled time
Starts a scheduler in a separate thread that will run in the background
until a specific time is reached, usually a time in a flight's
schedule. Once that specific time is reached, the program will regularly
check if the state of the flight has changed as expected.
Args:
evt_time (:obj:`datetime`): The scheduled time of the status change.
Schedulers need Unix timestamps, so this has to be converted
inside the function.
number (str): Flight number of the flight that should be observed.
This function itself does not need it but it has to be passed on
as an argument.
"""
# evt_time is given in UTC time but needs to be checked against
# system time
scheduler = sched.scheduler(time.time, time.sleep)
tz = pytz.timezone(player.tz)
# Turns the event time from datetime UTC into datetime player actual
evt_time = pytz.utc.localize(evt_time).astimezone(tz)
# Turns the datetime object into a UNIX time stamp
evt_time = evt_time.timestamp()
threading.Thread(target=lambda: scheduler.enterabs(
evt_time, 2, check_dep, [number])).start()
scheduler.run()
The user selects a flight they want to "board". Let's say it's 1 PM and the flight is scheduled to leave at 2 PM. The program will start a scheduler that later starts a "listener" at 2 PM to see if the flight has departed. To enable the user to continue using the program until 2 PM, the scheduler is running in a separate thread. I'm trying to figure out how I can terminate that thread and/or scheduler if the user decides to choose a different "flight" before 2 PM. I can't seem to find an elegant way of terminating threads.
I have found threading.timer, which can be cancelled, but that takes a time interval, not a time object...
I'm not married at all to this function. What I need is a way to run another function at a fixed time so that the program remains usable and that can be terminated if the user changes their mind. Thanks a lot for ideas!

Python scheduled task to run every 14 days

I am currently working on a program that needs to run every 14 days. I have looked into Schedule which works fine, but I have a few doubts about how to go about this.
I will create a service which will handle the execution of the python program itself on a CentOS 7 system.
The issue here is that every 14 days I will run a function that generates a lot of email addresses and send them to a support entity. I am afraid that if something unintended happens, and the program restart - the support entity will get spammed with emails outside the time frame in which they should receive emails.
As far as I can tell, Schedule does not have any way of determining if the program has restarted, and therefore a reboot of either the system or the service will cause this behaviour.
Would it be a correct solution to write a date to a text file after each completed function run, and then check that text file once a day to determine whether the function should run or not? This method would survive a service and/or system reboot, but is it a "correct" way of doing it?
****UPDATE**** Having the cronjob run on specific days of the month (for example 1st and 15th.) is not sufficient. This could cause gaps in the data which the program processes. The script makes a call which pulls data from 14 days back, and this is the maximum number of days supported by the script (licensing and stuff, can't be changed so not that important except that it is a limitation). So it need to run on lets say odd or even week numbers (to get 14 days).
Any ideas on how to accomplish this given this new information?.
You should look into the use of cron (or google it yourself if you dont like the link).
I suggest creating a simple Python script that is called by cron every 14 days. The crontab entry could look like the following:
# this will run at 00:01 on the 15th and 30th of every month
1 0 */15 * * /path/to/python/script.py
# this will run at 00:01 on the 1st and 15th of every month
1 0 1,15 * * /path/to/python/script.py
You still could make your script write some sort of result (with maybe a timestamp) to a file, so that you could easily check that file to see if it ran correctly (or log some error info).
# this will run at 00:01 on the 1st and 15th of every month
1 0 1,15 * * /path/to/python/script.py >> /path/to/logfile.log 2>&1
EDIT
You can also configure cron to run every Monday (or another day) if the 1st and 15th of every month are not sufficient. And the script could check a log file to see if it was run the previous Monday to assure it only executes your business logic every 2 weeks.
# this will run at 00:01 once a week on Mondays
1 0 * * 1 /path/to/python/script.py >> /path/to/logfile.log 2>&1

MYSQL changing value of column when certain time is reached

I currently have a job scheduler made in python that talks to a mysql database. The scheduler finds any job with the status 0 and run it.
I want add a new column which will be "start_time". So if i want to run a job at 10 o'clock mysql database will pick up on the start_time and when it reaches 10 o'clock it will change the status from 1 to 0.
Does anyone know if this is possible? and if it is how i go about setting this up?

Scheduling a method to run at specific times

I'm making an app in python to send texts via Twilio. I'm using flask and it's hosted on Google App Engine. I have a list of messages that need to be sent at a specific date and time, by calling my message function. What's a simple way to go about creating this? I'm relatively new to all this.
I tried apscheduler, but it only worked on my local and not on the app engine. I've read about cron jobs, but can't find anything about specific dates/times or how to pass args when the job runs.
As mention in the comments by Fabio, you could make a cron task to run every 10 min (or every minute). I would look into a folder for messages to send. If you would make a filename format in that folder to start with the date and time, you could do something like that :
folder content:
201707092205_<#message_id>
pseudo-code for sending the message:
intant_when_the_script_is_ran = datetime.now().strftime(format_to_the_minute)
for file in folder:
if intant_when_the_script_is_ran in file
with open(file, 'rw') as fh:
destination = fh.readline() #reading the fisrt line
message = fh.readlines() #reading the rest of the message
twilioapi.sendmessage(destination, message)
os.remove(file) #the remove could be done in another script to leave some traces
This is where Google app engine come in handy. You can use cron jobs from app engine. Create a cron.yaml file in your project.In this file you can make all kind of scheduling option every day to one day in a week in a particular time. The following is an example cron.yaml file
cron:
- description: "daily summary job"
url: /tasks/summary
schedule: every 24 hours
- description: "monday morning mailout"
url: /mail/weekly
schedule: every monday 09:00
timezone: Australia/NSW
- description: "new daily summary job"
url: /tasks/summary
schedule: every 24 hours
target: beta
Cron schedules are specified using a simple English-like format.
every 12 hours
every 5 minutes from 10:00 to 14:00
every day 00:00
every monday 09:00
2nd,third mon,wed,thu of march 17:00
1st monday of sep,oct,nov 17:00
1 of jan,april,july,oct 00:00
for more well-explained scheduling format please refer this documentation.
Another option is to use a taskqueue task where you specify the time that the task should be run using the eta option (estimated time of arrival).
The task will sit in the queue until its time of execution arrives, and then GAE will cause the task to be launched to do whatever processing you need such as sending a text message.
The tasks may not be executed at the precise time you specify but in my experience it is generally quite close. Certainly far more accurate than running a CRON job every 10 minutes.
This will also be much more efficient than using a CRON job because a CRON job will cause a request to your app every 10 minutes but the task will only execute when needed. If you have a low volume app this may help you stay within the free quota.

Categories

Resources