run pm2 from remote shell script












1















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'
exit
STARTAPP


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.










share|improve this question



























    1















    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'
    exit
    STARTAPP


    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.










    share|improve this question

























      1












      1








      1








      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'
      exit
      STARTAPP


      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.










      share|improve this question














      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'
      exit
      STARTAPP


      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.







      shell-script remote






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jun 29 '16 at 2:12









      user137717user137717

      1062




      1062






















          1 Answer
          1






          active

          oldest

          votes


















          0














          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.






          share|improve this answer
























          • 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 '16 at 4:50











          • 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?

            – Stephen Harris
            Jun 29 '16 at 12:38











          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "106"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f292740%2frun-pm2-from-remote-shell-script%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          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.






          share|improve this answer
























          • 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 '16 at 4:50











          • 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?

            – Stephen Harris
            Jun 29 '16 at 12:38
















          0














          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.






          share|improve this answer
























          • 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 '16 at 4:50











          • 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?

            – Stephen Harris
            Jun 29 '16 at 12:38














          0












          0








          0







          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.






          share|improve this answer













          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.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jun 29 '16 at 2:22









          Stephen HarrisStephen Harris

          25.5k24477




          25.5k24477













          • 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 '16 at 4:50











          • 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?

            – Stephen Harris
            Jun 29 '16 at 12:38



















          • 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 '16 at 4:50











          • 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?

            – Stephen Harris
            Jun 29 '16 at 12:38

















          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 '16 at 4:50





          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 '16 at 4:50













          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?

          – Stephen Harris
          Jun 29 '16 at 12:38





          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?

          – Stephen Harris
          Jun 29 '16 at 12:38


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Unix & Linux Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f292740%2frun-pm2-from-remote-shell-script%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          How to make a Squid Proxy server?

          Is this a new Fibonacci Identity?

          19世紀