PM2 is a project that is stable, robust, and well designed. However, when running for Electron, it has two issues:
- Electron has an internal filesystem format called ASAR which improves the performance of Electron apps. PM2 is unable to work with this format.
- Electron distributes it's own (embedded) NodeJS. However, PM2 ignores this and requires the end user to install another version of NodeJS.
Hence I created this new "Process Manager". It can be used without Electron of course but will also work perfectly with Electron without any of the existing PM2 issues.
- Start multiple processes
- Start Python and NodeJS processes
- Understands NodeJS modules (you can just point to the module folder)
- WORKS WITH EMBEDDED Electron NodeJS
- Stop any started process whenever you want
- Direct the output to a log file
- Set the working directory and environment if needed
- Pass any required arguments to the scripts
- KEEP-ALIVE: Automatically restarts the process if crashes (completely configurable)
const pm2 = require('@tpp/pm2')
pm2.start({
script: 'path/to/script.py',
})
pm2
functions take an optional callback that is invoked when there are
errors.
pm2.start({
script: 'path/to/script.py',
}, (err, pid) => {
...
})
The start
callback can be invoked multiple times - for each error in
the process and when the process restarts. The callback also contains
the PID of the process when it starts. A callback without pid
or err
means it has terminated cleanly.
pm2.start({
script: 'path/to/script.py',
args: ['say','hello'],
})
pm2.start({
script: 'script.py',
cwd: '/path/to/folder',
})
Because pm2
understands Node modules we can simply point to the module
folder and it will pick up the correct starting point.
pm2.start({
cwd: '/path/to/npm/module/',
})
pm2.start({
cwd: '/path/to/npm/module/',
log: 'mylog.log',
})
In order to stop a process it needs to be given a name:
pm2.start({
name: 'my-process',
cwd: '/path/to/npm/module/',
})
pm2.stop('my-process')
stop
also takes an optional callback that will be called when
stopped/error.
pm2.stop('my-process', (err) => {
...
})
You can stop all running processes (named or un-named).
pm2.stopAll()
You can also restart a named process.
pm2.restart('my-process')
Note that restart
does not take a callback. It uses the same callback
you provided to start
.
You can set the environment of the running process:
pm2.start({
script: 'path/to/script.py',
env: ENVIRONMENT_OBJECT,
})
If you want to clean up your sub-processes or simply stop cleanly in a
sub-process you can use the onstopping
event handler:
const pm2 = require('@tpp/pm2')
pm2.onstopping(() => {
...
})
Of course this only works if you're sub-process is a NodeJS module.
By default pm2
will restart any crashed processes. To prevent
'spinning' it will restart each time with a delay:
- First crash? Wait 100ms before restarting
- Crashed again? Wait 500ms before restarting
- Crashed again? Wait 1 second, then 10 seconds, then 30 seconds, then 1 minute, then 5 minutes before restarting
- Still crashing? Re-try every 15 minutes
If it has been running successfully for 30 minutes assume started ok.
This is all configurable using the restartAt
and restartOk
parameters. The default parameters for the behaviour given above is:
pm2.start({
cwd: '/path/to/npm/module/',
restartAt: [100, 500, 1*1000, 10*1000, 30*1000, 60*1000, 5*60*1000],
restartOk: 30 * 60 * 1000,
})
You can design any restart strategy you want using the above parameters.
You can turn the restarting behaviour off completely by setting
restartAt
to []
or [0]
.
In some cases the output of programs designed for the terminal can
contain ANSI Escape Codes.
To remove them specify stripANSI:true
as an option.
pm2.start({
cwd: '/path/to/npm/module/',
log: 'mylog.log',
stripANSI: true,
})
This is usually because you want a clean log file.
[ ] Add Support for more process types (besides just python and node)