简体   繁体   English

Windows 10-如何确保只要打开计算机,Python脚本就能运行?

[英]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.

相关问题 我如何安排Python脚本在Windows XP运行时一直运行? - How do I schedule a Python script to run as long as Windows XP is running? 如何在 pypy3 上运行 python 脚本而不使用 windows 10 上的命令行? - How do I run a python script on pypy3 without using the command line on windows 10? 如何仅通过在 Windows 10 cmd 行上键入脚本名称来运行 python 程序? - How do i run a python program just by typing the script name on windows 10 cmd line? 如何在Windows 10中从常规命令提示符处激活python anaconda并运行脚本 - How do I activate python anaconda and run script from regular command prompt in Windows 10 如何获取 python 脚本以在 Windows (10) 中运行命令 shell (10),它在程序特定位置打开? - How do I get a python script to run a command shell in Windows (10) where it opens in a program specific location? 如何在 Windows 10 中的准确时间运行 Python 脚本? - How can I run a Python script at an exact time in Windows 10? 如何使Windows 10计算机使用python脚本进入睡眠状态? - How to make a Windows 10 computer go to sleep with a python script? 在Windows 10上运行python脚本 - Run python script on Windows 10 如何在Windows上的Docker中运行Tensorflow运行python脚本? - How do I run a python script with Tensorflow running in a Docker on Windows? 如何打包在Windows上运行的python脚本? - How do I package a python script to run on Windows?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM