Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python script stops running when screen turns off

I am trying to do a simple bot in Python, I have written the body and I wanted to try it on real data. So I ran it, it runs in an infinite loop and at the end of the loop it sleeps for 6 seconds because it gets data from server and needs to sleep so it does not get kicked from server. Anyway, by the time I sat in front of the PC and was doing my stuff the script was running (I am running it from Windows command prompt) but when I turned off the screen and came back, the script was not running anymore. I have tried this few times with different lengths of the runs of script and always it stopped after i turned off the screen.

  • How to keep python script running once the computer screen is turned off
like image 383
Coreggon Avatar asked Jul 07 '18 10:07

Coreggon


People also ask

What happens to Python script when computer sleeps?

Yes, terminal processes like a python script will stop execution when the machine goes into idle sleep. To prevent your machine from sleeping without changing the settings you can just use the terminal tool caffeinate . Once called it will prevent the machine from sleeping until you cancel the program using ctrl+c .

How do I keep a Python script running all the time?

sleep() Function To Run Script repeatedly. So, if you do not want to use the above code and just want to run your script repeatedly then you can use the time. sleep() function. This function allows your script to keep running after sleeping for a certain amount of time.


1 Answers

Check "Power Options" in the Control panel.

You don't need to worry about the screen locking or turning off as these wont affect running processes in python. However, if your system is set to sleep after a set amount of time you may need to change this to Never. Keep in mind there are separate settings depending on whether or not the system is plugged in.

like image 158
mrk Avatar answered Sep 21 '22 02:09

mrk