且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2022-05-25 02:43:17

查看 srvany.exe 来自 资源工具包.这将使任何东西都可以作为服务运行.

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:

[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"

注意:如果您在 RegEdit 中设置这些键而不是使用文件,则您只需要在值中使用单个反斜杠.

Note: if you set up these keys in RegEdit rather than using a file you only need single backslashes in the values.