Run terminal command from nodejs app through pm2












0














I have an express server running under pm2 with just one route:



app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html);
}


and a socket.io event handler that listens for "open youtube" (from the previously sent index.html)



io.on('connection', function(socket){
socket.on('open youtube', function() {
exec('start chrome youtube.com'); //yes ik, this is a windows-only solution
console.log('Opening youtube...');
});
});


While everything does work, when emit is handled on the express server, the only part that works is the console.log() and not the exec() ( const exec = require('child_process').exec; )



What's the reason pm2 doesn't allow me to run a cmd command and/or how can I run cmd command from a nodejs app running under pm2 ?



EDIT:



When I run the server with npm start and not as a pm2 process, it opens a new tab ( as expected ) with youtube.com



EDIT 2:



For some reason, after I run pm2 reload ecosystem.config.js and pm2 save, it did work. The only change I made to ecosystem.config.js was to change exec_mode to exec_mode: 'cluster' instead of fork which is the default value.

I don't know if this was the reason or not.










share|improve this question





























    0














    I have an express server running under pm2 with just one route:



    app.get('/', (req, res) => {
    res.sendFile(__dirname + '/index.html);
    }


    and a socket.io event handler that listens for "open youtube" (from the previously sent index.html)



    io.on('connection', function(socket){
    socket.on('open youtube', function() {
    exec('start chrome youtube.com'); //yes ik, this is a windows-only solution
    console.log('Opening youtube...');
    });
    });


    While everything does work, when emit is handled on the express server, the only part that works is the console.log() and not the exec() ( const exec = require('child_process').exec; )



    What's the reason pm2 doesn't allow me to run a cmd command and/or how can I run cmd command from a nodejs app running under pm2 ?



    EDIT:



    When I run the server with npm start and not as a pm2 process, it opens a new tab ( as expected ) with youtube.com



    EDIT 2:



    For some reason, after I run pm2 reload ecosystem.config.js and pm2 save, it did work. The only change I made to ecosystem.config.js was to change exec_mode to exec_mode: 'cluster' instead of fork which is the default value.

    I don't know if this was the reason or not.










    share|improve this question



























      0












      0








      0


      1





      I have an express server running under pm2 with just one route:



      app.get('/', (req, res) => {
      res.sendFile(__dirname + '/index.html);
      }


      and a socket.io event handler that listens for "open youtube" (from the previously sent index.html)



      io.on('connection', function(socket){
      socket.on('open youtube', function() {
      exec('start chrome youtube.com'); //yes ik, this is a windows-only solution
      console.log('Opening youtube...');
      });
      });


      While everything does work, when emit is handled on the express server, the only part that works is the console.log() and not the exec() ( const exec = require('child_process').exec; )



      What's the reason pm2 doesn't allow me to run a cmd command and/or how can I run cmd command from a nodejs app running under pm2 ?



      EDIT:



      When I run the server with npm start and not as a pm2 process, it opens a new tab ( as expected ) with youtube.com



      EDIT 2:



      For some reason, after I run pm2 reload ecosystem.config.js and pm2 save, it did work. The only change I made to ecosystem.config.js was to change exec_mode to exec_mode: 'cluster' instead of fork which is the default value.

      I don't know if this was the reason or not.










      share|improve this question















      I have an express server running under pm2 with just one route:



      app.get('/', (req, res) => {
      res.sendFile(__dirname + '/index.html);
      }


      and a socket.io event handler that listens for "open youtube" (from the previously sent index.html)



      io.on('connection', function(socket){
      socket.on('open youtube', function() {
      exec('start chrome youtube.com'); //yes ik, this is a windows-only solution
      console.log('Opening youtube...');
      });
      });


      While everything does work, when emit is handled on the express server, the only part that works is the console.log() and not the exec() ( const exec = require('child_process').exec; )



      What's the reason pm2 doesn't allow me to run a cmd command and/or how can I run cmd command from a nodejs app running under pm2 ?



      EDIT:



      When I run the server with npm start and not as a pm2 process, it opens a new tab ( as expected ) with youtube.com



      EDIT 2:



      For some reason, after I run pm2 reload ecosystem.config.js and pm2 save, it did work. The only change I made to ecosystem.config.js was to change exec_mode to exec_mode: 'cluster' instead of fork which is the default value.

      I don't know if this was the reason or not.







      javascript node.js terminal socket.io pm2






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 17 '18 at 19:36

























      asked Nov 17 '18 at 16:32









      johnchar

      391519




      391519
























          1 Answer
          1






          active

          oldest

          votes


















          1














          exec(String) is an asynchronous function,
          You may have an error which your'e not aware of, pass a callback like this:



          exec('start chrome youtube.com', (error, stdout, stderr) => {
          if (error) {
          // handle error
          } else {
          // handle success
          }
          });


          And you can also use the synchronous version



          try {
          let res = execSync('start chrome youtube.com');
          } catch(err){
          // handle error
          return;
          }
          // handle success





          share|improve this answer





















          • with the first approach if(error) else, the else statement is ran and I successfully run console.log('youtube opened'); but chrome does not open
            – johnchar
            Nov 17 '18 at 19:22










          • Please print all (stdout, stderr) to verify you don't have any error
            – Mazki516
            Nov 17 '18 at 19:24










          • b.t.w, "start chrome url" is not a default alias . how did you setup this alias?
            – Mazki516
            Nov 17 '18 at 19:25






          • 1




            You changed how pm2 scales your app . With cluster mode (load balancing between workers) or just forks (individual workers)
            – Mazki516
            Nov 17 '18 at 19:37






          • 1




            I guess all it needed was a reload... Thank you for your time though
            – johnchar
            Nov 17 '18 at 19:40











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          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: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          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%2fstackoverflow.com%2fquestions%2f53353200%2frun-terminal-command-from-nodejs-app-through-pm2%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









          1














          exec(String) is an asynchronous function,
          You may have an error which your'e not aware of, pass a callback like this:



          exec('start chrome youtube.com', (error, stdout, stderr) => {
          if (error) {
          // handle error
          } else {
          // handle success
          }
          });


          And you can also use the synchronous version



          try {
          let res = execSync('start chrome youtube.com');
          } catch(err){
          // handle error
          return;
          }
          // handle success





          share|improve this answer





















          • with the first approach if(error) else, the else statement is ran and I successfully run console.log('youtube opened'); but chrome does not open
            – johnchar
            Nov 17 '18 at 19:22










          • Please print all (stdout, stderr) to verify you don't have any error
            – Mazki516
            Nov 17 '18 at 19:24










          • b.t.w, "start chrome url" is not a default alias . how did you setup this alias?
            – Mazki516
            Nov 17 '18 at 19:25






          • 1




            You changed how pm2 scales your app . With cluster mode (load balancing between workers) or just forks (individual workers)
            – Mazki516
            Nov 17 '18 at 19:37






          • 1




            I guess all it needed was a reload... Thank you for your time though
            – johnchar
            Nov 17 '18 at 19:40
















          1














          exec(String) is an asynchronous function,
          You may have an error which your'e not aware of, pass a callback like this:



          exec('start chrome youtube.com', (error, stdout, stderr) => {
          if (error) {
          // handle error
          } else {
          // handle success
          }
          });


          And you can also use the synchronous version



          try {
          let res = execSync('start chrome youtube.com');
          } catch(err){
          // handle error
          return;
          }
          // handle success





          share|improve this answer





















          • with the first approach if(error) else, the else statement is ran and I successfully run console.log('youtube opened'); but chrome does not open
            – johnchar
            Nov 17 '18 at 19:22










          • Please print all (stdout, stderr) to verify you don't have any error
            – Mazki516
            Nov 17 '18 at 19:24










          • b.t.w, "start chrome url" is not a default alias . how did you setup this alias?
            – Mazki516
            Nov 17 '18 at 19:25






          • 1




            You changed how pm2 scales your app . With cluster mode (load balancing between workers) or just forks (individual workers)
            – Mazki516
            Nov 17 '18 at 19:37






          • 1




            I guess all it needed was a reload... Thank you for your time though
            – johnchar
            Nov 17 '18 at 19:40














          1












          1








          1






          exec(String) is an asynchronous function,
          You may have an error which your'e not aware of, pass a callback like this:



          exec('start chrome youtube.com', (error, stdout, stderr) => {
          if (error) {
          // handle error
          } else {
          // handle success
          }
          });


          And you can also use the synchronous version



          try {
          let res = execSync('start chrome youtube.com');
          } catch(err){
          // handle error
          return;
          }
          // handle success





          share|improve this answer












          exec(String) is an asynchronous function,
          You may have an error which your'e not aware of, pass a callback like this:



          exec('start chrome youtube.com', (error, stdout, stderr) => {
          if (error) {
          // handle error
          } else {
          // handle success
          }
          });


          And you can also use the synchronous version



          try {
          let res = execSync('start chrome youtube.com');
          } catch(err){
          // handle error
          return;
          }
          // handle success






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 17 '18 at 19:10









          Mazki516

          5731416




          5731416












          • with the first approach if(error) else, the else statement is ran and I successfully run console.log('youtube opened'); but chrome does not open
            – johnchar
            Nov 17 '18 at 19:22










          • Please print all (stdout, stderr) to verify you don't have any error
            – Mazki516
            Nov 17 '18 at 19:24










          • b.t.w, "start chrome url" is not a default alias . how did you setup this alias?
            – Mazki516
            Nov 17 '18 at 19:25






          • 1




            You changed how pm2 scales your app . With cluster mode (load balancing between workers) or just forks (individual workers)
            – Mazki516
            Nov 17 '18 at 19:37






          • 1




            I guess all it needed was a reload... Thank you for your time though
            – johnchar
            Nov 17 '18 at 19:40


















          • with the first approach if(error) else, the else statement is ran and I successfully run console.log('youtube opened'); but chrome does not open
            – johnchar
            Nov 17 '18 at 19:22










          • Please print all (stdout, stderr) to verify you don't have any error
            – Mazki516
            Nov 17 '18 at 19:24










          • b.t.w, "start chrome url" is not a default alias . how did you setup this alias?
            – Mazki516
            Nov 17 '18 at 19:25






          • 1




            You changed how pm2 scales your app . With cluster mode (load balancing between workers) or just forks (individual workers)
            – Mazki516
            Nov 17 '18 at 19:37






          • 1




            I guess all it needed was a reload... Thank you for your time though
            – johnchar
            Nov 17 '18 at 19:40
















          with the first approach if(error) else, the else statement is ran and I successfully run console.log('youtube opened'); but chrome does not open
          – johnchar
          Nov 17 '18 at 19:22




          with the first approach if(error) else, the else statement is ran and I successfully run console.log('youtube opened'); but chrome does not open
          – johnchar
          Nov 17 '18 at 19:22












          Please print all (stdout, stderr) to verify you don't have any error
          – Mazki516
          Nov 17 '18 at 19:24




          Please print all (stdout, stderr) to verify you don't have any error
          – Mazki516
          Nov 17 '18 at 19:24












          b.t.w, "start chrome url" is not a default alias . how did you setup this alias?
          – Mazki516
          Nov 17 '18 at 19:25




          b.t.w, "start chrome url" is not a default alias . how did you setup this alias?
          – Mazki516
          Nov 17 '18 at 19:25




          1




          1




          You changed how pm2 scales your app . With cluster mode (load balancing between workers) or just forks (individual workers)
          – Mazki516
          Nov 17 '18 at 19:37




          You changed how pm2 scales your app . With cluster mode (load balancing between workers) or just forks (individual workers)
          – Mazki516
          Nov 17 '18 at 19:37




          1




          1




          I guess all it needed was a reload... Thank you for your time though
          – johnchar
          Nov 17 '18 at 19:40




          I guess all it needed was a reload... Thank you for your time though
          – johnchar
          Nov 17 '18 at 19:40


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


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





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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%2fstackoverflow.com%2fquestions%2f53353200%2frun-terminal-command-from-nodejs-app-through-pm2%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 send String Array data to Server using php in android

          Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

          Is anime1.com a legal site for watching anime?