如何将 CLI 应用程序作为 Windows 服务运行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/165951/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I run a CLI Application as a Windows Service?
提问by Daren Thomas
Say I have a third party Application that does background work, but prints out all errors and messages to the console. This means, that currently, we have to keep a user logged on to the server, and restart the application (double-click) every time we reboot.
假设我有一个做后台工作的第三方应用程序,但将所有错误和消息打印到控制台。这意味着,目前,我们必须让用户登录到服务器,并在每次重新启动时重新启动应用程序(双击)。
Not so very cool.
不是很酷。
I was kind of sure, that there was an easy way to do this - a generic service wrapper, that can be configured with a log file for stdout
and stderr
.
我是那种肯定的是,有一个简单的方法来做到这一点-一个通用的服务包装,可以与日志文件来配置stdout
和stderr
。
I did check svchost.exe
, but according to this site, its only for DLL stuff. Pity.
我确实检查过svchost.exe
,但根据此站点,它仅适用于 DLL 内容。遗憾。
EDIT:The application needs to be started from a batch file. FireDaemon seems to do the trick, but I think it is a bit overkill, for something that can be done in <10 lines of python code... Oh well, Not Invented Here...
编辑:应用程序需要从批处理文件启动。FireDaemon 似乎可以解决问题,但我认为这有点矫枉过正,因为可以在 <10 行 python 代码中完成的事情......哦,不是在这里发明的......
回答by Dave Webb
Check out srvany.exe
from the Resource Kit. This will let run anything as a service.
srvany.exe
从资源工具包中查看。这将使任何东西作为服务运行。
You can pass parameters in the service definition to your executable via srvany.exe
so you could run a batch file as a service by seting the registry as follows:
您可以将服务定义中的参数传递给您的可执行文件,srvany.exe
以便您可以通过如下设置注册表来将批处理文件作为服务运行:
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\MyService\Parameters]
"Application"="C:\Windows\System32\cmd.exe"
"AppParameters"="/C C:\My\Batch\Script.cmd"
"AppDirectory"="C:\My\Batch"
Note: if you set up these keys in RegEdit
rather than using a file you only need single backslashes in the values.
注意:如果您在RegEdit
而不是使用文件中设置这些键,您只需要在值中使用单个反斜杠。
回答by Brad
I'd recommend NSSM: The Non-Sucking Service Manager.
我推荐NSSM:The Non-Sucking Service Manager。
- 32/64-bit EXEs
- Public Domain (!)
- Properly implements service stop messages, and sends the proper signal to your applications for graceful shutdown.
- 32/64 位 EXE
- 公共区域 (!)
- 正确实现服务停止消息,并向您的应用程序发送正确的信号以进行正常关闭。
回答by Torbj?rn Gyllebring
Why not simply implement a very thin service wrapper, here's a quickstart guide for writing a Service in .NET Writing a Useful Windows Service in .NET in Five Minutes
为什么不简单地实现一个非常瘦的服务包装器,这里有一个在 .NET 中编写服务的快速入门指南在五分钟内在.NET 中编写一个有用的 Windows 服务
When you got that running you can use the Processclass to start the application and configure it so that you can handle stdout/stderr yourself (ProcessStartInfois your friend).
当您运行它时,您可以使用Process类来启动应用程序并对其进行配置,以便您可以自己处理 stdout/stderr(ProcessStartInfo是您的朋友)。
回答by workmad3
Check out FireDaemon. There is a free version (FireDaemon lite I think) that only allows 1 service installed at a time, but this is a very useful tool for setting up services. It also wraps around batch files correctly, if this is required.
查看 FireDaemon。有一个免费版本(我认为 FireDaemon lite)一次只允许安装 1 个服务,但这是一个非常有用的设置服务的工具。如果需要,它还可以正确包装批处理文件。
回答by Jason
I second the firedaemon option. You may also want to set the option to allow the service to interact with the desktop to allow it to display the cli output window. They no longer offer a free version but if you search around the web for firedaemon lite you can find the older free lite version or maybe go the for pay route.
我第二个firedaemon选项。您可能还想设置选项以允许服务与桌面交互以允许它显示 cli 输出窗口。他们不再提供免费版本,但如果您在网上搜索 firedaemon lite,您可以找到较旧的免费 lite 版本,或者可能会选择付费路线。