7

I can't run pm2 on ubuntu box. I'm not sure what's the problem. The pm2 is installed globally.

npm list -g --depth=0
/opt/nodejs/lib
├── forever@0.15.3
├── node-gyp@3.4.0
├── npm@2.15.9
├── pm2@2.1.5
├── userdown@0.2.1
└── wait-for-mongo@0.2.0

But I still get

pm2
-bash: pm2: command not found

if I run other app

userdown
Starting Script is not provided

versions

node v4.5.0
npm  v2.15.9

log from installation:

sudo npm install pm2 -g
npm WARN optional dep failed, continuing fsevents@1.0.15
/opt/nodejs/bin/pm2 -> /opt/nodejs/lib/node_modules/pm2/bin/pm2
/opt/nodejs/bin/rundev -> /opt/nodejs/lib/node_modules/pm2/bin/rundev
/opt/nodejs/bin/pm2-dev -> /opt/nodejs/lib/node_modules/pm2/bin/pm2-dev
/opt/nodejs/bin/pm2-docker -> /opt/nodejs/lib/node_modules/pm2/bin/pm2-docker
pm2@2.1.5 /opt/nodejs/lib/node_modules/pm2
├── gkt@1.0.0
├── eventemitter2@1.0.5
├── semver@5.3.0
├── async@1.5.2
├── fclone@1.0.10
├── pidusage@1.1.0
├── vizion@0.2.13
├── commander@2.9.0 (graceful-readlink@1.0.1)
├── debug@2.3.0 (ms@0.7.2)
├── pm2-axon-rpc@0.4.5 (fclone@1.0.8)
├── pm2-deploy@0.3.3 (tv4@1.2.7)
├── pm2-multimeter@0.1.2 (charm@0.1.2)
├── chalk@1.1.3 (escape-string-regexp@1.0.5, ansi-styles@2.2.1, supports-color@2.0.0, strip-ansi@3.0.1, has-ansi@2.0.0)
├── cli-table@0.3.1 (colors@1.0.3)
├── mkdirp@0.5.1 (minimist@0.0.8)
├── source-map-support@0.4.6 (source-map@0.5.6)
├── nssocket@0.6.0 (eventemitter2@0.4.14, lazy@1.0.11)
├── pmx@0.6.8 (json-stringify-safe@5.0.1)
├── pm2-axon@3.0.2 (amp-message@0.1.2, escape-regexp@0.0.1, amp@0.3.1, debug@2.2.0)
├── cron@1.1.1 (moment-timezone@0.5.9)
├── yamljs@0.2.8 (glob@7.1.1, argparse@1.0.9)
├── chokidar@1.6.1 (path-is-absolute@1.0.1, async-each@1.0.1, inherits@2.0.3, glob-parent@2.0.0, is-glob@2.0.1, is-binary-path@1.0.1, readdirp@2.1.0, anymatch@1.3.0)
├── shelljs@0.7.5 (interpret@1.0.1, glob@7.1.1, rechoir@0.6.2)
└── moment@2.17.0
kamil@vps2:~$ pm2
-bash: pm2: command not found

ubuntu version:

uname -a
Linux vps2 2.6.32-042stab111.11 #1 SMP Tue Sep 1 18:19:12 MSK 2015 x86_64 x86_64 x86_64 GNU/Linux
Kamil
  • 321
  • 1
  • 2
  • 10

4 Answers4

14

Ok got answer myself. I check what happens for

whereis pm2
pm2: /opt/nodejs/bin/pm2

then I checked

whereis userdown
userdown: /usr/bin/userdown /usr/bin/X11/userdown /opt/nodejs/bin/userdown

hmm in /usr/bin.... So I did

sudo ln -s /opt/nodejs/bin/pm2 /usr/bin/pm2 

and it works :)

Kamil
  • 321
  • 1
  • 2
  • 10
4

The problem is that you are running NPM as sudo, so you will only be able to access it using:

sudo pm2 start server.js

Install without sudo, you may even install without the -gflag and call it directly from node_modules directory. This may be useful if you do not have root (admin) privileges in the machine you're working on.

npm install pm2
./node_modules/.bin/pm2 start server.js
Luís Brito
  • 1,652
  • 1
  • 17
  • 34
1

Follow the proper nodejs isntallation, npm permission fixes and npm global packages tweaks:

@ https://gist.github.com/servercharlie/9a7e0d0e1645b4c6fbfe5de566fcf1ca

Your script needs to do some thing that requires root privilege? (ie: you're getting an error on using port 80)

[wrong] - trying to run w/ sudo

[correct] - login as root "sudo su" then do pm2 start app.js --name "whatever" --watch

that does it, no need to configure any bashrc or profile files.

extra: worried about your app doing crazy shit? (ie, since it's executed as root, the script can use nodejs's exec and do some crazy stuff.)

hence. do this: do the root-stuff first with your script, then lower your privilege after some timeout:

// i use port 80 first.. at this point the script's uid is ROOT.

app.listen(80);

// after 2 seconds we switch to uid AZUREUSER, which obviously isn't root anymore.

setTimeout(function(){

process.setuid("azureuser");

}, 2000);

1

In my scenario I wrote a shell script that was triggered by jenkins build and

I fixed using following link

https://github.com/Unitech/pm2-deploy/issues/41

Shree Prakash
  • 2,052
  • 2
  • 22
  • 33