How to re-run process python in Windows/Linux? - python

I run Python script on Windows using IDE PyCharm.
Sometimes process is stopped. How can I configure that process will be re-run automatically after interruption?

There is also 3 ways as in this answer.
1 way create infinity batch loop
:loop
python your_script.py
goto loop
2 and 3rd way are already described here.
Also you can start python script as windows service but it much harder.

Related

How to restart python script inside bash script every x hours?

I have a quite simple question about starting and restarting python scripts inside an bash script. I hope it's not an duplicate but I didn't find a similiar question. I'm using a bash script which starts a couple of python scripts every time I create a docker container, but It should be probabaly the same on all linux based machines. It's simple as: python3 /mnt/device/script.py &
Now the script will keep running and measure the brightness until the container is stopped. Now I figured out that there are some problems with the sensor library which are brightly discussed on github and still not solved which causes the script to stop every couple of hours. For me It would be enough to have just some command which restarts my script which is running in the background every x hours to avoid that error. So what I'm looking for ist some command which will be like the follwing: python3 for hour=1 /mnt/device/script.py restart &
Thanks in advance!
You can use the Bash timeout command to send a signal to kill the Python script.
restart=""
while true; do
timeout 3600 python3 /mnt/device/script.py $restart
restart="restart"
done &
I'm guessing you don't really need this to run in the background (so maybe take out the &) and that you literally want restart to be passed as an argument after the first time.

How to make sure that python script will run forever on windows?

I wrote a python script that run an infinite loop and every half second checks if there are new files in a directory:
while True:
files = os.listdir(path_to_dir)
# do something
time.sleep(0.5)
The code runs on windows 10 in a cmd window and I need to make sure it will never stop.
A) I need to find a mechanism (or few mechanisms) that will restart the script in all possible scenarios that it might turn off (it is ok if the restart will happen only 2 minutes later...):
if the computer is restarting
if someone close the cmd windows
if the script end unexpectedly because of unhandle exception or memory leak (it is not suppose to happen...)
B) I want that once in a week, proactively,the script will be turn off and restart.
Any ideas?
Thanks!!
p.s. my first idea was that the python script will only check for new file, and the task scheduled will run it every second, but the minimum interval for task scheduler is 1 minute.
I think these answer all your questions:
Since your script runs on windows, you can use the Microsoft Task Scheduler (which is installed with Windows) to start your Python script when your computer starts up.
If you do not use the cmd window, you can change your Python script extention from .py to .pyw to run the script without a terminal window. A bit more on that here: Executing scripts.
For opening the script after an exception has happend, have a look at this blog post: How to Restart Python Script after Exception and Run it Forever.
To restart your script once a week, you can also use the Task Scheduler mentioned in answer 1. I think this post could help you with restarting your script: Start and stop a python task

Python Shell memory management for always running script

I have written a python (python version is 3) script that runs 24/7. The way I run my script in my Windows machine is the following. I right click on the .py file, then click on "Edit with IDLE" and then "Run". The script has no issue but, due to the many instructions printed in the python shell (I use a logger), after a couple of days this python shell gets very heavy. My newbie question is the following. Is there to limit the number of rows temporarily saved in the python shell to a specific number? Or perhaps somebody has a better suggestion to run this constantly running script that prints a lot of the script steps in the shell? Please, notice how I'm not asking how to run a script 24/7, it's my understanding the best way to do it is though a VPS. My problem is that the data saved in the displayed python shell gets bigger and bigger every day, so I only wonder how to limit the data temporarily displayed/saved in it. Thanks

Why is my Python script running twice in the background when I try to execute it at startup of my Raspberry Pi by adding the command to /etc/profile?

Why is my Python script running twice in the background when I try to execute it at startup of my Raspberry Pi by adding the command to /etc/profile?
I have a command written at the end of the file /etc/profile for a Python script to run at startup of my Raspberry Pi, "sudo python /path/filename.py &", and for some reason it runs twice, every time. When I comment the line out and execute it manually from the command line it runs normally. Why does that happen and what can I do to prevent that from happening?
I know for fact that it is running twice in the background because in my code I have a buzzer that beeps twice at times and 3 times at others, and it beeps 4 times instead of 2 and 6 times instead of 3. Also the code ends up contradicting itself, clearly because each script run is trying to do something else at the same time.
Thanks in advance.
I'm answering my own question with a better method for running scripts at boot/startup.
I'm not exactly sure why this was happening, but I did learn that executing scripts on startup with this method is a bad practice and is best avoided.
I started using the Crontab instead.
This is what you need to do:
crontab -e
This opens up the crontab, then add the following line:
#reboot python /filelocation/filename.py
This will execute the script as soon as the Pi boots up.
No more double script runs!
Do you have VNC enabled? I think that's the problem. It was for me.
See this related discussion:
https://www.raspberrypi.org/forums/viewtopic.php?f=66&t=59285
So you can disable VNC, do the run levels danny suggested, create a new user (different to the one used for VNC) or start doing this sort of stuff in your script:
if [ x"$ALREADY_DONE" == x"" ]; then
export ALREADY_DONE=yes
foobar
fi

How to kill a process in python 2.5 on windows 64bit platform?

I have a python script by which i am opening three executable files as follows:
Aa=subprocess.Popen([r"..\location\learning\A.exe"])
Bb=subprocess.Popen([r"..\location\learning\new\B.bat"])
Cc=subprocess.Popen([r"..\location\learning\new\B.bat"])
All the three files are getting opened. Now,next step i want kill these three opened modules.So, firstly i tried to kill "Aa" as follows:
PROCESS_TERMINATE= 1
k = ctypes.windll.kernel32klll
handle = k.OpenProcess(PROCESS_TERMINATE, False,Aa.pid)
k.TerminateProcess(handle, -1)
k.CloseHandle(handle)
But after adding these piece of lines the three modules 'Aa','Bb' and 'Cc' they don't gets opened.So,i want to know some clean solution so that firstly all the three modules gets executed and then after a while they get closed itself. As,i am using python 2.5 on windows 64 bit platform so kindly suggest solution accordingly.
The process returned by subprocess.Popen() has a terminate() method to kill the child process since Python 2.6. To use that you would have to upgrade your Python install or install two versions of Python in parallel.
But a better way is usually to find out how you can tell this process to stop and use that. Many processes stop when you send them certain standard input or when you close the pipes which Popen created.

Categories