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.
Related
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.
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.
How do you handle a code where you have to pause the code at any given moment. For example, you could be reading files from server and server is going to be rebooted; you would want to pause the code so it stops trying to read the file from the server. You also wouldn't want to rerun the code if you have been running it for a long time. Is there a way to pause a code for certain amount of time in python?
I looked into this everywhere and couldn't find any solution. There were few solution that was mentioned.
raw_input("") :if you are reading millions of file,I don't think you would want to manually enter every time it reaches this part of code.
sleep(): you wouldn't know when to pause the code and for how long so I don't think this would work.
There is a anything I can implement to take care of this issue?
Thanks,
Shone
I understand you don't see any snippet of code and I am sorry for not having any code snippet. I have been thinking about this issue and trying to find a solution in case this issue were to arise in future.
I don't know how this will affect efficiency-wise but can't you use sleep() inside a while loop or something like that.
As in,
while not condition: sleep(100)
or just,
while not condition: pass
What I've done in the past is have my script periodically check for the existence of a stop file somewhere, which can be manually put there when you want to pause.
So for this contrived example of an infinite while loop, the script checks for the file, and if it exists, it goes into a sleep loop and continues to check every second. When the file disappears the main loop will continue
import time, os.path
while True: # Main processing loop
while os.path.exists('path/to/file'):
time.sleep(1)
# Do processing stuff
# here
It's a bit of an ugly hack, but simple to implement.
OK, I have a IRC bot that has been my on going project while I pickup python, and thanks to everyone here who has helped when I have hit a wall.
ok so I have a while loop, I want a way to find if a new hour has started and if so, to run a method to update any settings, and to make sure that it is still connected to channels. I just have no idea how to go about, checking the time, and then to call a method.
there are many other things happening if this while loop, so doing a sleep for an hour isn't the best way to do this.
As I know questions with out code get marked down, here is some code.
while 1:
if(newhour() == 1):
run_Method()
To answer your question, you could do something like:
current_hour = datetime.now().hour
while 1:
if(datetime.now().hour != current_hour):
run_Method()
current_hour = datetime.now().hour
WARNING: This is a very simplistic solution.
Don't use an infinite while loop, you'll be wasting a processor core just to run a silly while loop. At least put a sleep() inside the while loop if you do use it anyway for a casual test code.
Cron service is already running in the system for this task. Place your script there (or create a script that calls your main script.)
$ cd /etc/cron.hourly/
Let's say proj_x.sh calls your main script. Create proj_x.sh:
$ vi proj_x.sh
Place the following code into proj_x.sh
#!/usr/bin/env python
/home/user/path_to_proj_x/cron_hourly.py
Make it executable:
$ chmod +744 proj_x.sh
Now, /home/user/path_to_proj_x/cron_hourly.py is the main script that carries your run_method()
Im sure there is a better way to do this, but I am quite the newbie so I did it the only way I could figure it out. The thing is, I have a script that updates a textfile with the newest posts from an RSS feed (I got some help from you guys to figure it out). But I want this script to be automated, so I made this:
import time
import os
seconds = 3600
kjor = 'python vg.py'
time.sleep(seconds)
os.system(kjor)
time.sleep(seconds)
os.system(kjor)
time.sleep(seconds)
os.system(kjor)
I continued with copying those 24x downwards. I know this problably can be done alot better with some loop (while?), but Im afraid I dont have alot of knowledge in that field (yet).
My question, however, is as following: Can the system be damaged in any way if I let this run over a longer period of time?
To answer your question, no, this won't hurt anything. While the time.sleeps are sleeping, the program will take very little processing power and the rest of the system can run normally.
Now, as for your looping issue. If you want the code run forever (or until you stop the program) the code you want is
while True:
os.system(kjor)
time.sleep(seconds)
This is, literally, and infinite loop, but in this case that (is probably) what you want.
If you are attached to having a particular number of iterations, then you could do something like sunqiang's answer (repeated here)
for loop in xrange(240):
os.system(kjor)
time.sleep(seconds)
Finally, if you are on a Unix platform (such as Linux or Mac) you should take a look at cron, as it is designed to set up recurring programs to run and particular time periods. You could set it up to run your script every minute and it will happily do so until the end of time (or you remove it, whichever comes first).
Use xrange please, don't copying your code 24x times.
for loop in xrange(240):
time.sleep(seconds)
os.system(kjor)
It will not damage your system, as far as I know.
It does not damage any system and it is pretty common as well.
Just create a loop so as your application will gently stop running after some time;
Or better yet, make it check for a condition, maybe listen to a tcp port waiting for someone to ask it to quit (then you'll need to create a second application to send this quit message).