SSH to a remote server and find the specific pid and kill it











up vote
0
down vote

favorite












I want to remotely log into a server and then find the pid of the specific port. After that kill that pid.



I tried this,



ssh -T test@192.168.94.139 ; "netstat -lnpt ; awk '$4 ~ /:2020$/ {sub(//.*/, "", $7); print $7}'" 


I should do this just only using netstat command










share|improve this question


























    up vote
    0
    down vote

    favorite












    I want to remotely log into a server and then find the pid of the specific port. After that kill that pid.



    I tried this,



    ssh -T test@192.168.94.139 ; "netstat -lnpt ; awk '$4 ~ /:2020$/ {sub(//.*/, "", $7); print $7}'" 


    I should do this just only using netstat command










    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I want to remotely log into a server and then find the pid of the specific port. After that kill that pid.



      I tried this,



      ssh -T test@192.168.94.139 ; "netstat -lnpt ; awk '$4 ~ /:2020$/ {sub(//.*/, "", $7); print $7}'" 


      I should do this just only using netstat command










      share|improve this question













      I want to remotely log into a server and then find the pid of the specific port. After that kill that pid.



      I tried this,



      ssh -T test@192.168.94.139 ; "netstat -lnpt ; awk '$4 ~ /:2020$/ {sub(//.*/, "", $7); print $7}'" 


      I should do this just only using netstat command







      linux shell






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 26 at 11:54









      Janith

      84




      84






















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          You miss the piping feature (| instead of ;) in your command. I propose this one:



          ssh -t test@192.168.94.139  "sudo netstat -plnt | grep 2020 | awk '{print $7}' | sed s+/.*++g | sort -u | xargs kill"


          I assume here you have sudo installed and permissions to use it on your server. My tests showed you may need it in order to get netstat to work properly with -p option.



          I prefer here using grep+sed to avoid over-complicated (in my opinion) awk script.





          • -t : allows you to enter your user password to perform sudo command


          • grep 2020 : filters the port number


          • awk '{print $7}' : lets awk choose the right field


          • sed s+/.*++g : removes the / and following characters (note: the + sign replaces the traditional / here for shortness)


          • sort -u : just in case you would have several processes working with the port (happens if both IPv4 and IPv6 are used)


          • xargs kill : calls kill using the standard input as a parameter


          You may need to add sudo to the last one, if the process to kill is owned by another user: sudo xargs kill



          As an example, you could also use variables (note here the semi-colons used to separate commands without piping) :



          server=test@192.168.94.139 ; port=2020 ; ssh -t $server "sudo netstat -plnt | grep $port | awk '{print $7}' | sed s+/.*++g | sort -u | xargs kill"





          share|improve this answer





















          • sometimes xargs kill working and sometimes not. Then I put xargs -i kill -kill {} and It is working fine.
            – Janith
            Nov 27 at 4:30












          • kill -kill will forcibly kill the process (while kill will gently ask for the process to terminate, but it may not do it)
            – lauhub
            Nov 27 at 9:15










          • For debugging purposes, you can add the -t option to xargs (it will echo the command to be executed): xargs -t kill
            – lauhub
            Nov 27 at 9:15


















          up vote
          0
          down vote













          You got close enough to your goal. There are some missing points though.




          1. No Semicolon after host IP required, the remote command should go right after the IP. So, remove the semicolon.

          2. Replace the other semicolon in front of awk with a pipe sign | - that way the netstat output will go to the awk.

          3. All the dollar sign needs to be escaped with a backslash. otherwise it is evaluated on the local machine


          You will get the PID printed after above points done (if such is there). Then you will need to pass that PID to kill command. I'd suggest you use xargs kill as it looks the simplest here. PID will be accepted by kill command that way.



          So the final version may look like:



          ssh -T test@192.168.94.139 "netstat -lnpt | awk '$4 ~ /:2020$/ {sub(//.*/, "", $7); print $7}') | xargs kill"


          Not the best look, but i suits your approach.






          share|improve this answer





















          • Thank you for your support. I got errors. sudo ssh -T zissa@192.168.94.139 "sudo netstat -lnpt | awk '$4 ~ /:2020$/ {sub(//.*/, "", $7); print $7}'" awk: cmd. line:1: $4 ~ /:2020$/ {sub(//.*/, , $7); print $7} awk: cmd. line:1: ^ syntax error awk: cmd. line:1: $4 ~ /:2020$/ {sub(//.*/, , $7); print $7} awk: cmd. line:1: ^ 1 is invalid as number of arguments for sub
            – Janith
            Nov 27 at 6:45












          • netstat output may vary, so the column 7 did not look as expected. if you are still solving ti, i'd suggest you to look at the netstat output to figure out how the awk needs to be adjusted for the line of interest to extract pid
            – Tagwint
            Nov 27 at 10:57











          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',
          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%2f484184%2fssh-to-a-remote-server-and-find-the-specific-pid-and-kill-it%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote



          accepted










          You miss the piping feature (| instead of ;) in your command. I propose this one:



          ssh -t test@192.168.94.139  "sudo netstat -plnt | grep 2020 | awk '{print $7}' | sed s+/.*++g | sort -u | xargs kill"


          I assume here you have sudo installed and permissions to use it on your server. My tests showed you may need it in order to get netstat to work properly with -p option.



          I prefer here using grep+sed to avoid over-complicated (in my opinion) awk script.





          • -t : allows you to enter your user password to perform sudo command


          • grep 2020 : filters the port number


          • awk '{print $7}' : lets awk choose the right field


          • sed s+/.*++g : removes the / and following characters (note: the + sign replaces the traditional / here for shortness)


          • sort -u : just in case you would have several processes working with the port (happens if both IPv4 and IPv6 are used)


          • xargs kill : calls kill using the standard input as a parameter


          You may need to add sudo to the last one, if the process to kill is owned by another user: sudo xargs kill



          As an example, you could also use variables (note here the semi-colons used to separate commands without piping) :



          server=test@192.168.94.139 ; port=2020 ; ssh -t $server "sudo netstat -plnt | grep $port | awk '{print $7}' | sed s+/.*++g | sort -u | xargs kill"





          share|improve this answer





















          • sometimes xargs kill working and sometimes not. Then I put xargs -i kill -kill {} and It is working fine.
            – Janith
            Nov 27 at 4:30












          • kill -kill will forcibly kill the process (while kill will gently ask for the process to terminate, but it may not do it)
            – lauhub
            Nov 27 at 9:15










          • For debugging purposes, you can add the -t option to xargs (it will echo the command to be executed): xargs -t kill
            – lauhub
            Nov 27 at 9:15















          up vote
          0
          down vote



          accepted










          You miss the piping feature (| instead of ;) in your command. I propose this one:



          ssh -t test@192.168.94.139  "sudo netstat -plnt | grep 2020 | awk '{print $7}' | sed s+/.*++g | sort -u | xargs kill"


          I assume here you have sudo installed and permissions to use it on your server. My tests showed you may need it in order to get netstat to work properly with -p option.



          I prefer here using grep+sed to avoid over-complicated (in my opinion) awk script.





          • -t : allows you to enter your user password to perform sudo command


          • grep 2020 : filters the port number


          • awk '{print $7}' : lets awk choose the right field


          • sed s+/.*++g : removes the / and following characters (note: the + sign replaces the traditional / here for shortness)


          • sort -u : just in case you would have several processes working with the port (happens if both IPv4 and IPv6 are used)


          • xargs kill : calls kill using the standard input as a parameter


          You may need to add sudo to the last one, if the process to kill is owned by another user: sudo xargs kill



          As an example, you could also use variables (note here the semi-colons used to separate commands without piping) :



          server=test@192.168.94.139 ; port=2020 ; ssh -t $server "sudo netstat -plnt | grep $port | awk '{print $7}' | sed s+/.*++g | sort -u | xargs kill"





          share|improve this answer





















          • sometimes xargs kill working and sometimes not. Then I put xargs -i kill -kill {} and It is working fine.
            – Janith
            Nov 27 at 4:30












          • kill -kill will forcibly kill the process (while kill will gently ask for the process to terminate, but it may not do it)
            – lauhub
            Nov 27 at 9:15










          • For debugging purposes, you can add the -t option to xargs (it will echo the command to be executed): xargs -t kill
            – lauhub
            Nov 27 at 9:15













          up vote
          0
          down vote



          accepted







          up vote
          0
          down vote



          accepted






          You miss the piping feature (| instead of ;) in your command. I propose this one:



          ssh -t test@192.168.94.139  "sudo netstat -plnt | grep 2020 | awk '{print $7}' | sed s+/.*++g | sort -u | xargs kill"


          I assume here you have sudo installed and permissions to use it on your server. My tests showed you may need it in order to get netstat to work properly with -p option.



          I prefer here using grep+sed to avoid over-complicated (in my opinion) awk script.





          • -t : allows you to enter your user password to perform sudo command


          • grep 2020 : filters the port number


          • awk '{print $7}' : lets awk choose the right field


          • sed s+/.*++g : removes the / and following characters (note: the + sign replaces the traditional / here for shortness)


          • sort -u : just in case you would have several processes working with the port (happens if both IPv4 and IPv6 are used)


          • xargs kill : calls kill using the standard input as a parameter


          You may need to add sudo to the last one, if the process to kill is owned by another user: sudo xargs kill



          As an example, you could also use variables (note here the semi-colons used to separate commands without piping) :



          server=test@192.168.94.139 ; port=2020 ; ssh -t $server "sudo netstat -plnt | grep $port | awk '{print $7}' | sed s+/.*++g | sort -u | xargs kill"





          share|improve this answer












          You miss the piping feature (| instead of ;) in your command. I propose this one:



          ssh -t test@192.168.94.139  "sudo netstat -plnt | grep 2020 | awk '{print $7}' | sed s+/.*++g | sort -u | xargs kill"


          I assume here you have sudo installed and permissions to use it on your server. My tests showed you may need it in order to get netstat to work properly with -p option.



          I prefer here using grep+sed to avoid over-complicated (in my opinion) awk script.





          • -t : allows you to enter your user password to perform sudo command


          • grep 2020 : filters the port number


          • awk '{print $7}' : lets awk choose the right field


          • sed s+/.*++g : removes the / and following characters (note: the + sign replaces the traditional / here for shortness)


          • sort -u : just in case you would have several processes working with the port (happens if both IPv4 and IPv6 are used)


          • xargs kill : calls kill using the standard input as a parameter


          You may need to add sudo to the last one, if the process to kill is owned by another user: sudo xargs kill



          As an example, you could also use variables (note here the semi-colons used to separate commands without piping) :



          server=test@192.168.94.139 ; port=2020 ; ssh -t $server "sudo netstat -plnt | grep $port | awk '{print $7}' | sed s+/.*++g | sort -u | xargs kill"






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 26 at 13:52









          lauhub

          430616




          430616












          • sometimes xargs kill working and sometimes not. Then I put xargs -i kill -kill {} and It is working fine.
            – Janith
            Nov 27 at 4:30












          • kill -kill will forcibly kill the process (while kill will gently ask for the process to terminate, but it may not do it)
            – lauhub
            Nov 27 at 9:15










          • For debugging purposes, you can add the -t option to xargs (it will echo the command to be executed): xargs -t kill
            – lauhub
            Nov 27 at 9:15


















          • sometimes xargs kill working and sometimes not. Then I put xargs -i kill -kill {} and It is working fine.
            – Janith
            Nov 27 at 4:30












          • kill -kill will forcibly kill the process (while kill will gently ask for the process to terminate, but it may not do it)
            – lauhub
            Nov 27 at 9:15










          • For debugging purposes, you can add the -t option to xargs (it will echo the command to be executed): xargs -t kill
            – lauhub
            Nov 27 at 9:15
















          sometimes xargs kill working and sometimes not. Then I put xargs -i kill -kill {} and It is working fine.
          – Janith
          Nov 27 at 4:30






          sometimes xargs kill working and sometimes not. Then I put xargs -i kill -kill {} and It is working fine.
          – Janith
          Nov 27 at 4:30














          kill -kill will forcibly kill the process (while kill will gently ask for the process to terminate, but it may not do it)
          – lauhub
          Nov 27 at 9:15




          kill -kill will forcibly kill the process (while kill will gently ask for the process to terminate, but it may not do it)
          – lauhub
          Nov 27 at 9:15












          For debugging purposes, you can add the -t option to xargs (it will echo the command to be executed): xargs -t kill
          – lauhub
          Nov 27 at 9:15




          For debugging purposes, you can add the -t option to xargs (it will echo the command to be executed): xargs -t kill
          – lauhub
          Nov 27 at 9:15












          up vote
          0
          down vote













          You got close enough to your goal. There are some missing points though.




          1. No Semicolon after host IP required, the remote command should go right after the IP. So, remove the semicolon.

          2. Replace the other semicolon in front of awk with a pipe sign | - that way the netstat output will go to the awk.

          3. All the dollar sign needs to be escaped with a backslash. otherwise it is evaluated on the local machine


          You will get the PID printed after above points done (if such is there). Then you will need to pass that PID to kill command. I'd suggest you use xargs kill as it looks the simplest here. PID will be accepted by kill command that way.



          So the final version may look like:



          ssh -T test@192.168.94.139 "netstat -lnpt | awk '$4 ~ /:2020$/ {sub(//.*/, "", $7); print $7}') | xargs kill"


          Not the best look, but i suits your approach.






          share|improve this answer





















          • Thank you for your support. I got errors. sudo ssh -T zissa@192.168.94.139 "sudo netstat -lnpt | awk '$4 ~ /:2020$/ {sub(//.*/, "", $7); print $7}'" awk: cmd. line:1: $4 ~ /:2020$/ {sub(//.*/, , $7); print $7} awk: cmd. line:1: ^ syntax error awk: cmd. line:1: $4 ~ /:2020$/ {sub(//.*/, , $7); print $7} awk: cmd. line:1: ^ 1 is invalid as number of arguments for sub
            – Janith
            Nov 27 at 6:45












          • netstat output may vary, so the column 7 did not look as expected. if you are still solving ti, i'd suggest you to look at the netstat output to figure out how the awk needs to be adjusted for the line of interest to extract pid
            – Tagwint
            Nov 27 at 10:57















          up vote
          0
          down vote













          You got close enough to your goal. There are some missing points though.




          1. No Semicolon after host IP required, the remote command should go right after the IP. So, remove the semicolon.

          2. Replace the other semicolon in front of awk with a pipe sign | - that way the netstat output will go to the awk.

          3. All the dollar sign needs to be escaped with a backslash. otherwise it is evaluated on the local machine


          You will get the PID printed after above points done (if such is there). Then you will need to pass that PID to kill command. I'd suggest you use xargs kill as it looks the simplest here. PID will be accepted by kill command that way.



          So the final version may look like:



          ssh -T test@192.168.94.139 "netstat -lnpt | awk '$4 ~ /:2020$/ {sub(//.*/, "", $7); print $7}') | xargs kill"


          Not the best look, but i suits your approach.






          share|improve this answer





















          • Thank you for your support. I got errors. sudo ssh -T zissa@192.168.94.139 "sudo netstat -lnpt | awk '$4 ~ /:2020$/ {sub(//.*/, "", $7); print $7}'" awk: cmd. line:1: $4 ~ /:2020$/ {sub(//.*/, , $7); print $7} awk: cmd. line:1: ^ syntax error awk: cmd. line:1: $4 ~ /:2020$/ {sub(//.*/, , $7); print $7} awk: cmd. line:1: ^ 1 is invalid as number of arguments for sub
            – Janith
            Nov 27 at 6:45












          • netstat output may vary, so the column 7 did not look as expected. if you are still solving ti, i'd suggest you to look at the netstat output to figure out how the awk needs to be adjusted for the line of interest to extract pid
            – Tagwint
            Nov 27 at 10:57













          up vote
          0
          down vote










          up vote
          0
          down vote









          You got close enough to your goal. There are some missing points though.




          1. No Semicolon after host IP required, the remote command should go right after the IP. So, remove the semicolon.

          2. Replace the other semicolon in front of awk with a pipe sign | - that way the netstat output will go to the awk.

          3. All the dollar sign needs to be escaped with a backslash. otherwise it is evaluated on the local machine


          You will get the PID printed after above points done (if such is there). Then you will need to pass that PID to kill command. I'd suggest you use xargs kill as it looks the simplest here. PID will be accepted by kill command that way.



          So the final version may look like:



          ssh -T test@192.168.94.139 "netstat -lnpt | awk '$4 ~ /:2020$/ {sub(//.*/, "", $7); print $7}') | xargs kill"


          Not the best look, but i suits your approach.






          share|improve this answer












          You got close enough to your goal. There are some missing points though.




          1. No Semicolon after host IP required, the remote command should go right after the IP. So, remove the semicolon.

          2. Replace the other semicolon in front of awk with a pipe sign | - that way the netstat output will go to the awk.

          3. All the dollar sign needs to be escaped with a backslash. otherwise it is evaluated on the local machine


          You will get the PID printed after above points done (if such is there). Then you will need to pass that PID to kill command. I'd suggest you use xargs kill as it looks the simplest here. PID will be accepted by kill command that way.



          So the final version may look like:



          ssh -T test@192.168.94.139 "netstat -lnpt | awk '$4 ~ /:2020$/ {sub(//.*/, "", $7); print $7}') | xargs kill"


          Not the best look, but i suits your approach.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 26 at 13:33









          Tagwint

          1,4481714




          1,4481714












          • Thank you for your support. I got errors. sudo ssh -T zissa@192.168.94.139 "sudo netstat -lnpt | awk '$4 ~ /:2020$/ {sub(//.*/, "", $7); print $7}'" awk: cmd. line:1: $4 ~ /:2020$/ {sub(//.*/, , $7); print $7} awk: cmd. line:1: ^ syntax error awk: cmd. line:1: $4 ~ /:2020$/ {sub(//.*/, , $7); print $7} awk: cmd. line:1: ^ 1 is invalid as number of arguments for sub
            – Janith
            Nov 27 at 6:45












          • netstat output may vary, so the column 7 did not look as expected. if you are still solving ti, i'd suggest you to look at the netstat output to figure out how the awk needs to be adjusted for the line of interest to extract pid
            – Tagwint
            Nov 27 at 10:57


















          • Thank you for your support. I got errors. sudo ssh -T zissa@192.168.94.139 "sudo netstat -lnpt | awk '$4 ~ /:2020$/ {sub(//.*/, "", $7); print $7}'" awk: cmd. line:1: $4 ~ /:2020$/ {sub(//.*/, , $7); print $7} awk: cmd. line:1: ^ syntax error awk: cmd. line:1: $4 ~ /:2020$/ {sub(//.*/, , $7); print $7} awk: cmd. line:1: ^ 1 is invalid as number of arguments for sub
            – Janith
            Nov 27 at 6:45












          • netstat output may vary, so the column 7 did not look as expected. if you are still solving ti, i'd suggest you to look at the netstat output to figure out how the awk needs to be adjusted for the line of interest to extract pid
            – Tagwint
            Nov 27 at 10:57
















          Thank you for your support. I got errors. sudo ssh -T zissa@192.168.94.139 "sudo netstat -lnpt | awk '$4 ~ /:2020$/ {sub(//.*/, "", $7); print $7}'" awk: cmd. line:1: $4 ~ /:2020$/ {sub(//.*/, , $7); print $7} awk: cmd. line:1: ^ syntax error awk: cmd. line:1: $4 ~ /:2020$/ {sub(//.*/, , $7); print $7} awk: cmd. line:1: ^ 1 is invalid as number of arguments for sub
          – Janith
          Nov 27 at 6:45






          Thank you for your support. I got errors. sudo ssh -T zissa@192.168.94.139 "sudo netstat -lnpt | awk '$4 ~ /:2020$/ {sub(//.*/, "", $7); print $7}'" awk: cmd. line:1: $4 ~ /:2020$/ {sub(//.*/, , $7); print $7} awk: cmd. line:1: ^ syntax error awk: cmd. line:1: $4 ~ /:2020$/ {sub(//.*/, , $7); print $7} awk: cmd. line:1: ^ 1 is invalid as number of arguments for sub
          – Janith
          Nov 27 at 6:45














          netstat output may vary, so the column 7 did not look as expected. if you are still solving ti, i'd suggest you to look at the netstat output to figure out how the awk needs to be adjusted for the line of interest to extract pid
          – Tagwint
          Nov 27 at 10:57




          netstat output may vary, so the column 7 did not look as expected. if you are still solving ti, i'd suggest you to look at the netstat output to figure out how the awk needs to be adjusted for the line of interest to extract pid
          – Tagwint
          Nov 27 at 10:57


















          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.





          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%2funix.stackexchange.com%2fquestions%2f484184%2fssh-to-a-remote-server-and-find-the-specific-pid-and-kill-it%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

          List directoties down one level, excluding some named directories and files

          list processes belonging to a network namespace

          list systemd RuntimeDirectory mounts