如何将CLI应用程序作为Windows服务运行?

时间:2021-04-07 20:45:48

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代码中完成...哦,好吧,不是在这里发明的......

5 个解决方案

#1


11  

Check out srvany.exe from the Resource Kit. This will let run anything as a service.

从Resource Kit中查看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中设置这些键而不是使用文件,则只需要在值中使用单个反斜杠。

#2


4  

I'd recommend NSSM: The Non-Sucking Service Manager.

我推荐NSSM:非吸吮服务经理。

  • 32/64-bit EXEs
  • Public Domain (!)
  • 公共区域 (!)

  • Properly implements service stop messages, and sends the proper signal to your applications for graceful shutdown.
  • 正确实现服务停止消息,并将正确的信号发送到您的应用程序以实现正常关闭。

#3


3  

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 Process class to start the application and configure it so that you can handle stdout/stderr yourself (ProcessStartInfo is your friend).

当你运行它时,你可以使用Process类来启动应用程序并对其进行配置,以便你自己处理stdout / stderr(ProcessStartInfo是你的朋友)。

#4


1  

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个服务,但这是一个非常有用的工具来设置服务。如果需要,它还会正确地包装批处理文件。

#5


0  

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.

我是第二个选中的选项。您可能还需要设置选项以允许服务与桌面交互以允许它显示cli输出窗口。他们不再提供免费版本,但如果你在网上搜索fireaemon lite,你可以找到旧的免费精简版或者可以去付费路线。

#1


11  

Check out srvany.exe from the Resource Kit. This will let run anything as a service.

从Resource Kit中查看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中设置这些键而不是使用文件,则只需要在值中使用单个反斜杠。

#2


4  

I'd recommend NSSM: The Non-Sucking Service Manager.

我推荐NSSM:非吸吮服务经理。

  • 32/64-bit EXEs
  • Public Domain (!)
  • 公共区域 (!)

  • Properly implements service stop messages, and sends the proper signal to your applications for graceful shutdown.
  • 正确实现服务停止消息,并将正确的信号发送到您的应用程序以实现正常关闭。

#3


3  

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 Process class to start the application and configure it so that you can handle stdout/stderr yourself (ProcessStartInfo is your friend).

当你运行它时,你可以使用Process类来启动应用程序并对其进行配置,以便你自己处理stdout / stderr(ProcessStartInfo是你的朋友)。

#4


1  

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个服务,但这是一个非常有用的工具来设置服务。如果需要,它还会正确地包装批处理文件。

#5


0  

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.

我是第二个选中的选项。您可能还需要设置选项以允许服务与桌面交互以允许它显示cli输出窗口。他们不再提供免费版本,但如果你在网上搜索fireaemon lite,你可以找到旧的免费精简版或者可以去付费路线。