简体   繁体   中英

Windows 10- how do I ensure Python script will run as long as computer is on?

Unfortunately, I can't seem to use Windows Task Scheduler as I don't have admin rights on my work computer, and when I sign in as admin to run tasks the Python script doesn't run. Because of this, the only alternative I've found is creating a bash script that will run my .py file and adding that to my programs that run on startup.

This seems to work fine whenever I restart my computer, but I notice that if I leave my computer on overnight the batch script will close and my Python script won't run. I've checked the log files for any errors and there don't seem to be any issues in the script, it just seems that the cmd prompt screen closes.

Here is what I put in my .bat file:

"C:\Python\Python37-32\python.exe" "T:\service.py" > T:\output.log 2>&1

cmd /k

Just trying to figure out- why is this happening? And is there any way I can just keep this script running as long as my computer is on?

You could attempt to create a Windows Service with PowerShell. Just have it automatically recover when it's terminated. See this as a reference.

https://github.com/yaplex/CodeSamples/blob/092159a847e330778494ea7b69ee84d120db7e36/PowerShell/Install-Windows-Service.ps1

Then you can monitor trends with Event Viewer as to why and when it's terminated.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM