Restart Python with threading or crontab - python

I have written a function that crawls and clusters news articles. I want this function to restart every 10 minutes, aiming to get the newest articles. I wrote a Python script that this with the help of threading module:
import threading
def run():
do_it()
if not END:
threading.Timer(600.0, run).start()
END = False
threading.Timer(600.0, do_it).start()
It works fine with Python IDLE. Now I see in different forums that "crontab" is mentioned for this purpose. Since I plan to host this application on the web and since I have no prior experience with web development/hosting, what would you suggest to me: to continue with the previous code or to try to do it with the "crontab" function? I am working with the Django framework.

Using Django doesn't really work in this context. if you want to use a cron script then you will want it running independently of your webserver (you don't want your webserver ending after you finish the script). Alternatively you could start up a GET or POST request to an already running Django instance by this method.
First type crontab -e at a prompt in your unix based os or mac. (since you mentioned crontab I'll assume your not using windows)
This will allow you to edit a file for the user you are logged in as. This is the line you will want to type to get a do_it.py file to run every ten min.
*/10 * * * * do_it.py
For more information at your prompt type man 5 crontab. Other modifications you could make are prefixing the word sudo to the command which would allow you to change root's crontab.

Related

using WDT to monitor if my python program crashes

Background
I'm struggling to find a example of WDT in the way I want to use it. Wondering if I misunderstanding its use.
my python writing is pure hobby, honestly Classes intimidate me.
in short my program reads a number of sensors connected to a raspberry pi and writes the data to a cloud hosted object database.
i have an intermittent error that while I try to figure out I want to implement a based watchdog timer.
This is what I'd like to implement so in the very least I continue to collect and store data.
I've read about the builtin watchdog timer the raspberry pi has built in here: https://diode.io/raspberry%20pi/running-forever-with-the-raspberry-pi-hardware-watchdog-20202/
The problem I want the raspberry pi to reboot if my program hangs, but when that happens the OS is still fine, so the solution in the link above is not effective.
What I'd like to implement:
set the builtin watchdog timer to reboot the raspberry pi after 200 seconds without restarting (patting?) the timer. I think the instructions for this are in the link above.
Within my python script, after I iterate through each sensor, restart (or pat?) the watchdog timer and if 200 seconds elapse between pattings (meaning my program hangs) then RPi reboots.
is this possible?
can someone help me with some simple code? I was hoping to keep this simple and avoid classes and/or threads...
thank you in advance
The WDT is probably not the right solution for the problem you are describing. Based on your description, it sounds like what you have is a program that is intended to run periodically (either on a fixed schedule or in response to some event), and that program currently has a bug that is causing it to hang intermittently and never complete it's task or terminate.
The first and best way to solve that, I'm sure you can guess, is to fix the bug. But your thinking is not unreasonable either, and doing what you describe is very common. There are many valid approaches that will do what you want without the complexity of trying to deal with a hardware timer. One of the easiest is probably to just wrap the program in a shell script and use the timeout command to limit how long it is allowed to execute before it is terminated.
Assuming the script is located at /home/user/my_script.py, and you want to run it every 10 minutes, allowing it 2 minutes before it is killed, this would work:
create a wrapper shell script:
#!/usr/bin/env bash
if ! timeout 120s python /home/user/my_script.py; then
msg="$(date) - script exceeded timeout and was killed"
else
msg="$(date) - script ran successfully"
fi
echo "${msg}" >> /home/user/my_script.log
put the script in a file, say at /home/user/wrapper_script.sh and run chmod 755 /home/user/wrapper_script.sh to make it executable.
schedule the script to run every 10 minutes using cron. At a shell, use crontab -e to edit the users crontab file and add a new line like this:
*/10 * * * * /home/user/wrapper_script.sh
now, every 10 minutes the wrapper will start automatically, and it will kick off the python script. It will wait 2 minutes for the script to stop normally, after which it will reach the timeout and terminate it.
Note: depending on how your python program is written, you might have to use some other options to the timeout command to specify which signal it should use to stop the program. If it is a very basic python script, it should be fine using the default.
Edit: based on the comments, you might be able to just change the command you're using to this:
xterm -T "HMS" -geometry 100x70+10+35 -hold -e sudo timeout 120s /usr/bin/python3 /home/pi/h$
Doing that won't actually schedule the script to run at any fixed interval, so it assumes you already have something in place to handle that. All this will do is make srue that the script is restricted to 120 seconds of run time before it is killed.

How to run a python script after an x amount of time it has just finished?

I want to be able to run my python script which scans for something related to cryptocurrencies 1 minute after the same script has just been complete. Is this possible? Or could I maybe set a limit before the loop repeats itself or something?
The code I have isn't something I am willing to share due to its sensitive nature. It is a trading bot.
You have a few solutions:
Use a cron job if you are on a unix like platform (you can use this for the syntax, or man cron in the terminal to learn more about it)
Create a long running python script that sleeps for one minute before executing your logic again. Something like this:
import time
while True:
# execute code here
time.sleep(60)
If you are running it on Windows platform ,
You can create a batch file to run your script in cmd using the following command:
start "" "path_to_python.exe" "path_to_python_script"
then create a task in windows task scheduler .
You can refer
https://dev.to/tharindadilshan/running-a-python-script-every-x-minutes-in-windows-10-3nm9 . It might helps.

Run Python At Specific Time

I am working on a Python program. It needs to run every 15 minutes. It currently waits 870 seconds (14.5 mins) before running again, but as the time it takes to complete the action varies, sometimes it runs before it has been 15 minutes since it last run, sometimes after 15 minutes.
My code for this part currently looks like this:
print(colour.BOLD, colour.PURPLE, "Finished", colour.END)
print(colour.BOLD, colour.BLUE, 'WAITING 15 MINUTES (900 SECONDS)', colour.END)
time.sleep(870)
Is there a way I can get it to run at xx:15, xx:30, xx:45, xx:00 where xx is every hour from 00 to 23?
Sorry if I'm being confusing here. Thanks for any help in advance.
I would use the schedule module: https://pypi.org/project/schedule/
you would run:
schedule.every().minute.at(":00").do(job)
schedule.every().minute.at(":15").do(job)
schedule.every().minute.at(":30").do(job)
schedule.every().minute.at(":45").do(job)
Use your OS tools to achieve similar results.
They are very reliable and, in case of your script failure, it will run anyway next time.
Linux
Use crontab.
How to set it will slightly change depending on your Distribution.
As a general idea:
sudo crontab -e
Inside the crontab write (be sure to customize the python executable and script path):
*/15 * * * * /usr/bin/python /path/to/your/script.py
This will make sure that your script is executed every 15 minutes.
Windows
How to schedule a task on Windows is more dependent on the Windows version you are using and it is a very visual task.
Googling "How to schedule a task in Windows" will return way better / more specific / updated results than the one I could clumsily explain here.
Here's a nice one I have found for you.
Mac
Read the amazing answer by Meki here on StackOverflow.
Having a script that does a thing at discrete intervals taking control of its own destiny like that makes me squirrely. I would use an external scheduling framework to run this job at discrete intervals. In Linux, this can be done with cronjobs; in Windows, it can be done with Task Scheduler.
Linux:
In terminal, type
crontab -e
to edit the cron schedule for the current user context. Docs on editing cron can be found all over the internet - here's one: https://www.raspberrypi.org/documentation/linux/usage/cron.md
Windows:
You can schedule a python script to run on that schedule in Windows Task Scheduler. here's a link walking through that: https://www.esri.com/arcgis-blog/products/product/analytics/scheduling-a-python-script-or-model-to-run-at-a-prescribed-time/
be certain to utilize the "if the task is already running" and "run task as soon as possible after a scheduled start is missed" options if you do this method to control appropriate behavior:

Make Python script run automatically + SQL database

I have done many research on the forum but I didn't find a clear answer to my question.
I have created a python code which extracts data from a website and put it in a SQL database (with SQLITE).
Now, I would like to make it run 24 hours a day on a server so that it collects data at each moment.
Although I know how to code, I don't have a broad knowledge of IT in general.
Would you know by chance how to solve my problem ?
Edit: I was more thinking about putting my scripts on a cloud server to make it run instead of running it directly from my pc.
Thank you very much.
Lcs
I can think of two options:
1) You use a simple "while true" and "wait" in python, so the script is running constantly.
import time
while True:
# Code
time.sleep(5) # goes to sleep for 5 seconds
You can also do this in shell script (infinite loop+sleep) and then run the program when you want.
2) You can use cronjobs to run the pytho script. You basically have the script the will scrap the website and it will be launched every X seconds/minutes/hours. You can read more about how to use cronjobs in Google
Depends on what system you are using (Unix or windows) you can use either:
1. Windows Task Scheduler for Windows
2. Crontab for Unix

Allowing user to configure cron

I have this bash script on the server that runs every hour, via cron. I was perfectly happy, but now the user wants to be able to configure the frequency through the web interface.
I don't feel comfortable manipulating the cron configuration programmatically, but I'm not sure if the other options are any better.
The way I see it, I can either:
Schedule a script to run once a minute and check if it should really run "now"
Forgo cron altogether and use a deamon that is its own scheduler. This probably means rewriting the script in python
...or suck it up and manipulate the cron configuration from the web interface (written in python BTW)
What should I do?
EDIT: to clarify, the main reason I'm apprehensive about manipulating cron is because it's basically text manipulation with no validation, and if I mess it up, none of my other cron jobs will run.
Here's what I ended up doing:
Taking stefanw's advice, I added the following line at the top of my bash script:
if [ ! `cat /home/username/settings/run.freq` = $1 ]; then
exit 0
fi
I set up the following cron jobs:
0 */2 * * * /home/username/scripts/publish.sh 2_hours
#hourly /home/username/scripts/publish.sh 1_hour
*/30 * * * * /home/username/scripts/publish.sh 30_minutes
*/10 * * * * /home/username/scripts/publish.sh 10_minutes
From the web interface, I let the user choose between those four options, and based on what the user chose, I write the string 2_hours/1_hour/30_minutes/10_minutes into the file at /home/username/settings/run.freq.
I don't love it, but it seems like the best alternative.
Give your users some reasonable choices like every minute, every 5 minutes, every half an hour, ... and translate these values to a cron job string. This is user friendly and forbids users to tamper directly with the cron job string.
You could use a python scheduler library that does most of the work already:
pycron
scheduler-py
What about Webmin? I have never used it myself but it seems you could configure the cron module and give permissions to the user who wants to configure the job.
Well something I use is a main script started every minute by cron. this script check touched files. If the files are there the main cron script start a function/subscript. You just have to touch a defined file and "rm -f"ed it when done. It has the side benefits to be more concurrent proof if you want other way to start jobs. Then you can use your favourite web programming language to handle your users scheduling ...
The main script looks like :
[...]
if [ -e "${tag_archive_log_files}" ]; then
archive_log_files ${params}
rm -f ${tag_archive_files}
fi
if [ -e "${tag_purge_log_files}" ]; then
purge_log_files ${params}
rm -f ${tag_purge_log_files}
fi
[...]
I found a module that can manipulate the cron info for me. It's called python-crontab, and it's available with easy_install. From the source:
Example Use:
from crontab import CronTab
tab = CronTab()
cron = tab.new(command='/usr/bin/echo')
cron.minute().during(5,50).every(5)
cron.hour().every(4)
cron2 = tab.new(command='/foo/bar',comment='SomeID')
cron2.every_reboot()
list = tab.find('bar')
cron3 = list[0]
cron3.clear()
cron3.minute().every(1)
print unicode(tab.render())
for cron4 in tab.find('echo'):
print cron4
for cron5 in tab:
print cron5
tab.remove_all('echo')
t.write()
(I kept googling for "cron" and couldn't find anything. The keyword I was missing was "crontab").
I'm now deciding if I'll use this or just replace cron entirely with a python based scheduler.

Categories

Resources