"NSSM Series" uses NSSM to install programs as Windows services

1. Introduction

NSSMis a service wrapper, no need to "install", just put it somewhere on the system (preferably somewhere in your path) and run it, you can wrap a normal exe program into Windowsservice to Windowsrun like a service. There are similar tools from Microsoft srvany, but NSSMthey are easier to use and more powerful. Its features are as follows:

  • Supports ordinary exe programs (console programs or Windows programs with interfaces are acceptable).
  • Easy to install and easy to modify.
  • Output can be redirected (and Rotation is supported).
  • Encapsulated services can be automatically guarded, and the program can be automatically restarted after it hangs.
  • Environment variables can be customized.

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

2. Download

Download address: https://nssm.cc/download

3. Instructions for use

1. Installation services

After downloading from the official website NSSM, it will be in the form of a compressed package locally. After decompression, there are two versions win32. win64Select according to the number of bits of your operating system. After entering the corresponding directory, right-click and select 在终端中打开or 在此处打开 Powershell 窗口, as shown in the figure:
Insert image description here
Open Windows PowerShellthe command line After opening the window, you can use to NSSMinstall the service. The specific command is as follows:

nssm install <服务名称>

The installer consists of several tabs with many configurable parameters. Most are preset to NSSMthe default values ​​for , so the service can be installed without leaving the Applications tab.

  • Application TabInsert image description here
    The path to the application (or script) you want to run is the only required field. If the application needs to start in a specific directory, you can enter that directory in the "Startup directory" field. If this field is left blank, the default startup directory will be the directory containing the application. The Arguments field can be used to specify any command line arguments to be passed to the application.

  • Details Tab
    Insert image description here
    The Details tab lists system details about the service.

  • Login Tab
    Insert image description here
    The Login tab allows you to manage the user account that will run the service. nssm will automatically ensure that the account you select has the necessary login as a service permissions.

  • Dependencies
    Insert image description here
    The Dependencies tab lists any services or service groups that must be started before the service can run.
    You can enter service names or display names, one per line. The service group name must be preceded by SC_GROUP_IDENTIFIER prefix (+ sign).

  • Process Tab
    Insert image description here
    The Process tab allows you to set the process priority and CPU affinity of your application. By default, applications will run at normal priority and allowed to execute on all CPUs. If you wish to limit the process to a subset of available CPUs, uncheck All Processors and select the CPUs as appropriate.
    While the service is running, process priority and affinity can be changed from Windows Task Manager.

  • Shutdown Tab
    Insert image description here
    The Shutdown tab lists the various stop methods and timeouts used to clean up an application after a crash or to gracefully stop a service.

  • Exit Action Tab
    Insert image description here
    The Exit Action tab lets you adjust restart limits and the default action when a service exits. You can also specify a mandatory delay between automatic application restarts.
    To configure the exit action for a specific application exit code, you must use the registry.

  • Input/Output Tab
    Insert image description here
    The I/O tab allows you to specify the input and/or output files used when I/O redirection is enabled. Setting output and errors is usually sufficient to capture the log messages generated by the application.
    Configure I/O in the registry for better control over paths and access patterns.

  • File Rotation Tab
    Insert image description here
    The File Rotation tab can be used in conjunction with the I/O settings to configure the rotation of output files when the service is restarted.
    If the Replace existing output and/or error files checkbox is checked, nssm will overwrite existing output files when starting the service. The default setting is to append to any existing file. If the Rotate files checkbox is selected, nssm will rename existing files before setting up I/O redirection. Use the Limit Rotation field to disable file rotation for files that were last modified later than a specified number of seconds or smaller than a specified number of kilobytes.
    By default, nssm only performs file rotation when the service (re)starts. To enable rotation of files that grow to a specified size limit while the service is running, select the Rotate while the service is running check box. Online rotation ignores any configured file age limits.
    Danger, moving parts! Online rotation requires nssm to intercept the application's output and write it to the file itself. Increased complexity inevitably leads to increased risk of failure.

  • Environment Tab
    Insert image description here
    The Environment tab allows you to specify a newline-delimited list of environment variables to be passed to the application. If the "Replace default environment" checkbox is selected, the specified variables will be the only ones passed to the service. If unchecked (default), the environment created when the service starts is retained.

2. Delete service

removeThe commands that can be used to delete services are as follows:

nssm remove <服务名>

After executing the above command, a confirmation window for deleting the service will be displayed, as shown in the figure:
Insert image description here
A confirmation window will be displayed before deleting the service.
Insert image description here

Starting from 2.0version 1, you can also use the following command to delete, and the confirmation window will not pop up:

nssm remove <服务名> confirm
3. Modify services

editThe commands that can be used to modify the service are as follows:

nssm edit <服务名>

After executing the command, the page for modifying the service will pop up. Just save the content that needs to be modified after operating on the page.

4. Start the service

startThe commands that can be used to start the service are as follows:

nssm start <服务名>

Note: In addition to starting the service through commands, you can also start it through the task manager or service management page of the operating system.

5. Stop service

stopThe commands that can be used to stop or close the service are as follows:

nssm stop <服务名>

Note: In addition to stopping the service through commands, you can also stop it through the task manager or service management page of the operating system.

6. Restart the service

restartThe commands that can be used to restart the service are as follows:

nssm restart <服务名>

Note: In addition to restarting the service through commands, you can also restart it through the task manager or service management page of the operating system.

For more instructions, you can also refer to the official website instructions: https://nssm.cc/usage

Guess you like

Origin blog.csdn.net/liuhuanping/article/details/133350327