I am trying to write a script which ssh into a bunch of my servers and runs some commands to start my app. One of the commands (pm2) always says pm2: command not found. This is how I'm attempting this:

ssh -o StrictHostKeyChecking=no -i /Path/to/key-pair.pem ubuntu@${ec2ips[i]} 'bash ' << 'STARTAPP'
    cd ~/my-app-folder
    pm2 start ./bin/www --name 'my-app'

when I ssh in normally, all pm2 commands run fine. If I ssh in and run a script with pm2 in it, it also works as expected. It's only when I try and run it on a remote machine from my machine.

1 Answer 1


When you run an interactive shell it sets the PATH variable according to your "rc" files (e.g .bash_profile). When you run that "non-interactive" ssh command then many of these scripts aren't run and so PATH isn't set.

You should include the pathname to pm2 even if it is the current directory (eg ./pm2 or /path/to/pm2) to ensure it is found, or else export PATH=.... before the call to pm2.

  • i did 'which pm2' which should get me the path to the executable, correct me if i'm wrong. then i tried doing /home/ubuntu/.nvm/versions/node/v6.0.0/bin/pm2 start ./bin/www --name 'my-app' but that is still giving me command not found. did i misunderstand your solution?
    – user137717
    Jun 29, 2016 at 4:50
  • 1
    The point is your execution environments are different; does pm2 use other variables to find things? Login and run the env command and compare that to ssh server env; there will be a LOT of differences. Do any of those variables impact the execution of the command? Jun 29, 2016 at 12:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .