Paramiko doesn't return grep correctly











up vote
1
down vote

favorite












I am building myself a tool to run/kill scripts on my VPS over SSH. So far I was able to do exactly that (start or kill processes) but I can't manage to get the ps -fA | grep python command to work.



Some of my scripts actually spawn new scripts with Popen so I really need a way to check the PID of python scripts through SSH (and the name of the file the PID belongs to).



    ssh_obj = self.get_ssh_connection()
stdin, stdout, stderr = ssh_obj.exec_command('ps -fA | grep python')
try:
stdin_read = "stdin: {0}".format(stdin.readline())
except Exception as e:
stdin_read = "stdin: ERROR " + str(e)
try:
stdout_read = "stdout: {0}".format(stdout.readline())
except Exception as e:
stdout_read = "stdout: ERROR " + str(e)
try:
stderr_read = "stderr: {0}".format(stderr.readline())
except Exception as e:
stderr_read = "stderr: ERROR " + str(e)
print("n".join([stdin_read, stdout_read, stderr_read]))


But it doesn't works, the result it shows to me is:



stdin: ERROR File not open for reading
stdout: root 739 738 0 17:12 ? 00:00:00 bash -c ps -fA | grep python
stderr:


While the desired output would be something like:



PID: 123 - home/whatever/myfile.py
PID: 125 - home/whatever/myfile2.py
PID: 126 - home/whatever/myfile.py


That way I'll know which PIDs to kill for myfile script (123 and 126).



Bonus question: I'm not very linux experienced, does executing grep commands outside of the Terminal creates any PID that I have to manually kill?










share|improve this question


























    up vote
    1
    down vote

    favorite












    I am building myself a tool to run/kill scripts on my VPS over SSH. So far I was able to do exactly that (start or kill processes) but I can't manage to get the ps -fA | grep python command to work.



    Some of my scripts actually spawn new scripts with Popen so I really need a way to check the PID of python scripts through SSH (and the name of the file the PID belongs to).



        ssh_obj = self.get_ssh_connection()
    stdin, stdout, stderr = ssh_obj.exec_command('ps -fA | grep python')
    try:
    stdin_read = "stdin: {0}".format(stdin.readline())
    except Exception as e:
    stdin_read = "stdin: ERROR " + str(e)
    try:
    stdout_read = "stdout: {0}".format(stdout.readline())
    except Exception as e:
    stdout_read = "stdout: ERROR " + str(e)
    try:
    stderr_read = "stderr: {0}".format(stderr.readline())
    except Exception as e:
    stderr_read = "stderr: ERROR " + str(e)
    print("n".join([stdin_read, stdout_read, stderr_read]))


    But it doesn't works, the result it shows to me is:



    stdin: ERROR File not open for reading
    stdout: root 739 738 0 17:12 ? 00:00:00 bash -c ps -fA | grep python
    stderr:


    While the desired output would be something like:



    PID: 123 - home/whatever/myfile.py
    PID: 125 - home/whatever/myfile2.py
    PID: 126 - home/whatever/myfile.py


    That way I'll know which PIDs to kill for myfile script (123 and 126).



    Bonus question: I'm not very linux experienced, does executing grep commands outside of the Terminal creates any PID that I have to manually kill?










    share|improve this question
























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I am building myself a tool to run/kill scripts on my VPS over SSH. So far I was able to do exactly that (start or kill processes) but I can't manage to get the ps -fA | grep python command to work.



      Some of my scripts actually spawn new scripts with Popen so I really need a way to check the PID of python scripts through SSH (and the name of the file the PID belongs to).



          ssh_obj = self.get_ssh_connection()
      stdin, stdout, stderr = ssh_obj.exec_command('ps -fA | grep python')
      try:
      stdin_read = "stdin: {0}".format(stdin.readline())
      except Exception as e:
      stdin_read = "stdin: ERROR " + str(e)
      try:
      stdout_read = "stdout: {0}".format(stdout.readline())
      except Exception as e:
      stdout_read = "stdout: ERROR " + str(e)
      try:
      stderr_read = "stderr: {0}".format(stderr.readline())
      except Exception as e:
      stderr_read = "stderr: ERROR " + str(e)
      print("n".join([stdin_read, stdout_read, stderr_read]))


      But it doesn't works, the result it shows to me is:



      stdin: ERROR File not open for reading
      stdout: root 739 738 0 17:12 ? 00:00:00 bash -c ps -fA | grep python
      stderr:


      While the desired output would be something like:



      PID: 123 - home/whatever/myfile.py
      PID: 125 - home/whatever/myfile2.py
      PID: 126 - home/whatever/myfile.py


      That way I'll know which PIDs to kill for myfile script (123 and 126).



      Bonus question: I'm not very linux experienced, does executing grep commands outside of the Terminal creates any PID that I have to manually kill?










      share|improve this question













      I am building myself a tool to run/kill scripts on my VPS over SSH. So far I was able to do exactly that (start or kill processes) but I can't manage to get the ps -fA | grep python command to work.



      Some of my scripts actually spawn new scripts with Popen so I really need a way to check the PID of python scripts through SSH (and the name of the file the PID belongs to).



          ssh_obj = self.get_ssh_connection()
      stdin, stdout, stderr = ssh_obj.exec_command('ps -fA | grep python')
      try:
      stdin_read = "stdin: {0}".format(stdin.readline())
      except Exception as e:
      stdin_read = "stdin: ERROR " + str(e)
      try:
      stdout_read = "stdout: {0}".format(stdout.readline())
      except Exception as e:
      stdout_read = "stdout: ERROR " + str(e)
      try:
      stderr_read = "stderr: {0}".format(stderr.readline())
      except Exception as e:
      stderr_read = "stderr: ERROR " + str(e)
      print("n".join([stdin_read, stdout_read, stderr_read]))


      But it doesn't works, the result it shows to me is:



      stdin: ERROR File not open for reading
      stdout: root 739 738 0 17:12 ? 00:00:00 bash -c ps -fA | grep python
      stderr:


      While the desired output would be something like:



      PID: 123 - home/whatever/myfile.py
      PID: 125 - home/whatever/myfile2.py
      PID: 126 - home/whatever/myfile.py


      That way I'll know which PIDs to kill for myfile script (123 and 126).



      Bonus question: I'm not very linux experienced, does executing grep commands outside of the Terminal creates any PID that I have to manually kill?







      python python-3.x unix paramiko






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 20 at 16:13









      Saelyth

      69311223




      69311223
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          You may need to escape the pipe character by passing the whole statement in single quotes to a shell on the other end:



          ssh_obj.exec_command("sh -c 'ps -fA | grep python'")


          Alternatively you could try running pgrep:



          ssh_obj.exec_command('pgrep python')


          pgrep will search the current running processes that match the search string python and will send just the process IDs to the stdout.






          share|improve this answer





















          • pgrep wouldn't work for this. As I explained, there might be several different python scripts running and I need to know which ones to kill, so I need something more than just the pid (I need to know to which file each python script process belongs to). Your first suggestion works great but only returns the first result, any fix for that?
            – Saelyth
            Nov 20 at 17:34












          • Sorry, missed that you needed to know the file. Perhaps you could pass the -a switch to pgrep to list the full command line in addition to the pid. As for the grep itself only returning the first value, that's likely because you're calling stdout.readline() which would read up to the end of the first line. You'd need to call it repeatedly to get the other lines, or better, call read() which would read the whole lot.
            – Will Keeling
            Nov 20 at 17:54










          • There we go, read() did the trick.
            – Saelyth
            Nov 20 at 18:27











          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%2f53397132%2fparamiko-doesnt-return-grep-correctly%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








          up vote
          1
          down vote



          accepted










          You may need to escape the pipe character by passing the whole statement in single quotes to a shell on the other end:



          ssh_obj.exec_command("sh -c 'ps -fA | grep python'")


          Alternatively you could try running pgrep:



          ssh_obj.exec_command('pgrep python')


          pgrep will search the current running processes that match the search string python and will send just the process IDs to the stdout.






          share|improve this answer





















          • pgrep wouldn't work for this. As I explained, there might be several different python scripts running and I need to know which ones to kill, so I need something more than just the pid (I need to know to which file each python script process belongs to). Your first suggestion works great but only returns the first result, any fix for that?
            – Saelyth
            Nov 20 at 17:34












          • Sorry, missed that you needed to know the file. Perhaps you could pass the -a switch to pgrep to list the full command line in addition to the pid. As for the grep itself only returning the first value, that's likely because you're calling stdout.readline() which would read up to the end of the first line. You'd need to call it repeatedly to get the other lines, or better, call read() which would read the whole lot.
            – Will Keeling
            Nov 20 at 17:54










          • There we go, read() did the trick.
            – Saelyth
            Nov 20 at 18:27















          up vote
          1
          down vote



          accepted










          You may need to escape the pipe character by passing the whole statement in single quotes to a shell on the other end:



          ssh_obj.exec_command("sh -c 'ps -fA | grep python'")


          Alternatively you could try running pgrep:



          ssh_obj.exec_command('pgrep python')


          pgrep will search the current running processes that match the search string python and will send just the process IDs to the stdout.






          share|improve this answer





















          • pgrep wouldn't work for this. As I explained, there might be several different python scripts running and I need to know which ones to kill, so I need something more than just the pid (I need to know to which file each python script process belongs to). Your first suggestion works great but only returns the first result, any fix for that?
            – Saelyth
            Nov 20 at 17:34












          • Sorry, missed that you needed to know the file. Perhaps you could pass the -a switch to pgrep to list the full command line in addition to the pid. As for the grep itself only returning the first value, that's likely because you're calling stdout.readline() which would read up to the end of the first line. You'd need to call it repeatedly to get the other lines, or better, call read() which would read the whole lot.
            – Will Keeling
            Nov 20 at 17:54










          • There we go, read() did the trick.
            – Saelyth
            Nov 20 at 18:27













          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          You may need to escape the pipe character by passing the whole statement in single quotes to a shell on the other end:



          ssh_obj.exec_command("sh -c 'ps -fA | grep python'")


          Alternatively you could try running pgrep:



          ssh_obj.exec_command('pgrep python')


          pgrep will search the current running processes that match the search string python and will send just the process IDs to the stdout.






          share|improve this answer












          You may need to escape the pipe character by passing the whole statement in single quotes to a shell on the other end:



          ssh_obj.exec_command("sh -c 'ps -fA | grep python'")


          Alternatively you could try running pgrep:



          ssh_obj.exec_command('pgrep python')


          pgrep will search the current running processes that match the search string python and will send just the process IDs to the stdout.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 20 at 17:21









          Will Keeling

          10.5k22329




          10.5k22329












          • pgrep wouldn't work for this. As I explained, there might be several different python scripts running and I need to know which ones to kill, so I need something more than just the pid (I need to know to which file each python script process belongs to). Your first suggestion works great but only returns the first result, any fix for that?
            – Saelyth
            Nov 20 at 17:34












          • Sorry, missed that you needed to know the file. Perhaps you could pass the -a switch to pgrep to list the full command line in addition to the pid. As for the grep itself only returning the first value, that's likely because you're calling stdout.readline() which would read up to the end of the first line. You'd need to call it repeatedly to get the other lines, or better, call read() which would read the whole lot.
            – Will Keeling
            Nov 20 at 17:54










          • There we go, read() did the trick.
            – Saelyth
            Nov 20 at 18:27


















          • pgrep wouldn't work for this. As I explained, there might be several different python scripts running and I need to know which ones to kill, so I need something more than just the pid (I need to know to which file each python script process belongs to). Your first suggestion works great but only returns the first result, any fix for that?
            – Saelyth
            Nov 20 at 17:34












          • Sorry, missed that you needed to know the file. Perhaps you could pass the -a switch to pgrep to list the full command line in addition to the pid. As for the grep itself only returning the first value, that's likely because you're calling stdout.readline() which would read up to the end of the first line. You'd need to call it repeatedly to get the other lines, or better, call read() which would read the whole lot.
            – Will Keeling
            Nov 20 at 17:54










          • There we go, read() did the trick.
            – Saelyth
            Nov 20 at 18:27
















          pgrep wouldn't work for this. As I explained, there might be several different python scripts running and I need to know which ones to kill, so I need something more than just the pid (I need to know to which file each python script process belongs to). Your first suggestion works great but only returns the first result, any fix for that?
          – Saelyth
          Nov 20 at 17:34






          pgrep wouldn't work for this. As I explained, there might be several different python scripts running and I need to know which ones to kill, so I need something more than just the pid (I need to know to which file each python script process belongs to). Your first suggestion works great but only returns the first result, any fix for that?
          – Saelyth
          Nov 20 at 17:34














          Sorry, missed that you needed to know the file. Perhaps you could pass the -a switch to pgrep to list the full command line in addition to the pid. As for the grep itself only returning the first value, that's likely because you're calling stdout.readline() which would read up to the end of the first line. You'd need to call it repeatedly to get the other lines, or better, call read() which would read the whole lot.
          – Will Keeling
          Nov 20 at 17:54




          Sorry, missed that you needed to know the file. Perhaps you could pass the -a switch to pgrep to list the full command line in addition to the pid. As for the grep itself only returning the first value, that's likely because you're calling stdout.readline() which would read up to the end of the first line. You'd need to call it repeatedly to get the other lines, or better, call read() which would read the whole lot.
          – Will Keeling
          Nov 20 at 17:54












          There we go, read() did the trick.
          – Saelyth
          Nov 20 at 18:27




          There we go, read() did the trick.
          – Saelyth
          Nov 20 at 18:27


















          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%2f53397132%2fparamiko-doesnt-return-grep-correctly%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

          To store a contact into the json file from server.js file using a class in NodeJS

          Redirect URL with Chrome Remote Debugging Android Devices

          Dieringhausen