{"id":4362,"date":"2017-09-19T14:20:20","date_gmt":"2017-09-19T17:20:20","guid":{"rendered":"https:\/\/blog.clusterweb.com.br\/?p=4362"},"modified":"2017-09-19T14:20:20","modified_gmt":"2017-09-19T17:20:20","slug":"nssm-the-non-sucking-service-manager","status":"publish","type":"post","link":"https:\/\/blog.clusterweb.com.br\/?p=4362","title":{"rendered":"NSSM &#8211; the Non-Sucking Service Manager"},"content":{"rendered":"<h3>Usage<\/h3>\n<p>No &#8220;installation&#8221; of\u00a0<em>nssm<\/em>\u00a0is needed. Just place it somewhere on the system (preferably somewhere in your PATH) and run it.<\/p>\n<p>Note however that\u00a0<em>nssm<\/em>\u00a0registers itself as an Event Log message source which means that running multiple instances or different version of\u00a0<em>nssm<\/em>\u00a0from different locations may cause confusion. Also note that if you run the Event Viewer it will open the\u00a0<em>nssm<\/em>\u00a0executable, preventing you from overwriting it. Keep this in mind if you come to upgrade\u00a0<em>nssm<\/em>.<\/p>\n<p><em>Some features are labelled as supported as of a particular version. If the version described is newer than that available from the\u00a0<a href=\"https:\/\/nssm.cc\/download\">download<\/a>\u00a0page there may be a pre-release\u00a0<a href=\"https:\/\/nssm.cc\/builds\">build<\/a>\u00a0with the feature enabled.<\/em><\/p>\n<p><em>Equivalent command<\/em>\u00a0examples below show the\u00a0<a href=\"https:\/\/nssm.cc\/commands\">commands<\/a>\u00a0which would configure an\u00a0<em>existing<\/em>\u00a0service 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<!--more--><\/p>\n<pre class=\"code\">nssm reset &lt;servicename&gt; &lt;parameter&gt;<\/pre>\n<h3>Installing a service<\/h3>\n<p>You can use\u00a0<em>nssm<\/em>\u00a0to install a service. The command to type is:<\/p>\n<pre class=\"code\">nssm install &lt;servicename&gt;<\/pre>\n<p>The installer consists of several tabs with lots of configurable parameters. Most are preset to\u00a0<em>nssm<\/em>&#8216;s defaults, so it&#8217;s possible to install a service without leaving the\u00a0<em>Application<\/em>\u00a0tab.<\/p>\n<h4>Application tab<\/h4>\n<p>The\u00a0<em>Path<\/em>\u00a0to 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\u00a0<em>Startup directory<\/em>\u00a0field. If the field is left blank the default startup directory will be the directory containing the application. The\u00a0<em>Arguments<\/em>\u00a0field can be used to specify any commandline arguments to pass to the application.<\/p>\n<p>The screenshot below shows installation of a\u00a0<a href=\"http:\/\/www.unrealtournament2003.com\/\">UT2003<\/a>\u00a0server. The command to run such a service is\u00a0<strong>ucc server<\/strong>\u00a0so the full path to\u00a0<strong>UCC.exe<\/strong>\u00a0is entered under\u00a0<em>Path<\/em>\u00a0and\u00a0<strong>server<\/strong>\u00a0is entered under\u00a0<em>Options<\/em>.<\/p>\n<p align=\"center\"><img decoding=\"async\" src=\"https:\/\/nssm.cc\/images\/install_application.png\" border=\"0\" \/><\/p>\n<p>Equivalent\u00a0<a href=\"https:\/\/nssm.cc\/commands\">commands<\/a>:<\/p>\n<pre class=\"code\">nssm set UT2003 Application C:\\games\\ut2003\\System\\UCC.exe<\/pre>\n<pre class=\"code\">nssm set UT2003 AppDirectory C:\\games\\ut2003\\System<\/pre>\n<pre class=\"code\">nssm set UT2003 AppParameters server<\/pre>\n<p>Clicking\u00a0<em>Install service<\/em>\u00a0completes the installation of the service.<\/p>\n<h4>Details tab<\/h4>\n<p>The\u00a0<em>Details<\/em>\u00a0tab lists system details about the service.<\/p>\n<p align=\"center\"><img decoding=\"async\" src=\"https:\/\/nssm.cc\/images\/install_details.png\" border=\"0\" \/><\/p>\n<p>Equivalent\u00a0<a href=\"https:\/\/nssm.cc\/commands\">commands<\/a>:<\/p>\n<pre class=\"code\">nssm set UT2003 DisplayName UT2k3<\/pre>\n<pre class=\"code\">nssm set UT2003 Description Unreal Tournament 2003<\/pre>\n<pre class=\"code\">nssm set UT2003 Start SERVICE_AUTO_START<\/pre>\n<h4>Log on tab<\/h4>\n<p>The\u00a0<em>Log on<\/em>\u00a0tab can be used to manage the user account which will run the service.\u00a0<em>nssm<\/em>\u00a0will automatically ensure that the account you choose has the necessary\u00a0<em>log on as a service<\/em>\u00a0permissions.<\/p>\n<p align=\"center\"><img decoding=\"async\" src=\"https:\/\/nssm.cc\/images\/install_logon.png\" border=\"0\" \/><\/p>\n<p>Equivalent\u00a0<a href=\"https:\/\/nssm.cc\/commands\">commands<\/a>:<\/p>\n<pre class=\"code\">nssm set UT2003 ObjectName LocalSystem<\/pre>\n<pre class=\"code\">nssm set UT2003 Type SERVICE_WIN32_OWN_PROCESS<\/pre>\n<p>Refer to the\u00a0<a href=\"https:\/\/nssm.cc\/commands#exceptions\">command line usage<\/a>\u00a0documentation for details on configuring an account and password on the command line. If you need to configure a blank password you\u00a0<em>must<\/em>\u00a0use the command line.<\/p>\n<h4>Dependencies<\/h4>\n<p>The\u00a0<em>Dependencies<\/em>\u00a0tab lists any services or service groups which must be started before the the service can run.<\/p>\n<p>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).<\/p>\n<p align=\"center\"><img decoding=\"async\" src=\"https:\/\/nssm.cc\/images\/install_dependencies.png\" border=\"0\" \/><\/p>\n<p>Equivalent\u00a0<a href=\"https:\/\/nssm.cc\/commands\">commands<\/a>:<\/p>\n<pre class=\"code\">nssm set UT2003 DependOnService MpsSvc<\/pre>\n<h4>Process tab<\/h4>\n<p>The\u00a0<em>Process<\/em>\u00a0tab can be used to set\u00a0<a href=\"https:\/\/nssm.cc\/usage#process\">process<\/a>\u00a0priority 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 &#8220;All processors&#8221; and select the CPU(s) as desired.<\/p>\n<p>Process priority and affinity can be changed from the Windows task manager while the service is running.<\/p>\n<p align=\"center\"><img decoding=\"async\" src=\"https:\/\/nssm.cc\/images\/install_process.png\" border=\"0\" \/><\/p>\n<p>Equivalent\u00a0<a href=\"https:\/\/nssm.cc\/commands\">commands<\/a>:<\/p>\n<pre class=\"code\">nssm set UT2003 AppPriority NORMAL_PRIORITY_CLASS<\/pre>\n<pre class=\"code\">nssm set UT2003 AppNoConsole 0<\/pre>\n<pre class=\"code\">nssm set UT2003 AppAffinity All<\/pre>\n<h4>Shutdown tab<\/h4>\n<p>The\u00a0<em>Shutdown<\/em>\u00a0tab lists the various\u00a0<a href=\"https:\/\/nssm.cc\/usage#shutdown\">stop methods and timeouts<\/a>\u00a0used when tidying up the application after a crash or when the service is gracefully stopped.<\/p>\n<p align=\"center\"><img decoding=\"async\" src=\"https:\/\/nssm.cc\/images\/install_shutdown.png\" border=\"0\" \/><\/p>\n<p>Equivalent\u00a0<a href=\"https:\/\/nssm.cc\/commands\">commands<\/a>:<\/p>\n<pre class=\"code\">nssm set UT2003 AppStopMethodSkip 0<\/pre>\n<pre class=\"code\">nssm set UT2003 AppStopMethodConsole 1500<\/pre>\n<pre class=\"code\">nssm set UT2003 AppStopMethodWindow 1500<\/pre>\n<pre class=\"code\">nssm set UT2003 AppStopMethodThreads 1500<\/pre>\n<h4>Exit actions tab<\/h4>\n<p>The\u00a0<em>Exit actions<\/em>\u00a0tab can be used to tweak the\u00a0<a href=\"https:\/\/nssm.cc\/usage#throttling\">restart throttling<\/a>\u00a0and default\u00a0<a href=\"https:\/\/nssm.cc\/usage#exit\">action on exit<\/a>\u00a0for the service. You can also specify a\u00a0<a href=\"https:\/\/nssm.cc\/usage#delay\">mandatory delay<\/a>\u00a0between automatic restarts of the application.<\/p>\n<p>To configure exit actions for specific application exit codes you must use the registry as described\u00a0<a href=\"https:\/\/nssm.cc\/usage#exit\">below<\/a>.<\/p>\n<p align=\"center\"><img decoding=\"async\" src=\"https:\/\/nssm.cc\/images\/install_exitactions.png\" border=\"0\" \/><\/p>\n<p>Equivalent\u00a0<a href=\"https:\/\/nssm.cc\/commands\">commands<\/a>:<\/p>\n<pre class=\"code\">nssm set UT2003 AppThrottle 1500<\/pre>\n<pre class=\"code\">nssm set UT2003 AppExit Default Restart<\/pre>\n<pre class=\"code\">nssm set UT2003 AppRestartDelay 0<\/pre>\n<h4>I\/O tab<\/h4>\n<p>The\u00a0<em>I\/O<\/em>\u00a0tab can be used to specify the input and\/or output files used when\u00a0<a href=\"https:\/\/nssm.cc\/usage#io\">I\/O redirection<\/a>\u00a0is enabled. Setting\u00a0<em>Output<\/em>\u00a0and\u00a0<em>Error<\/em>\u00a0is usually sufficient to capture log messages generated by the application.<\/p>\n<p>Configure I\/O in the registry as described\u00a0<a href=\"https:\/\/nssm.cc\/usage#io\">below<\/a>\u00a0for more control over paths and access modes.<\/p>\n<p align=\"center\"><img decoding=\"async\" src=\"https:\/\/nssm.cc\/images\/install_io.png\" border=\"0\" \/><\/p>\n<p>Equivalent\u00a0<a href=\"https:\/\/nssm.cc\/commands\">commands<\/a>:<\/p>\n<pre class=\"code\">nssm set UT2003 AppStdout C:\\games\\ut2003\\service.log<\/pre>\n<pre class=\"code\">nssm set UT2003 AppStderr C:\\games\\ut2003\\service.log<\/pre>\n<h4>File rotation tab<\/h4>\n<p>The\u00a0<em>File rotation<\/em>\u00a0tab can be used in conjunction with\u00a0<a href=\"https:\/\/nssm.cc\/usage#io\">I\/O<\/a>\u00a0settings to configure\u00a0<a href=\"https:\/\/nssm.cc\/usage#rotation\">rotation<\/a>\u00a0of output files when the service restarts.<\/p>\n<p>If the\u00a0<em>Replace existing Output and\/or Error files<\/em>\u00a0checkbox is checked,\u00a0<em>nssm<\/em>\u00a0will overwrite existing output files when starting the service. The default is to append to any existing files. If the\u00a0<em>Rotate files<\/em>\u00a0checkbox is checked,\u00a0<em>nssm<\/em>\u00a0will rename existing files prior to setting up I\/O redirection. Use the\u00a0<em>Restrict rotation<\/em>\u00a0fields to disable file rotation for files which have been modified more recently than the specified number of\u00a0<em>seconds<\/em>\u00a0or are smaller than the specified number of\u00a0<em>kilobytes<\/em>.<\/p>\n<p>By default\u00a0<em>nssm<\/em>\u00a0only 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\u00a0<em>Rotate while service is running<\/em>\u00a0checkbox. Online rotation ignores any configured file age limit.<\/p>\n<p>Danger, moving parts! Online rotation requires\u00a0<em>nssm<\/em>\u00a0to intercept the application&#8217;s output and write the files itself. Increased complexity necessarily leads to increased risk of failure.<\/p>\n<p align=\"center\"><img decoding=\"async\" src=\"https:\/\/nssm.cc\/images\/install_rotation.png\" border=\"0\" \/><\/p>\n<p>Equivalent\u00a0<a href=\"https:\/\/nssm.cc\/commands\">commands<\/a>:<\/p>\n<pre class=\"code\">nssm set UT2003 AppStdoutCreationDisposition 4<\/pre>\n<pre class=\"code\">nssm set UT2003 AppStderrCreationDisposition 4<\/pre>\n<pre class=\"code\">nssm set UT2003 AppRotateFiles 1<\/pre>\n<pre class=\"code\">nssm set UT2003 AppRotateOnline 0<\/pre>\n<pre class=\"code\">nssm set UT2003 AppRotateSeconds 86400<\/pre>\n<pre class=\"code\">nssm set UT2003 AppRotateBytes 1048576<\/pre>\n<h4>Environment tab<\/h4>\n<p>The\u00a0<em>Environment<\/em>\u00a0tab can be used to specify a newline-separated list of\u00a0<a href=\"https:\/\/nssm.cc\/usage#environment\">environment variables<\/a>\u00a0to pass to the application. If the\u00a0<em>Replace default environment<\/em>\u00a0checkbox is checked the variables specified will be the\u00a0<em>only<\/em>\u00a0ones passed to the service. When it is left unchecked (the default), the environment created at service startup will be preserved.<\/p>\n<p align=\"center\"><img decoding=\"async\" src=\"https:\/\/nssm.cc\/images\/install_environment.png\" border=\"0\" \/><\/p>\n<p>Equivalent\u00a0<a href=\"https:\/\/nssm.cc\/commands\">commands<\/a>:<\/p>\n<pre class=\"code\">nssm set &lt;servicename&gt; AppEnvironmentExtra JAVA_HOME=C:\\java<\/pre>\n<h4>Installing from the command line<\/h4>\n<p>As of version 2.0 you can also bypass the GUI and install a service from the command line. The syntax is:<\/p>\n<pre class=\"code\">nssm install &lt;servicename&gt; &lt;application&gt; [&lt;options&gt;]<\/pre>\n<p>Please note that the actual program entered into the services database is\u00a0<em>nssm<\/em>\u00a0itself so you must not move or delete\u00a0<em>nssm.exe<\/em>\u00a0after installing a service. If you do wish to change the path to\u00a0<em>nssm.exe<\/em>\u00a0you can either remove and reinstall the service or edit\u00a0<strong>HKLM\\System\\CurrentControlSet\\Services\\<em>servicename<\/em>\\ImagePath<\/strong>\u00a0to reflect the new location.<\/p>\n<h4>Quoting issues<\/h4>\n<p><em>nssm<\/em>\u00a0correctly handles paths with spaces but passing arguments to it can be tricky because of how the command prompt works.<\/p>\n<p>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\u00a0<em>two<\/em>\u00a0arguments.<\/p>\n<pre class=\"code\">nssm install &lt;servicename&gt; \"C:\\Program Files\\app.exe\"<\/pre>\n<p>If one of the options you wish to provide contains spaces, you will need to quote that too\u00a0<em>and<\/em>\u00a0quote the quotation marks themselves.<\/p>\n<pre class=\"code\">nssm install &lt;servicename&gt; &lt;application&gt; \"\"\"This is one argument\"\"\"<\/pre>\n<p>Isaballa Sanfelipo suggests a method of installing a Java application from a batch file.<\/p>\n<pre class=\"code\">nssm install solr \"%JavaExe%\" -Dsolr.solr.home=\"\\\"%CD%\\solr\"\\\"\r\n-Djetty.home=\"\\\"%CD%\"\\\" -Djetty.logs=\"\\\"%CD%\\logs\"\\\" -cp\r\n\"\\\"%CD%\\lib\\*.jar\"\\\";\"\\\"%CD%\\start.jar\"\\\" -jar \"\\\"%CD%\\start.jar\"\\\"<\/pre>\n<p>John Duffy needed to pass quotes through to the parameter list.<\/p>\n<pre class=\"code\">nssm set NodeServer3000 AppParameters \"\"\"\"\"\"\"$Env:NODE_JS_NPM\"\"\"\"\"\" start\"<\/pre>\n<h3>Removing a service<\/h3>\n<p>The command to remove a service is:<\/p>\n<pre class=\"code\">nssm remove &lt;servicename&gt;<\/pre>\n<p align=\"center\"><img decoding=\"async\" src=\"https:\/\/nssm.cc\/images\/remove.png\" border=\"0\" \/><\/p>\n<p>A confirmation window is displayed before the service is removed.<\/p>\n<p align=\"center\"><img decoding=\"async\" src=\"https:\/\/nssm.cc\/images\/confirmremove.png\" border=\"0\" \/><\/p>\n<p>As of version 2.0 you can also remove services from the command line viz:<\/p>\n<pre class=\"code\">nssm remove &lt;servicename&gt; confirm<\/pre>\n<p><em>nssm<\/em>\u00a0will happily try to remove any service, not just ones\u00a0<em>nssm<\/em>\u00a0itself manages. Try not to delete services you shouldn&#8217;t&#8230;<\/p>\n<p><a name=\"shutdown\"><\/a><\/p>\n<h3>Service shutdown<\/h3>\n<p>When\u00a0<em>nssm<\/em>\u00a0receives 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&#8217;s process tree does not exit promptly,\u00a0<em>nssm<\/em>\u00a0can forcibly terminate all processes and subprocesses belonging to the application.<\/p>\n<p>There are four stages which\u00a0<em>nssm<\/em>\u00a0can 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.<\/p>\n<p>First\u00a0<em>nssm<\/em>\u00a0will attempt to generate a Control-C event and send it to the application&#8217;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.\u00a0<em>Not supported on Windows 2000.<\/em><\/p>\n<p>Secondly\u00a0<em>nssm<\/em>\u00a0will enumerate all windows created by the application and send them a\u00a0<em>WM_CLOSE<\/em>\u00a0message. Applications may follow the convention of responding to the message by initiating a graceful exit.<\/p>\n<p>Thirdly\u00a0<em>nssm<\/em>\u00a0will enumerate all threads created by the application and send them a\u00a0<em>WM_QUIT<\/em>\u00a0message, which will be received if the application has a thread message queue.<\/p>\n<p>As a last resort\u00a0<em>nssm<\/em>\u00a0can call\u00a0<code>TerminateProcess()<\/code>\u00a0to request that the operating system forcibly terminate the application. The\u00a0<code>TerminateProcess()<\/code>\u00a0call 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.<\/p>\n<p>To disable any of the methods above, create an integer (REG_DWORD) value\u00a0<strong>HKLM\\System\\CurrentControlSet\\Services\\<em>servicename<\/em>\\Parameters\\AppStopMethodSkip<\/strong>\u00a0and set it to the sum of one or more of the numbers below.<\/p>\n<ul>\n<li><strong>1<\/strong>\u00a0&#8211; Don&#8217;t send Control-C to the console.<\/li>\n<li><strong>2<\/strong>\u00a0&#8211; Don&#8217;t send WM_CLOSE to windows.<\/li>\n<li><strong>4<\/strong>\u00a0&#8211; Don&#8217;t send WM_QUIT to threads.<\/li>\n<li><strong>8<\/strong>\u00a0&#8211; Don&#8217;t call TerminateProcess().<\/li>\n<\/ul>\n<p>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\u00a0<em>AppStopMethodSkip<\/em>\u00a0to 5.<\/p>\n<p>It is highly recommended\u00a0<strong>not<\/strong>\u00a0to disable the\u00a0<code>TerminateProcess()<\/code>\u00a0call. When the service is stopped\u00a0<em>nssm<\/em>\u00a0will exit. If the application is not terminated before that happens it may continue running and\u00a0<em>nssm<\/em>\u00a0will no longer be able to control it.<\/p>\n<p>By default\u00a0<em>nssm<\/em>\u00a0will 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<strong>HKLM\\System\\CurrentControlSet\\Services\\<em>servicename<\/em>\\Parameters<\/strong>\u00a0in the registry and setting them to the desired number of milliseconds to wait.<\/p>\n<ul>\n<li><strong>AppStopMethodConsole<\/strong>\u00a0&#8211; Time to wait after sending Control-C.<\/li>\n<li><strong>AppStopMethodWindow<\/strong>\u00a0&#8211; Time to wait after sending WM_CLOSE.<\/li>\n<li><strong>AppStopMethodThreads<\/strong>\u00a0&#8211; Time to wait after sending WM_QUIT.<\/li>\n<\/ul>\n<p>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.<\/p>\n<p><a name=\"exit\"><\/a><\/p>\n<h3>Actions on exit<\/h3>\n<p>To configure the action which\u00a0<em>nssm<\/em>\u00a0should take when the application exits, edit the default value of the key\u00a0<strong>HKLM\\System\\CurrentControlSet\\Services\\<em>servicename<\/em>\\Parameters\\AppExit<\/strong>. If the key does not exist in the registry when\u00a0<em>nssm<\/em>\u00a0runs it will create it and set the value to\u00a0<strong>Restart<\/strong>. Change it to either\u00a0<strong>Ignore<\/strong>\u00a0or\u00a0<strong>Exit<\/strong>\u00a0to specify the action taken.\u00a0<em>nssm<\/em>\u00a0will only create this key if it doesn&#8217;t already exist. Your changes will not be overridden.<\/p>\n<p>To specify a different action for particular exit codes, create a string (REG_SZ) value underneath the\u00a0<strong>AppExit<\/strong>\u00a0key 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\u00a0<strong>HKLM\\System\\CurrentControlSet\\Services\\<em>servicename<\/em>\\Parameters\\AppExit\\0<\/strong>\u00a0and set it to\u00a0<strong>Exit<\/strong>. Look in the Event Log for messages from\u00a0<em>nssm<\/em>\u00a0to see what exit codes are returned by your application.<\/p>\n<p>If your application&#8217;s exit code does not correspond to a registry entry,\u00a0<em>nssm<\/em>\u00a0will use the default value of\u00a0<strong>AppExit<\/strong>\u00a0when deciding what to do.<\/p>\n<p><a name=\"delay\"><\/a><\/p>\n<h3>Restart delay<\/h3>\n<p>As of version 2.22,\u00a0<em>nssm<\/em>\u00a0can 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.<\/p>\n<p>To specify a restart delay, create an integer (REG_DWORD) value\u00a0<strong>HKLM\\System\\CurrentControlSet\\Services\\<em>servicename<\/em>\\Parameters\\AppDelay<\/strong>\u00a0and set it to the number of milliseconds to wait between restarts.<\/p>\n<p>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.<\/p>\n<p>Please see the section on\u00a0<a name=\"#throttling\"><\/a>restart throttling\u00a0below for notes on how\u00a0<em>nssm<\/em>\u00a0works when both throttling and restart delays are configured.<\/p>\n<p><a name=\"throttling\"><\/a><\/p>\n<h3>Restart throttling<\/h3>\n<p>To avoid a tight CPU loop,\u00a0<em>nssm<\/em>\u00a0will 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<strong>HKLM\\System\\CurrentControlSet\\Services\\<em>servicename<\/em>\\Parameters\\AppThrottle<\/strong>\u00a0and set it to the required number of milliseconds.<\/p>\n<p>The first restart will be attempted with no delay. If the restarted application continues to exit before running for the threshold amount of milliseconds,\u00a0<em>nssm<\/em>\u00a0will 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.<\/p>\n<p>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.<\/p>\n<p>When a\u00a0<a href=\"https:\/\/nssm.cc\/usage#delay\">restart delay<\/a>\u00a0is configured and the application exits prematurely,\u00a0<em>nssm<\/em>\u00a0will throttle the restart by whichever is\u00a0<em>longer<\/em>\u00a0of 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.<\/p>\n<p>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\u00a0<strong>AppThrottle<\/strong>.<\/p>\n<p><a name=\"process\"><\/a><\/p>\n<h3>Process priority and CPU affinity<\/h3>\n<p>As of version 2.22,\u00a0<em>nssm<\/em>\u00a0can manage the CPU affinity and process priority of the managed application.<\/p>\n<p>By default application will be launched with normal process priority and be allowed to execute on any CPU.\u00a0<em>nssm<\/em>\u00a0will look under\u00a0<strong>HKLM\\System\\CurrentControlSet\\Services\\<em>servicename<\/em>\\Parameters<\/strong>\u00a0for registry entries to configure the application startup.<\/p>\n<p>If the integer (REG_DWORD) value\u00a0<strong>AppPriority<\/strong>\u00a0is set,\u00a0<em>nssm<\/em>\u00a0will interpret its value as an argument to\u00a0<code>SetPriorityClass()<\/code>\u00a0and start the application with the specified priority.<\/p>\n<p>If the string (REG_SZ) value\u00a0<strong>AppAffinity<\/strong>\u00a0is set,\u00a0<em>nssm<\/em>\u00a0will interpret it as a comma-separated list of CPU IDs, starting from\u00a0<em>0<\/em>\u00a0on which the application can run. Alternatively, a range of IDs may be specified by separating the indices with a dash.<\/p>\n<p>Only digits, dashes and commas are valid in an affinity string.<\/p>\n<p>For example, the the string\u00a0<em>0-2,4<\/em>\u00a0specifies that the application may run on the first, second, third and fifth CPUs in the system.<\/p>\n<h3>Console window<\/h3>\n<p>As of version 2.22,\u00a0<em>nssm<\/em>\u00a0will 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\u00a0<strong>AppNoConsole<\/strong>\u00a0under\u00a0<strong>HKLM\\System\\CurrentControlSet\\Services\\<em>servicename<\/em>\\Parameters<\/strong>\u00a0to a non-zero value.<\/p>\n<p><a name=\"io\"><\/a><\/p>\n<h3>I\/O redirection<\/h3>\n<p><em>nssm<\/em>\u00a0can redirect the managed application&#8217;s I\/O to any path capable of being opened by\u00a0<code>CreateFile()<\/code>. This feature may be useful if your application expects to be able to log to the console.<\/p>\n<p><em>nssm<\/em>\u00a0will look under\u00a0<strong>HKLM\\System\\CurrentControlSet\\Services\\<em>servicename<\/em>\\Parameters<\/strong>\u00a0for keys corresponding to arguments to\u00a0<code>CreateFile()<\/code>. 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.<\/p>\n<ul>\n<li><strong>AppStdin<\/strong>\u00a0(string) &#8211; Path to receive input.<\/li>\n<li><strong>AppStdinShareMode<\/strong>\u00a0(integer) &#8211;\u00a0<code>ShareMode<\/code>\u00a0argument for the input.<\/li>\n<li><strong>AppStdinCreationDisposition<\/strong>\u00a0(integer) &#8211;\u00a0<code>CreationDisposition<\/code>\u00a0argument for the input.<\/li>\n<li><strong>AppStdinFlagsAndAttributes<\/strong>\u00a0(integer) &#8211;\u00a0<code>FlagsAndAttributes<\/code>\u00a0argument for the input.<\/li>\n<li><strong>AppStdout<\/strong>\u00a0(string) &#8211; Path to receive output.<\/li>\n<li><strong>AppStdoutShareMode<\/strong>\u00a0(integer) &#8211;\u00a0<code>ShareMode<\/code>\u00a0argument for the output.<\/li>\n<li><strong>AppStdoutCreationDisposition<\/strong>\u00a0(integer) &#8211;\u00a0<code>CreationDisposition<\/code>\u00a0argument for the output.<\/li>\n<li><strong>AppStdoutFlagsAndAttributes<\/strong>\u00a0(integer) &#8211;\u00a0<code>FlagsAndAttributes<\/code>\u00a0argument for the output.<\/li>\n<li><strong>AppStderr<\/strong>\u00a0(string) &#8211; Path to receive error output.<\/li>\n<li><strong>AppStderrShareMode<\/strong>\u00a0(integer) &#8211;\u00a0<code>ShareMode<\/code>\u00a0argument for the error output.<\/li>\n<li><strong>AppStderrCreationDisposition<\/strong>\u00a0(integer) &#8211;\u00a0<code>CreationDisposition<\/code>\u00a0argument for the error output.<\/li>\n<li><strong>AppStderrFlagsAndAttributes<\/strong>\u00a0(integer) &#8211;\u00a0<code>FlagsAndAttributes<\/code>\u00a0argument for the error output.<\/li>\n<\/ul>\n<p>In general it is advisable to set both\u00a0<strong>AppStdout<\/strong>\u00a0and\u00a0<strong>AppStderr<\/strong>\u00a0in order to log output, as applications may log informational and error messages separately.<\/p>\n<p>It is possible to direct both stderr and stdout to the same path but due to a limitation with\u00a0<em>nssm<\/em>\u00a0you must provide the\u00a0<em>exact<\/em>\u00a0same string in both the\u00a0<strong>AppStdout<\/strong>\u00a0and\u00a0<strong>AppStderr<\/strong>\u00a0registry values. Only if the two entries are\u00a0<em>the same<\/em>\u00a0will\u00a0<em>nssm<\/em>\u00a0be able to interleave the two streams.<\/p>\n<p><a name=\"rotation\"><\/a><\/p>\n<h3>File rotation<\/h3>\n<p>As of version 2.22, if\u00a0<a href=\"https:\/\/nssm.cc\/usage#io\">I\/O redirection<\/a>\u00a0is enabled,\u00a0<em>nssm<\/em>\u00a0can rotate existing output files prior to launching the application.\u00a0<em>nssm<\/em>\u00a0can also rotate files while the service is running. See\u00a0<a href=\"https:\/\/nssm.cc\/usage#online_rotation\">online rotation<\/a>\u00a0below.<\/p>\n<p>To enable rotation, create an integer (REG_DWORD) value\u00a0<strong>HKLM\\System\\CurrentControlSet\\Services\\<em>servicename<\/em>\\Parameters\\AppRotate<\/strong>\u00a0and set it to 1. Before (re)starting the service,\u00a0<em>nssm<\/em>\u00a0will rotate the file(s) configured in\u00a0<strong>AppStdout<\/strong>and\/or\u00a0<strong>AppStderr<\/strong>\u00a0if they already exist.<\/p>\n<p>Existing files will be renamed according to a template which uses the last modification time of the file. For example,\u00a0<em>C:\\Services\\myservice.log<\/em>\u00a0might be rotated to\u00a0<em>C:\\Services\\myservice-20140114T180840.953.log<\/em>.<\/p>\n<p>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.<\/p>\n<p>Two additional registry settings under\u00a0<strong>HKLM\\System\\CurrentControlSet\\Services\\<em>servicename<\/em>\\Parameters<\/strong>\u00a0can be used to tweak how\u00a0<em>nssm<\/em>\u00a0rotates files.<\/p>\n<p>If the integer (REG_DWORD) value\u00a0<strong>AppRotateSeconds<\/strong>\u00a0is set,\u00a0<em>nssm<\/em>\u00a0will not rotate any file which was last modified\u00a0<em>less<\/em>\u00a0than the configured number of seconds ago.<\/p>\n<p>If the integer (REG_DWORD) value\u00a0<strong>AppRotateBytes<\/strong>\u00a0is set,\u00a0<em>nssm<\/em>\u00a0will not rotate any file which is\u00a0<em>smaller<\/em>\u00a0than the configured number of bytes.\u00a0<em>nssm<\/em>\u00a0can also handle files whose size is too large to be expressed in 32-bits, in case you decide that it&#8217;s fine for a log file to grow to 4GB in size but any more is just\u00a0<em>too big<\/em>. The value of\u00a0<strong>AppRotateBytesHigh<\/strong>\u00a0will be interpreted as the high order part of a 64-bit size.<\/p>\n<p>If both\u00a0<strong>AppRotateSeconds<\/strong>\u00a0and\u00a0<strong>AppRotateBytes(High)<\/strong>\u00a0are set,\u00a0<em>nssm<\/em>\u00a0will require that both criteria are met for a file to be rotated.<\/p>\n<p><a name=\"online_rotation\"><\/a><\/p>\n<h4>Online rotation<\/h4>\n<p>If the integer (REG_DWORD) value\u00a0<strong>AppRotateOnline<\/strong>\u00a0is set to 1,\u00a0<em>nssm<\/em>\u00a0can rotate files which grow to the configured file size limit while the service is running. The value of\u00a0<strong>AppRotateSeconds<\/strong>\u00a0is ignored for the purposes of online rotation, though it will still apply for rotation before the service (re)starts.<\/p>\n<p><strong>AppRotateOnline<\/strong>\u00a0is ignored if\u00a0<strong>AppRotate<\/strong>\u00a0is not set.<\/p>\n<p>When online rotation is enabled,\u00a0<em>nssm<\/em>\u00a0reads the application&#8217;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\u00a0<em>nssm<\/em>\u00a0will 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\u00a0<a href=\"https:\/\/nssm.cc\/usage#io_technical\">technical discussion<\/a>\u00a0for more details about how\u00a0<em>nssm<\/em>\u00a0handles I\/O redirection.<\/p>\n<p><a name=\"ondemand_rotation\"><\/a><\/p>\n<h4>On-demand rotation<\/h4>\n<p><em>nssm<\/em>\u00a0can 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\u00a0<a href=\"https:\/\/nssm.cc\/commands\">command<\/a>:<\/p>\n<pre class=\"code\">nssm rotate &lt;servicename&gt;<\/pre>\n<p>A limitation of on-demand rotation is that the actual file renaming won&#8217;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.<\/p>\n<p>On-demand rotation will work even if\u00a0<strong>AppRotateBytes<\/strong>\u00a0is not configured, ie if the service would not rotate files while running. It will not work, however, unless both\u00a0<strong>AppRotate<\/strong>\u00a0and\u00a0<strong>AppRotateOnline<\/strong>\u00a0are configured.<\/p>\n<p><a name=\"io_technical\"><\/a><\/p>\n<h3>I\/O redirection technical details<\/h3>\n<p>There are three cases to consider when looking at how a service handles I\/O.<\/p>\n<h4>No redirection<\/h4>\n<p>In the simplest case,\u00a0<em>nssm<\/em>\u00a0is 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.<\/p>\n<h4>I\/O redirected; online rotation disabled<\/h4>\n<p>If stdout and\/or stderr are redirected and online rotation is disabled,\u00a0<em>nssm<\/em>\u00a0will call\u00a0<code>CreateFile()<\/code>\u00a0to open a handle for each I\/O stream, then call\u00a0<code>DuplicateHandle()<\/code>\u00a0to set a copy of the handle in the\u00a0<code>STARTUPINFO<\/code>\u00a0datastructure passed to\u00a0<code>CreateProcess()<\/code>. Thus the application will run with an open handle to the file whereas\u00a0<em>nssm<\/em>\u00a0itself has no open handles.<\/p>\n<h4>I\/O redirected; online rotation enabled<\/h4>\n<p>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.<\/p>\n<p><em>nssm<\/em>\u00a0first calls\u00a0<code>CreateFile()<\/code>\u00a0just as in the previous case. It then calls\u00a0<code>CreatePipe()<\/code>\u00a0to 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\u00a0<code>DuplicateHandle()<\/code>\u00a0for passing to\u00a0<code>CreateProcess()<\/code>. Thus the application will run with an open handle to one end of the pipe and\u00a0<em>nssm<\/em>\u00a0will run with a handle to the other end of the pipe and a handle to the output file.<\/p>\n<p>The writing thread runs a simple loop wherein it reads data from the pipe, ie the application&#8217;s output, into a buffer with\u00a0<code>ReadFile()<\/code>\u00a0then writes the contents of that buffer to the output file with\u00a0<code>WriteFile()<\/code>. 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.<\/p>\n<p>When\u00a0<em>nssm<\/em>\u00a0receives an on-demand rotation request it will set a flag which instructs the writing thread to perform a rotation after the next\u00a0<code>ReadFile()<\/code>\u00a0call completes, regardless of the file size. Because\u00a0<code>ReadFile()<\/code>\u00a0blocks execution until something is read,\u00a0<em>nssm<\/em>\u00a0won&#8217;t actually get round to rotating the file until the application produces the next line of output.<\/p>\n<p><em>nssm<\/em>\u00a0doesn&#8217;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\u00a0<code>IsTextUnicode()<\/code>\u00a0to try to determine the text encoding that&#8217;s in use. If it (looks like it) is Unicode,\u00a0<em>nssm<\/em>\u00a0writes 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.<\/p>\n<p>If both stdout and stderr are redirected, and to separate files,\u00a0<em>nssm<\/em>\u00a0will spawn a writing thread for each.\u00a0<em>nssm<\/em>\u00a0will deal with an application which writes stdout in Unicode and stderr in ANSI &#8211; or vice versa &#8211; if you are unfortunate (or evil) enough to run one.<\/p>\n<p>It should be clear from the above that there are a number of pitfalls to online rotation.\u00a0<em>nssm<\/em>\u00a0will 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.<\/p>\n<p><a name=\"environment\"><\/a><\/p>\n<h3>Environment variables<\/h3>\n<p>As of version 2.11,\u00a0<em>nssm<\/em>\u00a0respects the\u00a0<strong>AppEnvironment<\/strong>\u00a0registry value supported by\u00a0<em>srvany<\/em>. To specify a list of environment variables to pass to the monitored application, create a multi-valued string (REG_MULTI_SZ) value<strong>HKLM\\System\\CurrentControlSet\\Services\\<em>servicename<\/em>\\Parameters\\AppEnvironment<\/strong>\u00a0where each entry is of the form\u00a0<em>KEY=VALUE<\/em>.<\/p>\n<p>It is possible to omit the\u00a0<em>VALUE<\/em>\u00a0if you just want the environment variable\u00a0<em>KEY<\/em>\u00a0to exist but the\u00a0<em>=<\/em>\u00a0symbol is mandatory.\u00a0<em>The service will not run if an invalid environment is specified!<\/em><\/p>\n<p>As of version 2.19,\u00a0<em>nssm<\/em>\u00a0also respects the\u00a0<strong>AppEnvironmentExtra<\/strong>\u00a0registry value, which should have the same format as\u00a0<strong>AppEnvironment<\/strong>. Environment variables set in\u00a0<strong>AppEnvironmentExtra<\/strong>\u00a0will be\u00a0<em>added<\/em>\u00a0to the service&#8217;s default environment.<\/p>\n<p>For compatibility with\u00a0<em>srvany<\/em>, the environment variables specified in\u00a0<strong>AppEnvironment<\/strong>\u00a0will\u00a0<em>replace<\/em>\u00a0those set by the system at service startup. Since that probably isn&#8217;t what you want, use\u00a0<strong>AppEnvironmentExtra<\/strong>\u00a0instead.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Usage No &#8220;installation&#8221; of\u00a0nssm\u00a0is needed. Just place it somewhere on the system (preferably somewhere in your PATH) and run it. Note however that\u00a0nssm\u00a0registers itself as an Event Log message source which means that running multiple instances or different version of\u00a0nssm\u00a0from different locations may cause confusion. Also note that if you run the Event Viewer it [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[1,730,256,42,439,495,102],"tags":[1125,1127,1126],"class_list":["post-4362","post","type-post","status-publish","format-standard","hentry","category-viazap","category-clusterweb","category-desktop","category-leitura-recomendada","category-midia","category-profissional-de-ti","category-windows","tag-nssm","tag-service-manager","tag-the-non-sucking"],"_links":{"self":[{"href":"https:\/\/blog.clusterweb.com.br\/index.php?rest_route=\/wp\/v2\/posts\/4362","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.clusterweb.com.br\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.clusterweb.com.br\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.clusterweb.com.br\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.clusterweb.com.br\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4362"}],"version-history":[{"count":1,"href":"https:\/\/blog.clusterweb.com.br\/index.php?rest_route=\/wp\/v2\/posts\/4362\/revisions"}],"predecessor-version":[{"id":4363,"href":"https:\/\/blog.clusterweb.com.br\/index.php?rest_route=\/wp\/v2\/posts\/4362\/revisions\/4363"}],"wp:attachment":[{"href":"https:\/\/blog.clusterweb.com.br\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4362"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.clusterweb.com.br\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4362"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.clusterweb.com.br\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4362"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}