@fdawgs/pm2-windows-service v1.0.4
pm2-windows-service
Easily install and uninstall PM2 as a Windows service
Intro
This is a fork of Jon Hall's pm2-windows-service, updated to use ES6 features, newer versions of dependencies, and fix bugs.
Prerequisites
- Node.js version later than
4.0.0is required, but you should NOT use8.5.0as this module uses node's path module, which is vulnerable in version8.5.0.
Installation
Install using yarn:
yarn global add @fdawgs/pm2-windows-serviceOr npm:
npm install @fdawgs/pm2-windows-service -gThen run the following in a CLI as an administrator to install or uninstall:
pm2-service-install [-n <service name>] [--unattended]
pm2-service-uninstallThe install command also offers to perform some basic setup for you which helps address some of the caveats detailed below.
Quick start
After reading the caveats section, use PM2 to start the set of processes that you want the service to restore, and then just do:
pm2 saveThe service will then restart that set of processes when the service is next started (by default this will be on system boot).
Configuration
You can control what the PM2 service runs using the PM2_SERVICE_SCRIPTS environment variable, which should be set to a semi-colon separated list of JavaScript files and/or JSON config files to run when the service starts (using pm2 start).
If PM2_SERVICE_SCRIPTS is not set, then the default behaviour is to call pm2 resurrect on service start-up - 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.
Caveats
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:
- If you do not have the
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. - If you run the service under one user account, and then attempt to interact with PM2 from the command line using a different account, you'll find things do not work as expected if the
PM2_HOMEenvironment variable contains any "user context" env vars (%APPDATA%,%USERPROFILE%etc.), or if one of the users cannot access the location ofPM2_HOME.- To avoid this problem, either set
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.
- To avoid this problem, either set
- It also seems PM2 throws an error when you try to use the CLI from a non-administrative command prompt after it has been launched as a service, regardless of which user the service runs under (not tested with a non-admin user, yet).
- Currently, the workaround for this, unfortunately, is just use an administrative command prompt if you need to access PM2 via command line when it is running as a service.
- As raised in #24, there appears to be a dependency on having .NET 3.5 installed on the machine on which the service runs (and this holds true even when later versions are installed, you still need 3.5 as well) - this issue is still under investigation but, while that's happening, this is something to bear in mind when setting up the service.
- Lastly, when launching json config files using
PM2_SERVICE_SCRIPTS, problems arise if the apps declared in the config file do not explicitly have acwdset (it ends up being the home dir of the service user).pm2-windows-serviceattempts to solve this issue for you by automatically defaulting thecwdproperty to the directory of the config file when it isn't explicitly set, if this is an issue for you then explicitly setting thecwdfor your apps might be what you need to do.
Logs
The service created by node-windows is currently placed in <global npm/yarn 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.
License
@fdawgs/pm2-windows-service is licensed under the MIT license.