Use NSSM to wrap the exe as a service

NSSM is a service encapsulation program, which can encapsulate a common exe program into a service and make it run like a windows service. The same type of tool also has Microsoft's own srvany, but nssm is easier to use and more powerful. Its features are as follows:

  1. Support common exe program (console program or Windows program with interface can be used)
  2. Simple installation and easy modification
  3. Can redirect output (and supports Rotation)
  4. It can automatically guard the encapsulated service, and it can automatically restart after the program hangs.
  5. Customizable environment variables

Every function here is very practical, and using NSSM to encapsulate services can greatly simplify our development process.

  • It is an ordinary program during development, which reduces the difficulty of development and is very convenient to debug.
  • The installation is simple, and the service properties can be modified at any time, and the update is more convenient
  • A simple logging system can be implemented directly using console output
  • No need to consider adding another service to implement the service guard function

I think one of the functions that it needs to be added is to redirect the input and output to a tcp connection, so that the program interaction can be realized through telnet, which is more convenient to use.

The following is a brief introduction to how to use this tool.

First go to the official website of nssm to download

 

Service installation:

Service installation can be done using the following command:  nssm install <servicename>

After executing this command, an interface will appear. Basically, you can see how to use it. In most cases, you only need to fill in the program path of the first interface.

Other interfaces are the configuration of advanced parameters, which can be selected according to your needs.

After filling in the parameters, execute the "install service" button to install the service to the system, which can be viewed using the system's service management tool.

Of course, if you want to automate the installation, you can directly bring the program path:  nssm install <servicename> <program> [<arguments>]

NSSM itself basically supports win7 and above systems. I have tested win7, 2008, and 2016 systems, and there is no problem. If the installation fails, please first check whether a domestic housekeeper or domestic antivirus software is installed.

After the installation is complete, the service has not been started yet. You need to start the service through the following service management command.

 

Service management:

Service management mainly includes start, stop and restart. The commands are as follows:

  • Start the service: nssm start <servicename>
  • Stop the service: nssm stop <servicename>
  • Restart the service: nssm restart <servicename>

Of course, you can also use the service manager that comes with the system to operate and use system commands.

 

Change parameters:

The service installed by NSSM is very convenient to modify. The command is as follows:

nssm edit <servicename>

The operation interface will be automatically activated, and you can change it directly.

 

Service removal:

Service deletion can be done with one of the following commands:

  • nssm remove <servicename>
  • nssm remove <servicename> confirm

There is no big difference in function, the following commands are automatically confirmed, and there is no interactive interface.

 

Command Line:

Service automation requires more command lines, see the official documentation for details:  Managing services from the command line

The following is an example of installing the Jenkins service:

nssm install Jenkins %PROGRAMFILES%\Java\jre7\bin\java.exe
nssm set Jenkins AppParameters -jar slave.jar -jnlpUrl https://jenkins/computer/%COMPUTERNAME%/slave-agent.jnlp -secret redacted
nssm set Jenkins AppDirectory C:\Jenkins
nssm set Jenkins AppStdout C:\Jenkins\jenkins.log
nssm set Jenkins AppStderr C:\Jenkins\jenkins.log
nssm set Jenkins AppStopMethodSkip 6
nssm set Jenkins AppStopMethodConsole 1000
nssm set Jenkins AppThrottle 5000
nssm start Jenkins

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324813445&siteId=291194637