创建自定义的服务Windows NT service

https://blog.csdn.net/wsdhla/article/details/128776861

windows可以使用sc,创建服务。

sc 是用来与服务控制管理器和服务进行通信的命令行程序。

sc create 在注册表和服务数据库中创建服务项。

# 以管理员身份运行

# 增加服务(选项名称包括等号,等号和值之间需要一个空格)
sc create ServiceName binPath= "路径" start= auto displayname= "服务显示名称"


# 删除服务
sc delete ServiceName


# 
sc start ServiceName

[SC] CreateService 失败 1072: 指定的服务已标记为删除。

关闭服务窗口(services.msc)再试试

这种方法不一定能成功,如果你的exe不符合服务的规范,可能会启动失败

[SC] StartService 失败 1053: 服务没有及时响应启动或控制请求

再换另外一种解决方法:使用Instrsrv.exeSrvany.exe (下载

1、以管理员身份运行cmd

"C:\Program Files\Resource Kit\instsrv.exe" 自定义服务名 "C:\Program Files\Resource Kit\srvany.exe"


# 删除服务
# "C:\Program Files\Resource Kit\instsrv.exe" 自定义服务名 remove

 提示信息

2、注册表中找到:计算机\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\自定义服务名

 3、启动服务

net start 自定义服务名

很遗憾的是,按照微软官网的文章操作,服务虽然注册成功也能启动成功,批处理里面的内容依然没有正常运行...

后续待补充...

===================================================

以下为微软官方文档

IMPORTANT: This article contains information about editing the registry. Before you edit the registry, make sure you understand how to restore it if a problem occurs. For information on how to do this, view the "Restoring the Registry" or the "Restoring a Registry Key" online Help topics in Registry Editor.

 

Summary

The Windows NT Resource Kit provides two utilities that allow you to create a Windows NT user-defined service for Windows NT applications and some 16-bit applications (but not for batch files).


Instrsrv.exe installs and removes system services from Windows NT and Srvany.exe allows any Windows NT application to run as a service.

More Information

To create a Windows NT user-defined service, perform the following steps:
 

  1. At a MS-DOS command prompt(running CMD.EXE), type the following command:

    path\INSTSRV.EXE My Service path\SRVANY.EXE

    where path is the drive and directory of the Windows NT Resource Kit (i.e., C:\RESKIT) and My Service is the name of the service you are creating.

    Example:

    C:\Program Files\Resource Kit\Instsrv.exe Notepad C:\Program Files\Resource Kit\Srvany.exeNOTE: To verify that the service was created correctly, check the registry to verify that the ImagePath value under

    HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\service name is set to point to SRVANY.EXE. If this is not set correctly, the service will stop shortly after it starts and return an Event ID 7000 "The service name failed to start."


    WARNING: Using Registry Editor incorrectly can cause serious problems that may require you to reinstall your operating system. Microsoft cannot guarantee that problems resulting from the incorrect use of Registry Editor can be solved. Use Registry Editor at your own risk.

    For information about how to edit the registry, view the "Changing Keys And Values" online Help topic or the "Add and Delete Information in the Registry" and "Edit Registry Data" online Help topics in Registry Editor.

    NOTE: You should back up the registry before you edit it.

  2. Run Registry Editor (Regedt32.exe)and locate the following subkey:

    HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\<My Service>

  3. From the Edit menu, click Add Key. Type the following and click OK:

    Key Name: Parameters
    Class : <leave blank>

  4. Select the Parameters key.

  5. From the Edit menu, click Add Value. Type the following and click OK:

    Value Name: Application
    Data Type : REG_SZ
    String : <path>\<application.ext>

    where <path>\<application.ext> is the drive and full path to the application executable including the extension (i.e., C:\WinNT\Notepad.exe)

  6. Close Registry Editor.

By default, a newly created service it configured to run Automatically when the system is restarted. To change this setting to Manual, run the Services applet from Control Panel and change the Startup value to Manual. A service set to Manual can be started in one of several ways:
 

- From the Services applet in Control Panel


- From a MS-DOS command prompt, type the following:


NET START <My Service>


- Use the Sc.exe utility from the Resource Kit. Type the following from a MS-DOS command prompt:


<path>\Sc.exe start <My Service>


where <path> is the drive and directory of the Windows NT Resource Kit (i.e., C:\Reskit).


For more information on installing and removing a user-defined service, please see the Srvany.wri document provided with the Windows NT Resource Kit utilities (i.e., C:\Reskit\Srvany.wri). This document can also be found on the Windows NT Resource Kit CD in the Common\Config directory.

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wsdhla

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值