NSSM使用文档-机翻版

在此记录NSSM相关的功能和配置,由于NSSM官网响应慢,一起提供使用文档的机翻版本。

NSSM文档

下述文档为官网机翻,仅供参考

Usage 用法

No “installation” of nssm is needed. Just place it somewhere on the system (preferably somewhere in your PATH) and run it.
不需要“安装”nssm。只需将其放在系统上的某个位置(最好是在您的路径中的某个位置)并运行它。
Note however that nssm registers itself as an Event Log message source which means that running multiple instances or different version of nssm from different locations may cause confusion. Also note that if you run the Event Viewer it will open the nssm executable, preventing you from overwriting it. Keep this in mind if you come to upgrade nssm.
但请注意,nssm 将自身注册为事件日志消息源,这意味着从不同位置运行多个实例或不同版本的 nssm 可能会导致混乱。另请注意,如果您运行事件查看器,它将打开 nssm 可执行文件,从而防止您覆盖它。如果您要升级 nssm,请记住这一点。
_Some features are labelled as supported as of a particular version. If the version described is newer than that available from the download page there may be a pre-release build with the feature enabled.某些功能被标记为特定版本受支持。如果描述的版本比下载页面上提供的版本更新,则可能有启用该功能的预发布版本。
Equivalent command examples below show the commands which would configure an existing service to match the screenshots. In many cases they represent the defaults for the parameters in question and are thus redundant. Any parameter can also be reset to its default value with
下面的等效命令示例显示了配置现有服务以匹配屏幕截图的命令。在许多情况下,它们代表相关参数的默认值,因此是多余的。任何参数也可以重置为其默认值
nssm reset

Installing a service 安装服务

You can use nssm to install a service. The command to type is:
您可以使用 nssm 安装服务。要输入的命令是:
nssm install
The installer consists of several tabs with lots of configurable parameters. Most are preset to nssm’s defaults, so it’s possible to install a service without leaving the Application tab.
安装程序由多个选项卡组成,其中包含许多可配置参数。大多数都预设为 nssm 的默认值,因此可以在不离开“应用程序”选项卡的情况下安装服务。

Application tab 应用选项卡

The Path to the application (or script) you want to run and is the only mandatory field. If the application needs to start in a particular directory you can enter it in the Startup directory field. If the field is left blank the default startup directory will be the directory containing the application. The Arguments field can be used to specify any commandline arguments to pass to the application.
您要运行的应用程序(或脚本)的路径是唯一的必填字段。如果应用程序需要在特定目录中启动,您可以在“启动目录”字段中输入该目录。如果该字段留空,则默认启动目录将是包含应用程序的目录。 Arguments 字段可用于指定要传递给应用程序的任何命令行参数。
The screenshot below shows installation of a UT2003 server. The command to run such a service is ucc server so the full path to UCC.exe is entered under Path and server is entered under Options.
下面的屏幕截图显示了 UT2003 服务器的安装。运行此类服务的命令是 ucc server,因此在“路径”下输入 UCC.exe 的完整路径,在“选项”下输入服务器。
image.jpg
Equivalent commands: 等效命令:
nssm set UT2003 Application C:\games\ut2003\System\UCC.exenssm set UT2003 AppDirectory C:\games\ut2003\Systemnssm set UT2003 AppParameters server
Clicking Install service completes the installation of the service.
单击“安装服务”完成服务的安装。

Details tab 详细信息选项卡

The Details tab lists system details about the service.
“详细信息”选项卡列出了有关服务的系统详细信息。
image.jpg
Equivalent commands: 等效命令:
nssm set UT2003 DisplayName UT2k3nssm set UT2003 Description Unreal Tournament 2003nssm set UT2003 Start SERVICE_AUTO_START

Log on tab 登录选项卡

The Log on tab can be used to manage the user account which will run the service. nssm will automatically ensure that the account you choose has the necessary log on as a service permissions.
登录选项卡可用于管理将运行该服务的用户帐户。 nssm 将自动确保您选择的帐户具有必要的作为服务登录权限。
image.jpg
Equivalent commands: 等效命令:
nssm set UT2003 ObjectName LocalSystemnssm set UT2003 Type SERVICE_WIN32_OWN_PROCESS
Refer to the command line usage documentation for details on configuring an account and password on the command line. If you need to configure a blank password you must use the command line.
命令行配置账号和密码的详细内容请参见命令行使用文档。如果需要配置空白密码,则必须使用命令行。

Dependencies 依赖关系

The Dependencies tab lists any services or service groups which must be started before the the service can run.
“依赖项”选项卡列出了在服务运行之前必须启动的所有服务或服务组。
You can enter service names or display names, one per line. Service group names must be preceded by the SC_GROUP_IDENTIFIER prefix (the + symbol).
您可以输入服务名称或显示名称,每行一个。服务组名称前面必须带有 SC_GROUP_IDENTIFIER 前缀(+ 符号)。
image.jpg
Equivalent commands: 等效命令:
nssm set UT2003 DependOnService MpsSvc

Process tab 工艺选项卡

The Process tab can be used to set process priority and CPU affinity for the application. By default the application will run with normal priority and be allowed to execute on all CPUs. If you wish to restrict the process to a subset of available CPUs, uncheck “All processors” and select the CPU(s) as desired.
“进程”选项卡可用于设置应用程序的进程优先级和 CPU 关联性。默认情况下,应用程序将以正常优先级运行,并允许在所有 CPU 上执行。如果您希望将进程限制为可用 CPU 的子集,请取消选中“所有处理器”并根据需要选择 CPU。
Process priority and affinity can be changed from the Windows task manager while the service is running.
当服务运行时,可以从 Windows 任务管理器更改进程优先级和关联性。
image.jpg
Equivalent commands: 等效命令:
nssm set UT2003 AppPriority NORMAL_PRIORITY_CLASSnssm set UT2003 AppNoConsole 0nssm set UT2003 AppAffinity All

Shutdown tab 关机选项卡

The Shutdown tab lists the various stop methods and timeouts used when tidying up the application after a crash or when the service is gracefully stopped.
“关闭”选项卡列出了崩溃后清理应用程序或正常停止服务时使用的各种停止方法和超时。
image.jpg
Equivalent commands: 等效命令:
nssm set UT2003 AppStopMethodSkip 0nssm set UT2003 AppStopMethodConsole 1500nssm set UT2003 AppStopMethodWindow 1500nssm set UT2003 AppStopMethodThreads 1500

Exit actions tab 退出操作选项卡

The Exit actions tab can be used to tweak the restart throttling and default action on exit for the service. You can also specify a mandatory delay between automatic restarts of the application.
退出操作选项卡可用于调整重新启动限制和服务退出时的默认操作。您还可以指定应用程序自动重新启动之间的强制延迟。
To configure exit actions for specific application exit codes you must use the registry as described below.
要配置特定应用程序退出代码的退出操作,您必须使用注册表,如下所述。
image.jpg
Equivalent commands: 等效命令:
nssm set UT2003 AppThrottle 1500nssm set UT2003 AppExit Default Restartnssm set UT2003 AppRestartDelay 0

I/O tab 输入/输出选项卡

The I/O tab can be used to specify the input and/or output files used when I/O redirection is enabled. Setting Output and Error is usually sufficient to capture log messages generated by the application.
I/O 选项卡可用于指定启用 I/O 重定向时使用的输入和/或输出文件。设置输出和错误通常足以捕获应用程序生成的日志消息。
Configure I/O in the registry as described below for more control over paths and access modes.
如下所述在注册表中配置 I/O,以更好地控制路径和访问模式。
image.jpg
Equivalent commands: 等效命令:
nssm set UT2003 AppStdout C:\games\ut2003\service.lognssm set UT2003 AppStderr C:\games\ut2003\service.log

File rotation tab 文件旋转选项卡

The File rotation tab can be used in conjunction with I/O settings to configure rotation of output files when the service restarts.
文件轮换选项卡可与 I/O 设置结合使用,以配置服务重新启动时输出文件的轮换。
If the Replace existing Output and/or Error files checkbox is checked, nssm will overwrite existing output files when starting the service. The default is to append to any existing files. If the Rotate files checkbox is checked, nssm will rename existing files prior to setting up I/O redirection. Use the Restrict rotation fields to disable file rotation for files which have been modified more recently than the specified number of seconds or are smaller than the specified number of kilobytes.
如果选中替换现有输出和/或错误文件复选框,则 nssm 将在启动服务时覆盖现有输出文件。默认设置是附加到任何现有文件。如果选中旋转文件复选框,nssm 将在设置 I/O 重定向之前重命名现有文件。使用“限制轮换”字段可对最近修改时间晚于指定秒数或小于指定千字节数的文件禁用文件轮换。
By default nssm only performs file rotation when the service (re)starts. To enable rotation of files which grow to the specified size limit while the service is running, check the Rotate while service is running checkbox. Online rotation ignores any configured file age limit.
默认情况下,nssm 仅在服务(重新)启动时执行文件轮换。要在服务运行时启用轮换增长到指定大小限制的文件,请选中服务运行时轮换复选框。在线轮换会忽略任何配置的文件期限限制。
Danger, moving parts! Online rotation requires nssm to intercept the application’s output and write the files itself. Increased complexity necessarily leads to increased risk of failure.
危险,活动部件!在线轮换需要 nssm 拦截应用程序的输出并自行写入文件。复杂性的增加必然导致失败风险的增加。
image.jpg
Equivalent commands: 等效命令:
nssm set UT2003 AppStdoutCreationDisposition 4nssm set UT2003 AppStderrCreationDisposition 4nssm set UT2003 AppRotateFiles 1nssm set UT2003 AppRotateOnline 0nssm set UT2003 AppRotateSeconds 86400nssm set UT2003 AppRotateBytes 1048576

Environment tab 环境选项卡

The Environment tab can be used to specify a newline-separated list of environment variables to pass to the application. If the Replace default environment checkbox is checked the variables specified will be the only ones passed to the service. When it is left unchecked (the default), the environment created at service startup will be preserved.
“环境”选项卡可用于指定要传递给应用程序的以换行符分隔的环境变量列表。如果选中“替换默认环境”复选框,则指定的变量将是唯一传递给服务的变量。如果未选中(默认),将保留服务启动时创建的环境。
image.jpg
Equivalent commands: 等效命令:
nssm set AppEnvironmentExtra JAVA_HOME=C:\java

Installing from the command line

从命令行安装
As of version 2.0 you can also bypass the GUI and install a service from the command line. The syntax is:
从版本 2.0 开始,您还可以绕过 GUI 并从命令行安装服务。语法是:
nssm install []
Please note that the actual program entered into the services database is nssm itself so you must not move or delete nssm.exe after installing a service. If you do wish to change the path to nssm.exe you can either remove and reinstall the service or edit HKLM\System\CurrentControlSet\Services_servicename_\ImagePath to reflect the new location.
请注意,输入服务数据库的实际程序是 nssm 本身,因此安装服务后不得移动或删除 nssm.exe。如果您确实希望更改 nssm.exe 的路径,可以删除并重新安装该服务,或编辑 HKLM\System\CurrentControlSet\Services\servicename\ImagePath 以反映新位置。

Quoting issues 引用问题

nssm correctly handles paths with spaces but passing arguments to it can be tricky because of how the command prompt works.
nssm 可以正确处理带有空格的路径,但由于命令提示符的工作方式,向其传递参数可能会很棘手。
If the path to the application contains spaces you will need to enclose it in quotes otherwise the command prompt will interpret the path as two arguments.
如果应用程序的路径包含空格,则需要将其用引号引起来,否则命令提示符会将路径解释为两个参数。
nssm install “C:\Program Files\app.exe”
If one of the options you wish to provide contains spaces, you will need to quote that too and quote the quotation marks themselves.
如果您希望提供的选项之一包含空格,您也需要引用该选项并引用引号本身。
nssm install “”“This is one argument”“”
Isaballa Sanfelipo suggests a method of installing a Java application from a batch file.
Isabala Sanfelipo 建议一种从批处理文件安装 Java 应用程序的方法。

nssm install solr "%JavaExe%" -Dsolr.solr.home="\"%CD%\solr"\"
-Djetty.home="\"%CD%"\" -Djetty.logs="\"%CD%\logs"\" -cp
"\"%CD%\lib\*.jar"\";"\"%CD%\start.jar"\" -jar "\"%CD%\start.jar"\"

John Duffy needed to pass quotes through to the parameter list.
John Duffy 需要将引号传递到参数列表。
nssm set NodeServer3000 AppParameters “”“”“”“$Env:NODE_JS_NPM”“”“”" start"

Removing a service 删除服务

The command to remove a service is:
删除服务的命令是:
nssm remove
image.jpg
A confirmation window is displayed before the service is removed.
删除服务之前会显示确认窗口。
image.jpg
As of version 2.0 you can also remove services from the command line viz:
从版本 2.0 开始,您还可以从命令行删除服务,即:
nssm remove confirm
nssm will happily try to remove any service, not just ones nssm itself manages. Try not to delete services you shouldn’t…
nssm 会很乐意尝试删除任何服务,而不仅仅是 nssm 本身管理的服务。尽量不要删除不应该的服务…

Service shutdown 服务关闭

When nssm receives a stop command from the Windows service manager, or when it detects that the monitored application has exited, it tries to shut down the monitored application, and any subprocesses, gracefully. If the application’s process tree does not exit promptly, nssm can forcibly terminate all processes and subprocesses belonging to the application.
当 nssm 收到来自 Windows 服务管理器的停止命令时,或者当它检测到受监视的应用程序已退出时,它会尝试正常关闭受监视的应用程序以及任何子进程。如果应用程序的进程树没有及时退出,nssm可以强制终止属于该应用程序的所有进程和子进程。
There are four stages which nssm can use to shut down the application, and by default it will attempt all four in order. It is possible (though not recommended) to disable some or all of the methods from being used. Different applications will respond differently to the various requests, so leaving them all enabled is usually the best way to ensure that the application shuts down gracefully.
nssm 可以使用四个阶段来关闭应用程序,默认情况下它将按顺序尝试所有四个阶段。可以(尽管不推荐)禁用部分或全部方法。不同的应用程序对各种请求的响应不同,因此将它们全部启用通常是确保应用程序正常关闭的最佳方法。
First nssm will attempt to generate a Control-C event and send it to the application’s console. Batch scripts or console applications may intercept the event and shut themselves down gracefully. Java applications tend to respond well to Control-C events. GUI applications do not have consoles and will not respond to this method. Not supported on Windows 2000.
首先,nssm 将尝试生成 Control-C 事件并将其发送到应用程序的控制台。批处理脚本或控制台应用程序可能会拦截该事件并正常关闭自己。 Java 应用程序往往能够很好地响应 Control-C 事件。 GUI 应用程序没有控制台,因此不会响应此方法。 Windows 2000 不支持。
Secondly nssm will enumerate all windows created by the application and send them a WM_CLOSE message. Applications may follow the convention of responding to the message by initiating a graceful exit.
其次,nssm 将枚举应用程序创建的所有窗口并向它们发送 WM_CLOSE 消息。应用程序可以遵循通过启动正常退出来响应消息的约定。
Thirdly nssm will enumerate all threads created by the application and send them a WM_QUIT message, which will be received if the application has a thread message queue.
第三,nssm将枚举应用程序创建的所有线程并向它们发送WM_QUIT消息,如果应用程序有线程消息队列,则该消息将被接收。
As a last resort nssm can call TerminateProcess() to request that the operating system forcibly terminate the application. The TerminateProcess() call cannot be trapped or ignored, so in most circumstances the application will be killed. However, it is unlikely that it will be able to perform any cleanup operations before it exits.
作为最后的手段,nssm 可以调用 TerminateProcess() 来请求操作系统强制终止应用程序。 TerminateProcess() 调用无法被捕获或忽略,因此在大多数情况下应用程序将被终止。但是,它不太可能在退出之前执行任何清理操作。
To disable any of the methods above, create an integer (REG_DWORD) value HKLM\System\CurrentControlSet\Services_servicename_\Parameters\AppStopMethodSkip and set it to the sum of one or more of the numbers below.
要禁用上述任一方法,请创建一个整数 (REG_DWORD) 值 HKLM\System\CurrentControlSet\Services\servicename\Parameters\AppStopMethodSkip 并将其设置为以下一个或多个数字的总和。

  • 1 - Don’t send Control-C to the console.
    1 - 不要将 Control-C 发送到控制台。
  • 2 - Don’t send WM_CLOSE to windows.
    2 - 不要向窗口发送 WM_CLOSE。
  • 4 - Don’t send WM_QUIT to threads.
    4 - 不要向线程发送 WM_QUIT。
  • 8 - Don’t call TerminateProcess().
    8 - 不要调用 TerminateProcess()。

If, for example, you knew that an application did not respond to Control-C and did not have a thread message queue, you could set AppStopMethodSkip to 5.
例如,如果您知道应用程序没有响应 Control-C 并且没有线程消息队列,则可以将 AppStopMethodSkip 设置为 5。
It is highly recommended not to disable the TerminateProcess() call. When the service is stopped nssm will exit. If the application is not terminated before that happens it may continue running and nssm will no longer be able to control it.
强烈建议不要禁用 TerminateProcess() 调用。当服务停止时 nssm 将退出。如果应用程序在此之前没有终止,它可能会继续运行,并且 nssm 将无法再控制它。
By default nssm will wait up to 1500 milliseconds for the application to exit after trying each of the methods described above. The timeout can be configured on a per-method basis by creating integer (REG_DWORD) values under HKLM\System\CurrentControlSet\Services_servicename_\Parameters in the registry and setting them to the desired number of milliseconds to wait.
默认情况下,在尝试上述每种方法后,nssm 将等待最多 1500 毫秒,以便应用程序退出。可以通过在注册表中的 HKLM\System\CurrentControlSet\Services\servicename\Parameters 下创建整数 (REG_DWORD) 值并将其设置为所需等待的毫秒数,针对每个方法配置超时。

  • AppStopMethodConsole - Time to wait after sending Control-C.
    AppStopMethodConsole - 发送 Control-C 后等待的时间。
  • AppStopMethodWindow - Time to wait after sending WM_CLOSE.
    AppStopMethodWindow - 发送 WM_CLOSE 后等待的时间。
  • AppStopMethodThreads - Time to wait after sending WM_QUIT.
    AppStopMethodThreads - 发送 WM_QUIT 后等待的时间。

Note that the timeout applies to all processes spawned by the application so the total timeout may be longer than expected if the application has multiple subprocesses.
请注意,超时适用于应用程序生成的所有进程,因此如果应用程序有多个子进程,总超时可能会比预期长。

Actions on exit 退出时的操作

To configure the action which nssm should take when the application exits, edit the default value of the key HKLM\System\CurrentControlSet\Services_servicename_\Parameters\AppExit. If the key does not exist in the registry when nssm runs it will create it and set the value to Restart. Change it to either Ignore or Exit to specify the action taken. nssm will only create this key if it doesn’t already exist. Your changes will not be overridden.
要配置应用程序退出时 nssm 应采取的操作,请编辑 HKLM\System\CurrentControlSet\Services\servicename\Parameters\AppExit 项的默认值。如果 nssm 运行时注册表中不存在该键,它将创建该键并将值设置为“重新启动”。将其更改为“忽略”或“退出”以指定所采取的操作。仅当此密钥尚不存在时,nssm 才会创建该密钥。您的更改不会被覆盖。
To specify a different action for particular exit codes, create a string (REG_SZ) value underneath the AppExit key whose name is the exit code being considered. For example to stop the service on an exit code of 0 (which usually means the application finished successfully), create HKLM\System\CurrentControlSet\Services_servicename_\Parameters\AppExit\0 and set it to Exit. Look in the Event Log for messages from nssm to see what exit codes are returned by your application.
要为特定退出代码指定不同的操作,请在 AppExit 键下创建一个字符串 (REG_SZ) 值,其名称是正在考虑的退出代码。例如,要在退出代码为 0 时停止服务(这通常意味着应用程序成功完成),请创建 HKLM\System\CurrentControlSet\Services\servicename\Parameters\AppExit\0 并将其设置为 Exit。在事件日志中查找来自 nssm 的消息,以了解应用程序返回的退出代码。
If your application’s exit code does not correspond to a registry entry, nssm will use the default value of AppExit when deciding what to do.
如果应用程序的退出代码与注册表项不对应,则 nssm 在决定要执行的操作时将使用 AppExit 的默认值。

Restart delay 重启延迟

As of version 2.22, nssm can apply a mandatory delay between restarts of the application. This could be used, for example, to run a command at regular intervals such as an hourly batch script.
从版本 2.22 开始,nssm 可以在应用程序重新启动之间应用强制延迟。例如,这可以用于定期运行命令,例如每小时的批处理脚本。
To specify a restart delay, create an integer (REG_DWORD) value HKLM\System\CurrentControlSet\Services_servicename_\Parameters\AppRestartDelay and set it to the number of milliseconds to wait between restarts.
要指定重新启动延迟,请创建一个整数 (REG_DWORD) 值 HKLM\System\CurrentControlSet\Services\servicename\Parameters\AppRestartDelay 并将其设置为重新启动之间等待的毫秒数。
The service will report its state as Paused while waiting for the next restart. Sending it a Continue control will temporarily cancel the delay and trigger a restart immediately.
在等待下一次重新启动时,该服务将报告其状态为“已暂停”。向其发送“继续”控件将暂时取消延迟并立即触发重新启动。
Please see the section on restart throttling below for notes on how nssm works when both throttling and restart delays are configured.
请参阅下面有关重新启动限制的部分,了解有关配置限制和重新启动延迟时 nssm 如何工作的说明。

Restart throttling 重启节流

To avoid a tight CPU loop, nssm will throttle restarts of the service if the monitored application exits too soon after starting. By default a threshold of 1500 milliseconds is used. To specify a different value, create an integer (REG_DWORD) value HKLM\System\CurrentControlSet\Services_servicename_\Parameters\AppThrottle and set it to the required number of milliseconds.
为了避免紧密的 CPU 循环,如果受监控的应用程序在启动后过早退出,nssm 将限制服务的重新启动。默认情况下,使用 1500 毫秒的阈值。要指定不同的值,请创建一个整数 (REG_DWORD) 值 HKLM\System\CurrentControlSet\Services\servicename\Parameters\AppThrottle 并将其设置为所需的毫秒数。
The first restart will be attempted with no delay. If the restarted application continues to exit before running for the threshold amount of milliseconds, nssm will pause for at least 2000 milliseconds, doubling the pause time for each subsequent failure. The maximum time it will pause is 256000 milliseconds, around four minutes. The delay counter is reset when the service successfully runs for at least the threshold time.
将立即尝试第一次重新启动。如果重新启动的应用程序在运行阈值毫秒数之前继续退出,则 nssm 将暂停至少 2000 毫秒,使每次后续故障的暂停时间加倍。暂停的最长时间为 256000 毫秒,大约四分钟。当服务成功运行至少阈值时间时,延迟计数器将重置。
If you determine why the service failed and take action to correct the problem, you can send a Continue control to the service, which will be shown as Paused. In this way you can avoid having to wait for the next restart attempt.
如果您确定服务失败的原因并采取措施纠正问题,则可以向服务发送“继续”控件,该服务将显示为“已暂停”。通过这种方式,您可以避免等待下一次重新启动尝试。
When a restart delay is configured and the application exits prematurely, nssm will throttle the restart by whichever is longer of the configured delay and calculated throttle time. If, for instance, you configured a restart delay of 3000 milliseconds and the service failed on each startup, the first restart attempt would be delayed by 3000 milliseconds because 3000ms configured is longer than 0ms from throttling. The second attempt would also be delayed by 3000 milliseconds; 3000ms configured is again longer than 2000ms from throttling. The third attempt would be delayed by 4000 milliseconds; longer than the configured 3000ms.
当配置了重新启动延迟并且应用程序过早退出时,nssm 将以配置的延迟和计算的限制时间中较长者为准来限制重新启动。例如,如果您配置了 3000 毫秒的重新启动延迟,并且服务在每次启动时都失败,则第一次重新启动尝试将延迟 3000 毫秒,因为配置的 3000 毫秒比限制时间长于 0 毫秒。第二次尝试也会延迟 3000 毫秒;配置的 3000ms 再次比节流的 2000ms 长。第三次尝试将延迟 4000 毫秒;超过配置的3000ms。
For this reason, if you intend to use a restart delay to configure a short-running service which repeats at an interval less than five minutes, you should consider lowering the value of AppThrottle.
因此,如果您打算使用重新启动延迟来配置重复间隔小于五分钟的短时运行服务,则应考虑降低 AppThrottle 的值。

Process priority and CPU affinity

进程优先级和CPU亲和性
As of version 2.22, nssm can manage the CPU affinity and process priority of the managed application.
从版本 2.22 开始,nssm 可以管理托管应用程序的 CPU 关联性和进程优先级。
By default application will be launched with normal process priority and be allowed to execute on any CPU. nssm will look under HKLM\System\CurrentControlSet\Services_servicename_\Parameters for registry entries to configure the application startup.
默认情况下,应用程序将以正常进程优先级启动,并允许在任何 CPU 上执行。 nssm 将在 HKLM\System\CurrentControlSet\Services\servicename\Parameters 下查找注册表项以配置应用程序启动。
If the integer (REG_DWORD) value AppPriority is set, nssm will interpret its value as an argument to SetPriorityClass() and start the application with the specified priority.
如果设置了整数 (REG_DWORD) 值 AppPriority,nssm 会将其值解释为 SetPriorityClass() 的参数,并以指定的优先级启动应用程序。
If the string (REG_SZ) value AppAffinity is set, nssm will interpret it as a comma-separated list of CPU IDs, starting from 0 on which the application can run. Alternatively, a range of IDs may be specified by separating the indices with a dash.
如果设置了字符串 (REG_SZ) 值 AppAffinity,nssm 会将其解释为以逗号分隔的 CPU ID 列表,从 0 开始,应用程序可以在其上运行。或者,可以通过用破折号分隔索引来指定 ID 范围。
Only digits, dashes and commas are valid in an affinity string.
在关联字符串中只有数字、破折号和逗号有效。
For example, the the string 0-2,4 specifies that the application may run on the first, second, third and fifth CPUs in the system.
例如,字符串0-2,4指定应用程序可以在系统中的第一、第二、第三和第五CPU上运行。

Console window 控制台窗口

As of version 2.22, nssm will by default create a new console window for the application. This allows some programs to work which would otherwise fail, such as those which expect to be able to read user input. The console window can be disabled if it is not needed by setting the integer (REG_DWORD) value AppNoConsole under HKLM\System\CurrentControlSet\Services_servicename_\Parameters to a non-zero value.
从版本 2.22 开始,nssm 默认情况下将为应用程序创建一个新的控制台窗口。这使得一些本来会失败的程序能够工作,例如那些期望能够读取用户输入的程序。如果不需要,可以通过将 HKLM\System\CurrentControlSet\Services\servicename\Parameters 下的整数 (REG_DWORD) 值 AppNoConsole 设置为非零值来禁用控制台窗口。

I/O redirection I/O 重定向

nssm can redirect the managed application’s I/O to any path capable of being opened by CreateFile(). This feature may be useful if your application expects to be able to log to the console.
nssm 可以将托管应用程序的 I/O 重定向到 CreateFile() 能够打开的任何路径。如果您的应用程序希望能够登录到控制台,则此功能可能很有用。
nssm will look under HKLM\System\CurrentControlSet\Services_servicename_\Parameters for keys corresponding to arguments to CreateFile(). All are optional. If no path is given for a particular stream it will not be redirected. If a path is given but any of the other values they will receive sensible defaults.
nssm 将在 HKLM\System\CurrentControlSet\Services\servicename\Parameters 下查找与 CreateFile() 参数对应的键。全部都是可选的。如果没有为特定流指定路径,则不会重定向该流。如果给出了路径但给出了任何其他值,它们将收到合理的默认值。

  • AppStdin (string) - Path to receive input.
    AppStdin(字符串)- 接收输入的路径。
  • AppStdinShareMode (integer) - ShareMode argument for the input.
    AppStdinShareMode(整数)- 输入的 ShareMode 参数。
  • AppStdinCreationDisposition (integer) - CreationDisposition argument for the input.
    AppStdinCreationDisposition(整数)- 输入的 CreationDisposition 参数。
  • AppStdinFlagsAndAttributes (integer) - FlagsAndAttributes argument for the input.
    AppStdinFlagsAndAttributes(整数)- 输入的 FlagsAndAttributes 参数。
  • AppStdout (string) - Path to receive output.
    AppStdout(字符串)- 接收输出的路径。
  • AppStdoutShareMode (integer) - ShareMode argument for the output.
    AppStdoutShareMode(整数)- 输出的 ShareMode 参数。
  • AppStdoutCreationDisposition (integer) - CreationDisposition argument for the output.
    AppStdoutCreationDisposition(整数) - 输出的 CreationDisposition 参数。
  • AppStdoutFlagsAndAttributes (integer) - FlagsAndAttributes argument for the output.
    AppStdoutFlagsAndAttributes(整数) - 输出的 FlagsAndAttributes 参数。
  • AppStderr (string) - Path to receive error output.
    AppStderr(字符串)- 接收错误输出的路径。
  • AppStderrShareMode (integer) - ShareMode argument for the error output.
    AppStderrShareMode(整数) - 错误输出的 ShareMode 参数。
  • AppStderrCreationDisposition (integer) - CreationDisposition argument for the error output.
    AppStderrCreationDisposition(整数) - 错误输出的 CreationDisposition 参数。
  • AppStderrFlagsAndAttributes (integer) - FlagsAndAttributes argument for the error output.
    AppStderrFlagsAndAttributes(整数) - 错误输出的 FlagsAndAttributes 参数。

In general it is advisable to set both AppStdout and AppStderr in order to log output, as applications may log informational and error messages separately.
一般来说,建议同时设置 AppStdout 和 AppStderr 以便记录输出,因为应用程序可能会分别记录信息消息和错误消息。
It is possible to direct both stderr and stdout to the same path but due to a limitation with nssm you must provide the exact same string in both the AppStdout and AppStderr registry values. Only if the two entries are the same will nssm be able to interleave the two streams.
可以将 stderr 和 stdout 定向到同一路径,但由于 nssm 的限制,您必须在 AppStdout 和 AppStderr 注册表值中提供完全相同的字符串。只有当两个条目相同时,nssm 才能交错两个流。

File rotation 文件轮换

As of version 2.22, if I/O redirection is enabled, nssm can rotate existing output files prior to launching the application. nssm can also rotate files while the service is running. See online rotation below.
从版本 2.22 开始,如果启用了 I/O 重定向,nssm 可以在启动应用程序之前轮换现有输出文件。 nssm 还可以在服务运行时轮换文件。请参阅下面的在线轮换。
To enable rotation, create an integer (REG_DWORD) value HKLM\System\CurrentControlSet\Services_servicename_\Parameters\AppRotate and set it to 1. Before (re)starting the service, nssm will rotate the file(s) configured in AppStdout and/or AppStderr if they already exist.
要启用轮换,请创建一个整数 (REG_DWORD) 值 HKLM\System\CurrentControlSet\Services\servicename\Parameters\AppRotate 并将其设置为 1。在(重新)启动服务之前,nssm 将轮换在 AppStdout 中配置的文件,并且/或AppStderr(如果它们已经存在)。
Existing files will be renamed according to a template which uses the last modification time of the file. For example, C:\Services\myservice.log might be rotated to C:\Services\myservice-20140114T180840.953.log.
现有文件将根据使用文件上次修改时间的模板进行重命名。例如,C:\Services\myservice.log 可能会轮换到 C:\Services\myservice-20140114T180840.953.log。
Note that the timestamp is in ISO8601 format, so a list of rotated files sorted by name will show the oldest file first, and that it includes a millisecond part, so that files will not be lost if the service exits and is restarted in less than a second.
请注意,时间戳采用 ISO8601 格式,因此按名称排序的轮换文件列表将首先显示最旧的文件,并且它包含毫秒部分,因此如果服务退出并在不到 5 秒内重新启动,文件不会丢失一秒。
Two additional registry settings under HKLM\System\CurrentControlSet\Services_servicename_\Parameters can be used to tweak how nssm rotates files.
HKLM\System\CurrentControlSet\Services\servicename\Parameters 下的两个附加注册表设置可用于调整 nssm 轮换文件的方式。
If the integer (REG_DWORD) value AppRotateSeconds is set, nssm will not rotate any file which was last modified less than the configured number of seconds ago.
如果设置了整数 (REG_DWORD) 值 AppRotateSeconds,nssm 将不会轮换上次修改时间早于配置的秒数的任何文件。
If the integer (REG_DWORD) value AppRotateBytes is set, nssm will not rotate any file which is smaller than the configured number of bytes. nssm can also handle files whose size is too large to be expressed in 32-bits, in case you decide that it’s fine for a log file to grow to 4GB in size but any more is just too big. The value of AppRotateBytesHigh will be interpreted as the high order part of a 64-bit size.
如果设置了整数 (REG_DWORD) 值 AppRotateBytes,nssm 将不会旋转任何小于配置字节数的文件。 nssm 还可以处理大小太大而无法用 32 位表示的文件,以防您认为日志文件大小可以增长到 4GB,但再大就太大了。 AppRotateBytesHigh 的值将被解释为 64 位大小的高位部分。
If both AppRotateSeconds and AppRotateBytes(High) are set, nssm will require that both criteria are met for a file to be rotated.
如果同时设置了 AppRotateSeconds 和 AppRotateBytes(High),则 nssm 将要求满足两个条件才能轮换文件。

Online rotation 在线轮换

If the integer (REG_DWORD) value AppRotateOnline is set to 1, nssm can rotate files which grow to the configured file size limit while the service is running. The value of AppRotateSeconds is ignored for the purposes of online rotation, though it will still apply for rotation before the service (re)starts.
如果整数 (REG_DWORD) 值 AppRotateOnline 设置为 1,则 nssm 可以轮换在服务运行时增长到配置的文件大小限制的文件。出于在线轮换的目的,AppRotateSeconds 的值将被忽略,但在服务(重新)启动之前它仍会申请轮换。
AppRotateOnline is ignored if AppRotate is not set.
如果未设置 AppRotate,AppRotateOnline 将被忽略。
When online rotation is enabled, nssm reads the application’s stdout and/or stderr and writes the output files itself. Doing so introduces a degree of complexity compared to simple I/O redirection, so should not be used for services which do not require it. Although nssm will try to handle I/O errors gracefully, if something goes wrong it is possible for output from the application to be lost until the service is restarted. See the technical discussion for more details about how nssm handles I/O redirection.
启用在线轮换后,nssm 读取应用程序的 stdout 和/或 stderr 并自行写入输出文件。与简单的 I/O 重定向相比,这样做会带来一定程度的复杂性,因此不应将其用于不需要它的服务。尽管 nssm 会尝试妥善处理 I/O 错误,但如果出现问题,应用程序的输出可能会丢失,直到服务重新启动为止。有关 nssm 如何处理 I/O 重定向的更多详细信息,请参阅技术讨论。

On-demand rotation 按需轮换

nssm can rotate output files on-demand, regardless of whether or not they have hit the configured size limit. To request file rotation for a service, send user-defined service control 128 or run the command:
nssm 可以按需轮换输出文件,无论它们是否达到配置的大小限制。要请求服务的文件轮换,请发送用户定义的服务控制 128 或运行以下命令:
nssm rotate
A limitation of on-demand rotation is that the actual file renaming won’t happen until after the next line of input has been read from the application. For that reason there may be a considerable delay between issuing the rotation request and the rotation taking place, depending on how verbose the application is.
按需轮换的一个限制是,实际的文件重命名只有在从应用程序读取下一行输入之后才会发生。因此,发出轮换请求和轮换发生之间可能存在相当大的延迟,具体取决于应用程序的详细程度。
On-demand rotation will work even if AppRotateBytes is not configured, ie if the service would not rotate files while running. It will not work, however, unless both AppRotate and AppRotateOnline are configured.
即使未配置 AppRotateBytes,即如果服务在运行时不会轮换文件,按需轮换也将起作用。但是,除非同时配置了 AppRotate 和 AppRotateOnline,否则它将无法工作。

I/O redirection technical details

I/O 重定向技术细节
There are three cases to consider when looking at how a service handles I/O.
在查看服务如何处理 I/O 时需要考虑三种情况。

No redirection 无重定向

In the simplest case, nssm is not configured with any I/O redirection. It will launch the application with stdin, stdout and stderr connected to a console instance. If the service runs under the LOCALSYSTEM account and is configured to interact with the desktop, you may be able to view the output directly.
在最简单的情况下,nssm 未配置任何 I/O 重定向。它将启动应用程序,并将 stdin、stdout 和 stderr 连接到控制台实例。如果该服务在 LOCALSYSTEM 帐户下运行并配置为与桌面交互,您也许可以直接查看输出。

I/O redirected; online rotation disabled

I/O 重定向;在线轮换已禁用
If stdout and/or stderr are redirected and online rotation is disabled, nssm will call CreateFile() to open a handle for each I/O stream, then call DuplicateHandle() to set a copy of the handle in the STARTUPINFO datastructure passed to CreateProcess(). Thus the application will run with an open handle to the file whereas nssm itself has no open handles.
如果 stdout 和/或 stderr 被重定向并且在线轮换被禁用,nssm 将调用 CreateFile() 为每个 I/O 流打开一个句柄,然后调用 DuplicateHandle() 设置该句柄的副本传递给 CreateProcess() 的 STARTUPINFO 数据结构中的句柄。因此,应用程序将使用文件的打开句柄运行,而 nssm 本身没有打开句柄。

I/O redirected; online rotation enabled

I/O 重定向;启用在线轮换
This case is by far the most complex of the three and is thus necessarily most at risk of something going wrong, potentially resulting in the loss of output from the application.
这种情况是迄今为止三种情况中最复杂的,因此必然最有可能出现问题,从而可能导致应用程序输出丢失。
nssm first calls CreateFile() just as in the previous case. It then calls CreatePipe() to open an anonymous pipe. The reading end of the pipe and the handle to the output file are passed to a new thread which does the actual writing. The writing end of the pipe is duplicated with DuplicateHandle() for passing to CreateProcess(). Thus the application will run with an open handle to one end of the pipe and nssm will run with a handle to the other end of the pipe and a handle to the output file.
nssm 首先调用 CreateFile() ,就像前面的情况一样。然后它调用 CreatePipe() 打开一个匿名管道。管道的读取端和输出文件的句柄被传递给执行实际写入操作的新线程。管道的写入端与 DuplicateHandle() 重复以传递给 CreateProcess() 。因此,应用程序将使用管道一端的打开句柄运行,而 nssm 将使用管道另一端的句柄和输出文件的句柄运行。
The writing thread runs a simple loop wherein it reads data from the pipe, ie the application’s output, into a buffer with ReadFile() then writes the contents of that buffer to the output file with WriteFile(). If the file hits the configured size limit the thread will write up to the next newline, close its output handle, rotate the file and open a new handle to continue writing to the new file.
写入线程运行一个简单的循环,其中使用 ReadFile() 从管道(即应用程序的输出)读取数据到缓冲区,然后使用 WriteFile() 将该缓冲区的内容写入输出文件。如果文件达到配置的大小限制,线程将写入到下一个换行符,关闭其输出句柄,旋转文件并打开一个新句柄以继续写入新文件。
When nssm receives an on-demand rotation request it will set a flag which instructs the writing thread to perform a rotation after the next ReadFile() call completes, regardless of the file size. Because ReadFile() blocks execution until something is read, nssm won’t actually get round to rotating the file until the application produces the next line of output.
当 nssm 收到按需轮换请求时,它将设置一个标志,指示写入线程在下一个 ReadFile() 调用完成后执行轮换,无论文件大小如何。由于 ReadFile() 会阻止执行,直到读取某些内容为止,因此 nssm 实际上不会轮换文件,直到应用程序生成下一行输出。
nssm doesn’t know whether the output from the application will be Unicode or ANSI, so prior to writing the first data to the file, or to rotating, it calls IsTextUnicode() to try to determine the text encoding that’s in use. If it (looks like it) is Unicode, nssm writes a UTF-16 byte-order marker to the beginning of the new file so that it will be read correctly when opened with a text editor.
nssm 不知道应用程序的输出是 Unicode 还是 ANSI,因此在将第一个数据写入文件或旋转之前,它会调用 IsTextUnicode() 来尝试确定中的文本编码使用。如果它(看起来像)是 Unicode,则 nssm 会将 UTF-16 字节顺序标记写入新文件的开头,以便在使用文本编辑器打开时可以正确读取该文件。
If both stdout and stderr are redirected, and to separate files, nssm will spawn a writing thread for each. nssm will deal with an application which writes stdout in Unicode and stderr in ANSI - or vice versa - if you are unfortunate (or evil) enough to run one.
如果 stdout 和 stderr 都被重定向并分离到文件中,则 nssm 将为每个文件生成一个写入线程。 nssm 将处理一个以 Unicode 编写 stdout 并以 ANSI 编写 stderr 的应用程序 - 反之亦然 - 如果您不幸(或邪恶)运行一个应用程序。
It should be clear from the above that there are a number of pitfalls to online rotation. nssm will try to deal gracefully with any problems during the I/O process but to be safe you should consider not using online rotation unless your output is so voluminous that you have no choice.
从上面应该可以清楚地看出,在线轮换存在许多陷阱。 nssm 将尝试优雅地处理 I/O 过程中的任何问题,但为了安全起见,您应该考虑不要使用在线轮换,除非您的输出量太大而您别无选择。

Environment variables 环境变量

As of version 2.11, nssm respects the AppEnvironment registry value supported by srvany. To specify a list of environment variables to pass to the monitored application, create a multi-valued string (REG_MULTI_SZ) value HKLM\System\CurrentControlSet\Services_servicename_\Parameters\AppEnvironment where each entry is of the form KEY=VALUE.
从版本 2.11 开始,nssm 遵循 srvany 支持的 AppEnvironment 注册表值。要指定要传递到受监视应用程序的环境变量列表,请创建一个多值字符串 (REG_MULTI_SZ) 值 HKLM\System\CurrentControlSet\Services\servicename\Parameters\AppEnvironment,其中每个条目的格式为 KEY=VALUE。
It is possible to omit the VALUE if you just want the environment variable KEY to exist but the = symbol is mandatory. The service will not run if an invalid environment is specified!
如果您只希望环境变量 KEY 存在,但 = 符号是必需的,则可以省略 VALUE。如果指定的环境无效,服务将不会运行!
As of version 2.19, nssm also respects the AppEnvironmentExtra registry value, which should have the same format as AppEnvironment. Environment variables set in AppEnvironmentExtra will be added to the service’s default environment.
从版本 2.19 开始,nssm 还遵循 AppEnvironmentExtra 注册表值,该值应与 AppEnvironment 具有相同的格式。 AppEnvironmentExtra 中设置的环境变量将添加到服务的默认环境中。
For compatibility with srvany, the environment variables specified in AppEnvironment will replace those set by the system at service startup. Since that probably isn’t what you want, use AppEnvironmentExtra instead.
为了与 srvany 兼容,AppEnvironment 中指定的环境变量将替换服务启动时系统设置的环境变量。由于这可能不是您想要的,因此请改用 AppEnvironmentExtra。

来自: NSSM - 非吸式服务管理器 — NSSM - the Non-Sucking Service Manager

  • 31
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值