Implement bat or exe files as services in Windows_and implement command line installation, configuration, startup, and deletion of services

1. Background description

        During the daily project development process in the Windows environment, sometimes it is necessary to register the bat file or exe file program as a Windows service to run on its own (without user login, the service can run as usual after booting), and for those who do not have For the exe program of the user interaction interface, it only needs to be run in the background. There is no need to display the contents of the exe program window on the desktop or the user to manually open it. It can also prevent the user from accidentally closing the program.

2. Microsoft’s official SC command

        Due to using Microsoft's default sc command to start the service, [[SC] StartService failed 1053: The service did not respond to the startup or control request in a timely manner. 】Error , so I choose the Nssm service encapsulation tool here to implement the bat or exe file as a service.

SC, NET command description
serial number SC order SC command description
1

Syntax: [ sc create service name start= auto binpath= "The file path and file name required as the service" displayname= "The service name displayed on the service manager" ]

Example: Set the Test_MontiorSCADAConnNumber.exe program in the E:\SC, NET command test\Debug path to automatically start the service named testexe

(sc create testexe start= auto binpath= "E:\SC, NET command test\Debug\Test_MontiorSCADAConnNumber.exe" displayname= "Test executable program as a service")

Create service

2

Syntax: [ sc config service name start=AUTO ]

Example: Set the testexe service to start automatically

(sc config testexe start=AUTO)

Set how the service is started
3

Syntax: [ sc start service name ] or [ net start service name ]

Example: Start testexe service

(sc start testexe)或(net start testexe)

Start service
4

Syntax: [ sc pause service name ] or [ net pause service name ]

Example: Pause testexe service

(sc pause testexe) or (net pause testexe)

Service Unavailable
5

Syntax: [ sc delete service name ] or [ net delete service name ]

Example: Delete testexe service

(sc delete testexe) or (net delete testexe)

Delete service

sc.exe create | Microsoft Learn Reference article for the sc.exe create command, which creates subkeys and entries for services in the registry and Service Control Manager database. icon-default.png?t=N7T8https://learn.microsoft.com/zh-cn/windows-server/administration/windows-commands/sc-createUse SC Configuration Service - Win32 apps | Microsoft Learn Windows SDK contains the command line utility (Sc.exe), Can be used to query or modify the database of installed services. Its commands correspond to the functions provided by SCM. The syntax is as follows. icon-default.png?t=N7T8https://learn.microsoft.com/zh-CN/windows/win32/services/configuring-a-service-using-scNet commands on operating systems - Windows Server | Microsoft Learn provides some information about Net commands on operating systems information. icon-default.png?t=N7T8https://learn.microsoft.com/zh-cn/troubleshoot/windows-server/networking/net-commands-on-operating-systems View the syntax of the SC command: [Open the CMD window and enter sc], as shown below Shown:

3. NSSM service encapsulation tool

3.1. Introduction to NSSM

        NSSM is an installation-free service management software in the Windows environment . It can encapsulate bat files or exe applications into services. The encapsulated services can be set to automatically start and other operations; it can also monitor the running status of the program and automatically Start to realize the function of daemon process; it not only supports graphical interface operation, but also fully supports command line settings. The same type of tool is Microsoft's own srvany ; but NSSM is easier to use and more powerful.

NSSM - the Non-Sucking Service Manager icon-default.png?t=N7T8https://nssm.cc/ NSSM download interface icon-default.png?t=N7T8https://nssm.cc/downloadDownload nssm-2.24.zip icon-default.png?t=N7T8https://nssm.cc/release/nssm-2.24.zip

3.2. Encapsulate bat or exe files as services

        Unzip the downloaded NSSM 2.24; select the corresponding NSSM according to the bitness of your operating system. For example, if my operating system is 64-bit, I choose to use the NSSM program in the win64 folder, as shown in the figure below:

NSSM encapsulation service commands
serial number NSSM encapsulates bat or exe files as service commands illustrate
1 nssm install Create a command to encapsulate a bat or exe file as a service, and open an operable interface command to easily select the file that needs to be encapsulated as a service
2 nssm install service name "path and name of bat or exe file to be encapsulated"

Directly create services for bat or exe files that need to be encapsulated

For example: I need to create a service named [testbatservice] from the [E:\SC, NET command test\restart Default Web Site.bat] file and the command is [nssm install testbatservice "E:\SC, NET command test\restart Default Web Site.bat"]

3 nssm start service name

Start the specified service

For example: [nssm start testbatservice]

4 nssm pause service name

Service Unavailable

For example: [nssm pause testbatservice]

5 nssm restart service name

Restart service

For example: [nssm restart testbatservice]

6 nssm stop service name

Out of service

For example: [nssm stop testbatservice]

7 nssm remove service name

Delete service

For example: [nssm remove testbatservice]

The complete command line content of the NSSM service encapsulation tool icon-default.png?t=N7T8https://nssm.cc/commands

① You must use the super administrator to open the command line (CMD) interface, otherwise it will fail when starting the service.

② Enter the NSSM program path (for example: my NSSM path here is: [E:\Thunder Download\nssm-2.24\win64])

--比如:我这里的NSSM路径是【E:\迅雷下载\nssm-2.24\win64】,那么我进入该路径的操作如下:
E:
cd E:\迅雷下载\nssm-2.24\win64

 

③Open the NSSM form to install the bat file as a service

④Start the service we just created (for example, the service name I just created here is: testbatservice)

⑤Restart the service (for example, the service name I just created here is: testbatservice)

 

 ⑥Stop the service (for example, the service name I just created here is: testbatservice)

⑦Removal service

 

Guess you like

Origin blog.csdn.net/xiaochenXIHUA/article/details/133411829