简体   繁体   English

使用批处理文件创建并启动Windows 7或10服务?

[英]Create and start Windows 7 or 10 service using batch file?

According the this link, 根据此链接,

How to Create a User-Defined Service 如何创建用户定义的服务

you can create a Windows Service using Windows NT Resource Kit. 您可以使用Windows NT资源工具包创建Windows服务。 For example as shown in above link, 例如,如上面的链接所示,

C:\Program Files\Resource Kit\Instsrv.exe Notepad C:\Program Files\Resource Kit\Srvany.exe

But this does not work in Windows 7 or 10 and 64bit platform for that matter becasue Windows NT Resource Kit is not available for new Windows. 但这在Windows 7或10和64位平台上不起作用,因为Windows NT资源工具包不适用于新Windows。

I was able to create the Windows Service using this batch file, 我能够使用此批处理文件创建Windows服务,

sc create "MyService" binPath= "C:\Program Files (x86)\MyProg\myprog.exe" start= auto DisplayName= "My Service" obj= LocalSystem

But it does not start the Windows Service. 但是它不会启动Windows服务。 When I try to start I get this message, 当我尝试启动时,收到此消息,

在此处输入图片说明

The information in " Install a Windows service using a Windows command prompt? " and " Create Windows service from executable " does not work. 使用Windows命令提示符安装Windows服务? ”和“ 从可执行文件创建Windows服务 ”中的信息不起作用。

How to start the Windows Service without any error? 如何启动Windows服务而不会出现任何错误?

Srvany.exe is an ancient program and it will work, to my best knowledge, till windows 2003. You can just forget it existed for windows 7/10. Srvany.exe是一个古老的程序,据我所知,它将一直运行到Windows2003。您可能会忘记它在Windows 7/10中已经存在。

To start/stop service on windows 7/10 (I'm using acronis service) with the service name which is usually quite different from the display name: 要在Windows 7/10上启动/停止服务(我正在使用acronis服务),其服务名称通常与显示名称大不相同:

Stopping running service: 停止运行服务:

sc stop syncagentsrv

SERVICE_NAME: syncagentsrv
        TYPE               : 10  WIN32_OWN_PROCESS
        STATE              : 3  STOP_PENDING
                                (STOPPABLE, NOT_PAUSABLE, ACCEPTS_PRESHUTDOWN)
        WIN32_EXIT_CODE    : 0  (0x0)
        SERVICE_EXIT_CODE  : 0  (0x0)
        CHECKPOINT         : 0x0
        WAIT_HINT          : 0x0

Starting running service: 开始运行服务:

sc start syncagentsrv

SERVICE_NAME: syncagentsrv
        TYPE               : 10  WIN32_OWN_PROCESS
        STATE              : 2  START_PENDING
                                (STOPPABLE, NOT_PAUSABLE, ACCEPTS_PRESHUTDOWN)
        WIN32_EXIT_CODE    : 0  (0x0)
        SERVICE_EXIT_CODE  : 0  (0x0)
        CHECKPOINT         : 0x0
        WAIT_HINT          : 0x0
        PID                : 2240
        FLAGS              :

Check the statek of the service: 检查服务状态:

sc query syncagentsrv

SERVICE_NAME: syncagentsrv
        TYPE               : 10  WIN32_OWN_PROCESS
        STATE              : 4  RUNNING
                                (STOPPABLE, NOT_PAUSABLE, ACCEPTS_PRESHUTDOWN)
        WIN32_EXIT_CODE    : 0  (0x0)
        SERVICE_EXIT_CODE  : 0  (0x0)
        CHECKPOINT         : 0x0
        WAIT_HINT          : 0x0

To create the service you want to: 要创建服务,您需要:

sc create "MyService" binPath="C:\Program Files (x86)\MyProg\myprog.exe" start=auto DisplayName="My Service"

This only creates the service and adds record into the registry/serviceDB. 这只会创建服务并将记录添加到注册表/ serviceDB中。 You need to check the exe file itself. 您需要检查exe文件本身。

You don't need obj=LocalSystem as this is default option. 您不需要obj = LocalSystem,因为这是默认选项。 Don't create spaces between the equal sign = and the values! 不要在等号=和值之间创建空格!

Did you check event viewer? 您检查了事件查看器吗? What does it say when you start the service? 启动服务时会说什么? Only the timeout? 只有超时? Does the myprog.exe work alone without being a service? myprog.exe是否可以单独运行而无需提供服务? Please answer these questions and I'll edit the answer. 请回答这些问题,我将编辑答案。

Edit1/2 based on comments and should you have any difficulties with the above approach 基于注释的Edit1 / 2 ,如果您对上述方法有任何困难

There is a small utility called SrvStart (originally from Nick Rozanski ), but it has been now adapted for VS 2017 (get srvstart.v120.zip which needs to be build). 有一个名为SrvStart的小实用程序(最初来自Nick Rozanski ),但是现在已针对VS 2017进行了修改(获取srvstart.v120.zip ,需要进行构建)。 If you are happy with the older version and already provided exe files download the srvstart_run.v110.zip and copy both *.exe files and *.dll into the directory as indicated below. 如果您对旧版本满意,并且已经提供了exe文件,请下载srvstart_run.v110.zip ,并将* .exe文件和* .dll复制到目录中,如下所示。

Copy all the files into C:\\Windows (yes, your windows directory). 将所有文件复制到C:\\ Windows (是,您的Windows目录)。 The reason being that the C:\\Windows should be always in your PATH thus reachable by the SrvStart executable. 原因是C:\\ Windows应该始终在您的PATH因此SrvStart可执行文件可以访问它。

Now create a MyProg.ini file: 现在创建一个MyProg.ini文件:

[MyService] startup="C:\\Program Files (x86)\\MyProg\\myprog.exe" shutdown_method=winmessage [MyService] startup =“ C:\\ Program Files(x86)\\ MyProg \\ myprog.exe” shutdown_method = winmessage

winmessage forces to close any opened windows when you are shutting down the service. 当您关闭服务时, winmessage强制关闭所有打开的窗口。

Then place the *.ini file directly into your root c:\\ . 然后将*.ini文件直接放入根目录c:\\

Then use a command: 然后使用命令:

sc create <servicename> Displayname= "<servicename>" binpath= "srvstart.exe <servicename> -c <path to srvstart config file>" start= <starttype>

In your case it would be: 您的情况是:

sc create "MyService" DisplayName="My Service" binPath="srvstart.exe MyService -c C:myprog.ini" start=auto 

Note: There is no backslash ( \\ ) between C: and myprog.ini which is correct. 注意: C:myprog.ini之间没有反斜杠( \\ ),这是正确的。

Now check your services and you should see the My Service name there and it should behave like a service. 现在检查您的服务,您应该在此处看到“我的服务”名称,它的行为应类似于服务。

Edit 3 - based on comments from @CodenameK 编辑3-基于@CodenameK的注释

Apparently you need to compile the srvstart 120 version with that is compilable with VS2017 to make it run correctly @Win10. 显然,您需要使用VS2017编译的srvstart 120版本进行编译,以使其正确运行@ Win10。 The only version available is the old 1.1 version, which appears not to work correctly at Win10. 唯一可用的版本是旧的1.1版本,在Win10上似乎无法正常工作。

The solution that worked for CodenameK was to use - NSSM - the Non-Sucking Service Manager . 适用于CodenameK的解决方案是使用-NSSM-非吸吮服务管理器 For future reference if anyone needs it. 如果有人需要,以供将来参考。

暂无
暂无

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

相关问题 如何使用批处理文件固定到开始菜单 (Windows 10) - How to pin to start menu using a batch file (Windows 10) 如何使用带有空格的路径和命令的“start”在Windows中创建批处理文件 - How to create batch file in Windows using “start” with a path and command with spaces Windows Service-在Windows 2012 Server R2上使用任务计划程序使用批处理文件启动服务 - windows service - start service using batch file using task scheduler on windows 2012 server R2 为什么我不能在Windows Server 2008中使用system()从服务启动批处理文件? - Why can't I start a batch file from a service using system() in Windows Server 2008? 批处理脚本问题(Tomcat安装服务但开始使用批处理文件) - Batch script issue (Tomcat install the service but start using batch file ) 使用批处理文件重新启动远程 Windows 服务 - Restart a remote windows service using batch file 如何创建用于启动本地 Node 应用程序的 Windows 10 批处理文件? - How to create a Windows 10 batch file for launching local Node app? 创建一个显示消息框的批处理文件(Windows 10) - Create a batch file that displays a message box (Windows 10) 批处理脚本以在10分钟的间隔内启动服务 - Batch script to start the service in gap of 10 min 如何在 Windows 中创建批处理文件来修复和启动损坏的 mongoDB? - How to create a batch file to repair and start corrupted mongoDB in windows?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM