Schedule alert for a time - python

From python I'd been triggering notifications like this:
def show_alert(message, title="Flashlight"):
"""Display a macOS notification."""
message = json.dumps(message)
title = json.dumps(title)
script = 'display notification {0} with title {1}'.format(message, title)
os.system("osascript -e {0}".format(pipes.quote(script)))
but now I want to be able to trigger these alerts some time in the future.
I had a method using time, time.sleep(60) would trigger an alert a minute in the future.
The problem with that is that if the script is ended, or the computer sleeps, I'm not sure how realiable it would be.
Is there a way I can use python (maybe with applescripts, or other macOS tools) to schedule a notification for some arbitrary time in the future?

Assuming you are on macOS, you can use crontab. It executes processes at a given time. For example, every 5 hours, or every monday at 10pm.
Also, you should take a look at terminal-notifier. Here you have an example.

Related

Python restart file on windows or linux

I currently have a big long file that queries a webpage to build a dictionary. I'd like this file to restart at 4am every day as the webpage will have updated with fresh info. What do I need to put inside my while True: loop?
Current status:
##Various Imports
##Selenium code to get details
##Dictionary Compile
while True:
now = datetime.datetime.now()
current_time = now.strftime("%H:%M:%S")
if current_time == ("04:00:00"):
##The Code to Restart the process goes here
else:
#Other Stuff happens with the dictionary
Building and testing on windows but will ultimately run on a Raspberry Pi
I would suggest you make the script simply get the information from the web page then do whatever it needs to do with your dictionary and end. Only one time. Then you can schedule this script to run at 4:00 AM every day with Windows Task Scheduler or with a Cron Job on linux. Here is a link on how to set up a cron job to run a python script.
If you want the functionality of a cron job in Python you could use the schedule library. It looks like it has support to keep the script running and then restart at 4AM. More can be read on this StackOverflow Question.

Scheduling some python tasks

I am trying to schedule some tasks using python...
Here is the whole project:
I have online classes on zoom which I want to automatically record. (I can't wake up on time). I have the invite link to the meeting. The time and date of the meeting are mentioned in it.
I have written the python script to extract the message. Let's call this script A.
I have also written a script to click on the link so that the zoom meeting opens. Let's call this script B.
I need to run the scripts in this order:
I manually run script A at let's say 12:00 AM in the night(morning). By that time, the teachers would have sent the invite link message.
Based on the information that was extracted, I want to automatically run script B and start the OBS recording. A way to end the recording after the meeting has ended would also be appreciated. (or I could just record for 1 hour).
I just need to find a way to automatically start recording from OBS screen recorder at the time mentioned in the message. (possibly in script B only)
How do I go about it?
You could use the windows task scheduler.
scriptA extracts the start time for the online task, sleeps until the correct time (using time.sleep()) and then uses subprocess.Popen to start script B at the appropriate time.
For example; simply starting script B, discarding any output (assuming it resides in the current working directory):
import subprocess
subprocess.Popen(
['python3', 'scriptB.py'],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL
)

How to schedule a script to run at a specified time only on weekdays on Windows 10?

I have tried to run a Python script via the Windows 10 task scheduler but the script will not run:
I have double-checked the path to python and to the script. What could be the problem with the task scheduler? Is there any other ways to schedule the script? I need it to run five days a week at a specified time through the day, every week.
You might be able to get additional relevant information from the events log. Open Event Viewer (right-click on Start). In the left panel select 'Applications and Service Log', then 'Microsoft', then 'Windows', then 'TaskScheduler', the 'Operational'. You might find that the associated middle panel is empty because this log is disabled. In this case, right-click on 'Operational' and enable it temporarily.
Now go to Task Scheduler. Select your task, then click on 'Run' from the right panel a few times, and return to Event Viewer. I hope you will see something useful in the bottom window like
Task Scheduler failed to start "\Play a video" task for user "DESKTOP-K76A078\Bill". Additional Data: Error Value: 2147942402.
Presumably the number won't be the same for you. However, you can google for error value nnnnnnnnnn and get various speculations about mistakes or error conditions that you could look for.
Best of luck!
In Windows 10, if you install the Windows 10 Anniversary Update, you'd be able to install the Linux on Windows subsystem. Then, just use cron (crontab) to define any recurring task.

Methods to schedule a task prior to runtime

What are the best methods to set a .py file to run at one specific time in the future? Ideally, its like to do everything within a single script.
Details: I often travel for business so I built a program to automatically check me in to my flights 24 hours prior to takeoff so I can board earlier. I currently am editing my script to input my confirmation number and then setting up cron jobs to run said script at the specified time. Is there a better way to do this?
Options I know of:
• current method
• put code in the script to delay until x time. Run the script immediately after booking the flight and it would stay open until the specified time, then check me in and close. This would prevent me from shutting down my computer, though, and my machine is prone to overheating.
Ideal method: input my confirmation number & flight date, run the script, have it set up whatever cron automatically, be done with it. I want to make sure whatever method I use doesn't include keeping a script open and running in the background.
cron is best for jobs that you want to repeat periodically. For one-time jobs, use at or batch.

How can I implement some sort of alarm clock functionality for a Python program running on raspbmc?

I'm turning my raspberry pi running on raspbmc (based on Debian) into an alarm clock that turns my tv on, displays the weather and plays a predifined song at, you guessed it, a specific time. I currently have most of the functionality implemented with Python e.g.: storing the path to a file for the song and filling in the wake-up time via a simple web interface.
I'm wondering what the best way (least intensive) is to run the wake-up routine at a specific time entered by me and, of course, change it when a new time is entered.
Hope you guys can help me out. Thanks!
Just set up a cron task and set up a python script to perform the actions you need when triggered by cron.
Try:
% crontab -e
add a line:
10 7 * * 1-5 /path/to/your/script
Save & exit.
This should run your script at 7:10 Monday-Friday

Categories

Resources