I'd like to keep my Python program running, even when my computer is sleeping. The python program should not stop even when the system sleeps.
I am using a Ubuntu.
My file is only on my computer, not online.
Please help me out to achieve this.
If you need more information, please leave a comment!
Short Answer -
No! you can't.
Details -
Sleep Mode is a power-saving state that is similar to pausing a DVD movie, all applications on the computer are suspended and there is no setting to change this. If you set the machine to sleep, then all programs are suspended. Sleep mode and hibernation both simply save the state your desktop is in (what programs are open, what files are accessed) in a file that is saved in RAM or on the hard drive respectively. But the computer is then put into a low power state.
Related
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
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.
Change "Battery saver" mode to "Best Performance" mode.
This worked for me, in continue running the scripts when screen turned off
Is it possible for a python script to open and execute a program each day at a specific time? My attention is...poor, but I installed a productivity program (FocalFilter) which blocks unproductive websites. Could a python script be written where it opens this program and runs it so the sites are blocked as soon as I get to my computer in the morning?
Does it matter if the computer is in locked mode (Windows 10)? I don't need a script or anything, it can be a fun little project, but I just don't know if it is even possible.
Yes, indeed you can use python. But the operating system(s) do it for you now.
Scheduled tasks run even if the computer is locked.
Take look at this:
https://ss64.com/nt/schtasks.html
or look att the Scheduler in Administration tools in Control panel. There you have many cool options.
Also, if you permanently want to block certain sites, like facebook.com and stackoverflow.com you can edit your hosts file so that facebook.com goes to 127.0.0.1, i.e. your own computer. Antiviruses do this too. Read here:
https://support.rackspace.com/how-to/modify-your-hosts-file/
I am not sure how to phrase this question (does a tree falling in the forest make a sound if there's no one there to hear it?) but I have a simple program that visits several websites through a selenium loop, opening each url in a new tab.
I know the program works fine when I execute it and watch it work. When that happens, the program opens the tabs as instructed, then leaves them open for me to check that everything worked fine, then it sends me an email to let me know that the program has run.
Now my problem is that I have scheduled this task in Windows, and I receive the email when the program executes, but when this happens and I check my browser I don't see any tabs left open, as I usually see after the program has run.
So my question is/are: does Selenium have some code-only mode that executes the programs in the background and goes to the places I indicated it to go, without opening the tabs if I am not using the browser at the time? Could it be possible that the program is running and reaching the end -thus sending me the email- but did not open the tabs and thus it is not working when I schedule it to work and I am not at the computer or the computer is on standby (I have scheduled it to wake the computer to execute the program)
Is there anything I am missing? Thanks!
While the copy is in progress, can we put a PC into sleep mode for a specific period of time, then wake up and continue copy using python script? Can you please share the code?
Actually this is possible using shell script.
Most machines manufactured after 2000 support real-time clock wakeup. There are many reasons to do so, one of which would be to record a TV program at a certain time. See ACPI Wakeup.
You'll have to explain what you mean by "While the copy is in progress" - there's not much to go on in the question. While OS drivers have suspend/resume functions, I don't know how to tell the python interpreter to save its state in the middle of running a script and then resume after wakeup. It's possible that the OS suspend/hibernate would fully capture the state of a copy operation and resume without a hiccup, but I wouldn't trust it to do so without substantial testing.
If you have "Wake On Lan" enabled you could potentially run a python script on a different PC and trigger the wake up after your specific period of time.
The scripts would probably need to talk to each other, unless you just do it all at times set in advance.
I have a web crawling python script running in terminal for several hours, which is continuously populating my database. It has several nested for loops. For some reasons I need to restart my computer and continue my script from exactly the place where I left. Is it possible to preserve the pointer state and resume the previously running script in terminal?
I am looking for a solution which will work without altering the python script. Modifying the code is a lower priority as that would mean to relaunch the program and reinvest time.
Update:
Thanks for the VM suggestion. I'll take that. For the sake of completion, what generic modifications should be made to script to make it pause and resumable?
Update2:
Porting on VM works fine. I have also modified script to make it failsafe against network failures. Code written below.
You might try suspending your computer or running in a virtual machine which you can subsequently suspend. But as your script is working with network connections chances are your script won't work from the point you left once you bring up the system. Suspending a computer and restoring it or saving a Virtual M/C and restoring it would mean you need to restablish the network connection. This is true for any elements which are external to your system and network is one of them. And there are high chances that if you are using a dynamic network, the next time you boot chances are you would get a new IP and the network state that you were working previously would be void.
If you are planning to modify the script, few things you need to keep it mind.
Add serializing and Deserializing capabilities. Python has the pickle and the faster cPickle method to do it.
Add Restart points. The best way to do this is to save the state at regular interval and when restarting your script, restart from last saved state after establishing all the transients elements like network.
This would not be an easy task so consider investing a considrable amount of time :-)
Note***
On a second thought. There is one alternative from changing your script. You can try using cloud Virtualization Solutions like Amazon EC2.
I ported my script to VM and launched it from there. However there were network connection glitches after resuming from hibernation. Here's how I solved it by tweaking python script:
import logging
import socket
import time
socket.setdefaulttimeout(30) #set timeout in secs
maxretry = 10 #set max retries
sleeptime_between_retry = 1 #waiting time between retries
erroroccured = 0
while True:
try:
domroot = parse(urllib2.urlopen(myurl)).getroot()
except Exception as e:
erroroccured += 1
if erroroccured>maxretry:
logger.info("Maximum retries reached. Quitting this leg.")
break
time.sleep(sleeptime_between_retry)
logging.info("Network error occurred. Retrying %d time..."%(erroroccured))
continue
finally:
#common code to execute after try or except block, if any
pass
break
This modification made my script temper proof to network failures.
As others have commented, unless you are running your script in a virtual machine that can be suspended, you would need to modify your script to track its state.
Since you're populating a database with your data, I suggest to use it as a way to track the progress of the script (get the latest URL parsed, have a list of pending URLs, etc.).
If the script is terminated abruptly, you don't have to worry about saving its state because the database transactions will come to the rescue and only the data that you've committed will be saved.
When the script is retarted, only the data for the URLs that you completely processed will be stored and you it can resume just picking up the next URL according to the database.
If this problem is important enough to warrant this kind of financial investment, you could run the script on a virtual machine. When you need to shut down, suspend the virtual machine, and then shut down the computer. When you want to start again, start the computer, and then wake up your virtual machine.
WinPDB is a python debugger that supports remote debugging. I never used it, and don't know if remote debugging a running process requires a modification to the script (which is very likely, otherwise it'd be a security issue); but if remote debugging without modifying the script is possible then you may be able to dump the current state of the script to a file and figure out later how to load it. I don't think it would work though.