Scheduling Python Script Without Crontab - python

pls help me, how can i make my script to execute every day in 12:00 am without using crontab. I have tried to use Schedule library in Python and it works for single function
Example:
import schedule
from datetime import time,timedelta,datetime
import time as tm
from time import sleep
def mis_mes():
print('Hello')
schedule.every().second.do(mis_mes)
while True:
schedule.run_pending()
tm.sleep(1)
However i dont have functions every where in the code and this library feels like cant call whole file.
Any ideas of how can i automate my script using only Python?
Ideally if i could simply type something as 1 line of code, to execute code at 12:00 am like it is in crontab. But i would be glad to hear any possible solution
I am sorry if my question is stupid, I am just starting with Python and doing my project for University.
Probably 1 solution is to make defined function every where and then create scheduled function that will call other functions, but is there better solutions?
PS: Someone already asked what is wrong with Crontab, okay ill answer. My script is actually an REST API script for a mobile robot, that should make robot to go to the charging station and charge there at exact time. The script by itself should be able to work properly not only on my PC, but on other PCs as well, and the scheduling time should be easily modified by user.

Related

how to automated my selenium web scrapper?

I was able to write a script to scrape using selenium, right now I'm trying to automate it so it can work periodically on a server so I don't bother myself by running it from my local, I did a lot of googling but I got no clue of how I can do that, can anyone simplify things for me ..
In order to run a python script on a linux server periodically you can make a cronjob, since you already have a python script which most probably fetches or scrapes data and saves it in a file. You can make a cronjob and set the exact time it has to run, say for instance after every 2 hours you can do it using something like this,
crontab -e
this will open a editor in your terminal, at the bottom of the text just write timing and the command to be executed.
* * * * */path/to/your/code.py
from this link you can find out how to fill out the stars https://crontab.guru/#*_*_*_*_1
if you need anymore help with using cronjobs take a look at this https://www.geeksforgeeks.org/scheduling-python-scripts-on-linux/
You can simply redo your script on pythonanywhere and schedule it as a task and choose the frequency that you want the script to be executed. The current frequency options available include; script runs always, hourly or daily.
i dont know if it'll really work but
while True:
whole
code
here
time.sleep(period required)

Is there a python module to indicate if the .py is running?

I am new to python.
I created a simple Selenium bot that basically runs on a while loop.
However, it runs on an external machine that I do not have access at all times.
It runs into unexpected crashes sometimes, and it takes hours and sometimes days until I check it again only to find a crashed bot. With that said, I'd like to know if a monitoring module or program I can implement into my bot, so I can easily check if the program is running or not.
Some have suggested a WebSocket heartbeat, however, I do not know, after extensive research, how I would go about implementing such an intricate system. Thank you for taking the time to read my question.
Any answers appreciated!
You can just put a few lines of code at the appropriate point in your script (after the Selenium bot completes its work successfully) to write the current date and time into a file.
A second script can be scheduled just to read that file and ring an alarm bell (e.g. send you an email) if it is X hours since the timestamp was last updated.
Alternatively you could write a timestamp to a database table or use whatever other shared systems you have.

Call a function in a running program (and a code review)

Call a function in a running program
I am fairly new to programming and recently decided that I want to expand my Python knowledge and practice a bit. For that reason I decided that I create a little Weather Station with a Raspberry PI.
The program I am currently creating takes the output from a thermometer, parses it and writes it into a database. At the time the program is started every minute and after completing the aforementioned procedure, the program ends and all the instances get deleted (is that how you say it?).
I think that restarting it every minute wastes resources and time is getting lost so I want my program to run all the time and be accessible via command line commands (bash).
For example I have the class Thermometer:
class Thermometer():
def measure():
# do stuff
# return stuff
An instance of this class is created like this:
if __name__ == "__main__":
thermo = Thermometer
while True:
pass
Is it possible that I could call the function measure like this?:
sudo python3 < thermo.measure()
Or is there an other way to achieve this or am I doing a completely wrong approach? Also how could one describe this problem? I tried searching for "Python call function from outside" or "Python call function from bash" but I didn't find anything useful except Call a function from a running process
StackOverflow but it seems that this is the wrong Python version.
You can find my code here: github Jocomol/weatherstation
or if you don't trust the link go to github and search for "Jocomol/weatherstation".
Code review
As I already mentioned I am quite new to python and programming itself, so I make a lot of mistakes or writing useless code that could be resolved otherwise. So I am thankful for any feedback I can get on my code. If you guys could take a look at my code and point out places that aren't optimal or where I did something completely useless, I would be very happy.

Testing a loop that repeats itself every 10 seconds

I have a code that repeats itself every 10 seconds, but I can't test it for a long time because my powershell keeps on hanging and the code just stops for no particular reason (the code is running but it doesn't give out results). Is there a way to test the code, or safely running it without it being interrupted? I tried to search but it seems that a library like Unittest will just crash along with my code due to windowshell if I want to run it for lets say a day. Because it usually hangs just a few hours after I start testing manually.
The code is something like this:
import time
import requests
while True:
getting = requests.get(some_url)
result = getting. json()
posting = requests.post(another_url,headers,json=result)
time.sleep(10)
Thank you for your help.
So after testing and experimenting. It seems like a resource miss-management by windows when I use PowerShell, cmd.exe or the default Python IDE. Thus in case someone wants to test their code for a prolonged period of time, it is recommended to use PyCharm as it has been running for more than a day for me. This should mean that PyCharm has better management in this specific area.
For more details check the comments under the question itself.

python: permanent execution program date dependant

I wrote a script.py collecting data from the web from monday to friday. The script is usually executed from another script in the main function. I want it to close on friday and open monday automatically, and run from monday to friday.
At the moment I am obliged to run it manually every monday.
I wrote some code to stop it automatically on fridays. Basically it looks like this
import sys
import time
if strftime("%a %H:%M", gmtime()) != "Fri 20:00":
...code...
else:
sys.exit()
how to run the main script permanently and open the other script automatically when needed? hep me to improve this please thanks.
EDIT actually I will reformulate the question:
Is there a proper way to run prermanently a script, besides doing:
while 1!=0:
...code here
Do you have any option to automatically schedule the program to start on mondays, with cron or windows task scheduler?
Alternatley, you could write a separate program that runs permanently and controlls the startup and/or shutdown of the script.py.
For your reformulated question, theres nothing wrong with using
while True:
do things in a loop forever
If that is indeed how the code needs to run.
It is avoidable if you want to, you could avoid it by restructuring your code so that it does not need to run in an infinite loop. There is no magic way to have a script 'keep running in a loop forever' without using a loop construct.
Though I'm wondering. DO you not like
while 1!=0:
because it looks a bit silly to say 1!=0 ?
while True:
Is a perfectly neat alternative.

Categories

Resources