npm i pm2-windows-service -g
NOTE: pm2-windows-service currently requires node v4.0.0+, if node v0.12.x support is a requirement for you, please post in this issue.
TIP: Run these from an administrative command prompt to avoid getting hit with a bunch of UAC dialogs
pm2-service-install [-n <service name>]pm2-service-uninstall
The install command also offers to perform some basic setup for you which helps address some of the caveats detailed below.
After reading the caveats section, use PM2 to start the set of processes that you want the service to restore, and then just do:
The service will then restart that set of processes when the service is next started (by default this will be on system boot).
You can control what the PM2 service runs using the
PM2_SERVICE_SCRIPTS is not set, then the default behaviour is to call
pm2 resurrect on service startup - when PM2 is running with the list of processes you want launched by the service, use
pm2 save to dump the process list, ready for the service to restore it when it next starts.
While testing this, a few caveats have arisen which need to be detailed upfront, as they can lead to issues when PM2 is installed as a service:
PM2_HOMEenvironment variable set (in such a way that it is available to the service user - the simplest being set it at machine level), you might find PM2 attempting to run in strange directories. To resolve this, make sure you set the environment variable and restart the service - though see the comment just below about the location it points to being accessible for the service user.
PM2_HOMEenvironment variable contains any "user context" env vars (
%USERPROFILE%etc.), or if one of the users cannot access the location of
PM2_HOMEto an absolute path that all potential users (service and CLI) can write to, or run the service under the same account as you intend to use the CLI from.
PM2_SERVICE_SCRIPTS, problems arise if the apps declared in the config file don't explicitly have a
cwdset (it ends up being the home dir of the service user).
pm2-windows-serviceattempts to solve this issue for you by automatically defaulting the
cwdproperty to the directory of the config file when it isn't explicitly set, if this is an issue for you then explicitly setting the
cwdfor your apps might be what you need to do.
The service created by node-windows is currently placed in
<global npm packages directory>/node_modules/pm2-windows-service/src/daemon/, as such, this is also where you will find the log output from the service, should you need it.