[英]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. 不幸的是,由于我在工作计算机上没有管理员权限,因此我似乎无法使用Windows Task Scheduler,并且当我以管理员身份登录以运行任务时,Python脚本无法运行。 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.
因此,我找到的唯一替代方法是创建一个bash脚本,该脚本将运行.py文件并将其添加到启动时运行的程序中。
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. 每当我重新启动计算机时,这似乎都可以正常工作,但是我注意到,如果我将计算机保持通宵状态,则批处理脚本将关闭,而我的Python脚本将无法运行。 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.
我已经检查了日志文件中是否有任何错误,并且脚本中似乎没有任何问题,似乎cmd提示屏幕已关闭。
Here is what I put in my .bat file: 这是我放入.bat文件的内容:
"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. 您可以尝试使用PowerShell创建Windows服务。 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 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. 然后,您可以使用事件查看器监视趋势的终止原因和终止时间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.