Using pm2 Process Manager
PM2 is a great package for running node processes on a server. It supports saving its process list and running at system startup.
Get started with npm i -g pm2
or pnpm i -g pm2
Commands
start
Normally, you would run a process using the following syntax:
$ pm2 start npm -- start
where the --
delineates the arguments to send to the process you are starting. This would be for if your regular start script was npm start
.
For other scripts using the run command, you would do
$ pm2 start npm -- run develop
status
You can then check your processes using pm2 status
--name
You can also set a process name using the --name
flag
basedir
Error Workaround
Apparently there is an incompatibility between pm2 and pnpm where the following error occurs. If trying to run
$ pm2 start pnpm -- develop
you would get the following error:
basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')")
^^^^^^^ SyntaxError: missing ) after argument list
at ...
To get around this, I found that I could just call a script that would run the required commands for me. I like to keep these in a folder to save a bunch of cd
-ing.
$ pm2 start ~/launchers/my-job.sh
which contained this simple script
#!/bin/bash
cd ~/code/strapi-demo
pnpm develop
This solved the issue for me. Apparently this may depend on how you installed pnpm (via brew, asdf, npm, scoop, native install script). I had installed via native script.