Pm2: How to run pm2 command on AWS Elastic Beanstalk?

Created on 29 May 2016  ยท  9Comments  ยท  Source: Unitech/pm2

Hi. I followed Using PM2 in Cloud Providers and deployed to AWS Elastic Beanstalk successfully. But my problem is, when I run command like pm2 monit, bash tell me that pm2: command not found. I've tried something like the following but in vain:

  • First, export the path of node:
PATH=$PATH:`ls -td /opt/elasticbeanstalk/node-install/node-* | head -1`/bin
  • Then run pm2 list in this folder, but got an empty list:
ec2-user@ip-10-153-160-238 ~]$ /var/app/current/node_modules/pm2/bin/pm2 list
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ App name โ”‚ id โ”‚ mode โ”‚ pid โ”‚ status โ”‚ restart โ”‚ uptime โ”‚ memory โ”‚ watching โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
 Use `pm2 show <id|name>` to get more details about an app

What should I do to make it work? Thanks.

Most helpful comment

I know that the thread already closed, but may this will save time for the next one that will google it:
sudo -u nodejs /opt/elasticbeanstalk/node-install/node-v6.10.0-linux-x64/bin/node /var/app/current/node_modules/.bin/pm2 monit

Amazon Elastic Beanstalk environment, 64bit Amazon Linux 2016.09, pm2 installation according to this guide, e.g.
"scripts": { "start": "node ./node_modules/.bin/pm2 start app.js --name yourApp", "poststart": "node ./node_modules/.bin/pm2 logs" }

All 9 comments

try node_modules/.bin/pm2 monit instead of pm2 monit

looks like you didn't install your pm2 globally, which is fine, but your bash doesn't know to look into node_modules/bin

I've tried /var/app/current/node_modules/pm2/bin/pm2 list but got an empty list.

I had the same issue and it looks like the problem is to do with how PM2 starts up. My problem was that i was using the AWS EB interface to set the node command e.g. npm run serve which in turn would run pm2 start.... After some debugging, I realised that pm2 was being started by the nodejs user instead of ec2-user and thats the reason why i was getting an empty list, because the user was wrong.

In order to see the apps running I had to run the following command:
sudo -u nodejs pm2 list

sudo -u nodejs pm2 list Works for me too. Thank you @antonsamper!

I know that the thread already closed, but may this will save time for the next one that will google it:
sudo -u nodejs /opt/elasticbeanstalk/node-install/node-v6.10.0-linux-x64/bin/node /var/app/current/node_modules/.bin/pm2 monit

Amazon Elastic Beanstalk environment, 64bit Amazon Linux 2016.09, pm2 installation according to this guide, e.g.
"scripts": { "start": "node ./node_modules/.bin/pm2 start app.js --name yourApp", "poststart": "node ./node_modules/.bin/pm2 logs" }

@antonsamper What was the command then to start pm2? I understand then that it would be this

sudo -u nodejs node ./node_modules/.bin/pm2 start app.js

i have 502 bad gateway and i have aws eb

You should make sure that node is in your PATH.
If not you should specify the full path, something like /opt/elasticbeanstalk/node-install/node-v6.10.0-linux-x64/bin/node

@ArMouReR is right, it sounds like a PATH issue.

I get around this PATH issue by binding the PATH straight away with .ebextensions where i have the following lines:

yaml container_commands: 01_pm2_install: command: "if [ ! -e /bin/pm2 ]; then npm install [email protected] -g; fi" 02_pm2_binary: command: "ln -sf `ls -td /opt/elasticbeanstalk/node-install/node-* | head -1`/bin/pm2 /bin/pm2"

This is what I used

sudo ln -sf `ls -td /opt/elasticbeanstalk/node-install/node-* | head -1`/bin/node /bin/node
sudo -u nodejs node /var/app/current/node_modules/.bin/pm2 list
Was this page helpful?
0 / 5 - 0 ratings