简体   繁体   English

将 python 脚本作为 Windows 服务运行

[英]Run a python script as a windows service

Is there a way to run my python 3 script at system boot on windows 7?有没有办法在 Windows 7 上的系统启动时运行我的 python 3 脚本?

I know this has been asked a couple of times, but none of the solutions really filled my needs, because I prefer to stay with the free python interpreter rather than switching to ActivePython.我知道这已经被问过几次了,但是没有一个解决方案真正满足我的需求,因为我更喜欢使用免费的 python 解释器而不是切换到 ActivePython。

I have installed the Python for Windows extensions and would use py2exe, but it does not support Python 3.我已经安装了适用于 Windows 扩展Python并会使用 py2exe,但它不支持 Python 3。

You can also use the Windows Task Scheduler using the following steps (skip quotes when typing):您还可以通过以下步骤使用 Windows 任务计划程序(键入时跳过引号):

  • Search 'Task Scheduler' in Start button search box在开始按钮搜索框中搜索“任务计划程序”
  • Create a new task创建新任务
  • In 'Create Task' dialog, go to 'Triggers' tab and click 'New' button在“创建任务”对话框中,转到“触发器”选项卡并单击“新建”按钮
  • Select 'At startup' from the 'Begin the task' dropdown, click 'OK'从“开始任务”下拉菜单中选择“启动时”,单击“确定”
  • Go to 'Actions' tab and click 'New' button转到“操作”选项卡,然后单击“新建”按钮
  • 'Action' dropdown should show 'Start a program' “操作”下拉菜单应显示“启动程序”
  • In 'Program/script' box, type 'python.exe'在“程序/脚本”框中,键入“python.exe”
  • In 'Add arguments' box, type the full path to your python script with any arguments, such as 'C:\\Scripts\\startupscript.py -c onstart'在“添加参数”框中,输入带有任何参数的 Python 脚本的完整路径,例如“C:\\Scripts\\startupscript.py -c onstart”

Assuming that you have a fully woking stand alone exe file that's been generated from your python script using py2exe , you can just add a new string with some random key and value as the absolute path of your exe file under HKLM\\Software\\Microsoft\\Windows\\CurrentVersion\\Run of windows registry (accessible by running regedit from Window's run prompt).假设你有一个完全独立的 exe 文件,它是使用py2exe从你的 python 脚本生成的,你可以添加一个带有一些随机键和值的新字符串作为你的 exe 文件在HKLM\\Software\\Microsoft\\Windows\\CurrentVersion\\Run下的absolute path HKLM\\Software\\Microsoft\\Windows\\CurrentVersion\\Run windows registry的运行(可通过从 Window 的run提示运行regedit来访问)。 This will run the exe file whenever your Windows 7 boots up !这将在您的 Windows 7 启动时运行 exe 文件!

You can use the sc command.您可以使用sc命令。 I am unable to test it right now but i think it would look like this:我现在无法测试它,但我认为它看起来像这样:

sc create MyCoolService start=auto binpath=c:\mycoolprogram\supercool.exe obj=LocalSystem displayname=CoolService

See link for command syntax命令语法见链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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