How to write a shell script to check port and kill it?











up vote
1
down vote

favorite
1












I have a command to check the port and kill specific pid running on that port. But I want to modify it to,




  1. Check port is busy or available

  2. If the port is busy then kill the pid


How can I do this?



Code is given below.



sudo netstat -nlpt | grep 2020 | awk '{print $7}' | sed s+/.*++g | sort -u | xargs -i kill -kill {}









share|improve this question


















  • 2




    save sed, awk and sort with: kill -kill $(sudo netstat -nlpt | grep -oPm1 '2020.*/K.*')
    – dessert
    Dec 6 at 7:05

















up vote
1
down vote

favorite
1












I have a command to check the port and kill specific pid running on that port. But I want to modify it to,




  1. Check port is busy or available

  2. If the port is busy then kill the pid


How can I do this?



Code is given below.



sudo netstat -nlpt | grep 2020 | awk '{print $7}' | sed s+/.*++g | sort -u | xargs -i kill -kill {}









share|improve this question


















  • 2




    save sed, awk and sort with: kill -kill $(sudo netstat -nlpt | grep -oPm1 '2020.*/K.*')
    – dessert
    Dec 6 at 7:05















up vote
1
down vote

favorite
1









up vote
1
down vote

favorite
1






1





I have a command to check the port and kill specific pid running on that port. But I want to modify it to,




  1. Check port is busy or available

  2. If the port is busy then kill the pid


How can I do this?



Code is given below.



sudo netstat -nlpt | grep 2020 | awk '{print $7}' | sed s+/.*++g | sort -u | xargs -i kill -kill {}









share|improve this question













I have a command to check the port and kill specific pid running on that port. But I want to modify it to,




  1. Check port is busy or available

  2. If the port is busy then kill the pid


How can I do this?



Code is given below.



sudo netstat -nlpt | grep 2020 | awk '{print $7}' | sed s+/.*++g | sort -u | xargs -i kill -kill {}






bash scripts






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Dec 6 at 4:40









Janith

647




647








  • 2




    save sed, awk and sort with: kill -kill $(sudo netstat -nlpt | grep -oPm1 '2020.*/K.*')
    – dessert
    Dec 6 at 7:05
















  • 2




    save sed, awk and sort with: kill -kill $(sudo netstat -nlpt | grep -oPm1 '2020.*/K.*')
    – dessert
    Dec 6 at 7:05










2




2




save sed, awk and sort with: kill -kill $(sudo netstat -nlpt | grep -oPm1 '2020.*/K.*')
– dessert
Dec 6 at 7:05






save sed, awk and sort with: kill -kill $(sudo netstat -nlpt | grep -oPm1 '2020.*/K.*')
– dessert
Dec 6 at 7:05












1 Answer
1






active

oldest

votes

















up vote
4
down vote













You can make use of this command to delete port in Ubuntu.



sudo kill $(sudo lsof -t -i:portno)


The lsof command or list of open files with -i flag will find out all the running process of specific port and gives the corresponding process Id. The kill option will terminate the process with pid given from the lsof command.



So if you want to kill process running on port 3000, you will have to execute the following command



sudo kill $(sudo lsof -t -i:3000)





share|improve this answer





















  • But first I want to check the port hang by someone, and if it is true then kill it.
    – Janith
    Dec 7 at 6:12










  • Well , this will actually check all the process using the port initially and will kill all process attached to it
    – Sachu Shaji Abraham
    Dec 7 at 6:15











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "89"
};
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: 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%2faskubuntu.com%2fquestions%2f1098839%2fhow-to-write-a-shell-script-to-check-port-and-kill-it%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
4
down vote













You can make use of this command to delete port in Ubuntu.



sudo kill $(sudo lsof -t -i:portno)


The lsof command or list of open files with -i flag will find out all the running process of specific port and gives the corresponding process Id. The kill option will terminate the process with pid given from the lsof command.



So if you want to kill process running on port 3000, you will have to execute the following command



sudo kill $(sudo lsof -t -i:3000)





share|improve this answer





















  • But first I want to check the port hang by someone, and if it is true then kill it.
    – Janith
    Dec 7 at 6:12










  • Well , this will actually check all the process using the port initially and will kill all process attached to it
    – Sachu Shaji Abraham
    Dec 7 at 6:15















up vote
4
down vote













You can make use of this command to delete port in Ubuntu.



sudo kill $(sudo lsof -t -i:portno)


The lsof command or list of open files with -i flag will find out all the running process of specific port and gives the corresponding process Id. The kill option will terminate the process with pid given from the lsof command.



So if you want to kill process running on port 3000, you will have to execute the following command



sudo kill $(sudo lsof -t -i:3000)





share|improve this answer





















  • But first I want to check the port hang by someone, and if it is true then kill it.
    – Janith
    Dec 7 at 6:12










  • Well , this will actually check all the process using the port initially and will kill all process attached to it
    – Sachu Shaji Abraham
    Dec 7 at 6:15













up vote
4
down vote










up vote
4
down vote









You can make use of this command to delete port in Ubuntu.



sudo kill $(sudo lsof -t -i:portno)


The lsof command or list of open files with -i flag will find out all the running process of specific port and gives the corresponding process Id. The kill option will terminate the process with pid given from the lsof command.



So if you want to kill process running on port 3000, you will have to execute the following command



sudo kill $(sudo lsof -t -i:3000)





share|improve this answer












You can make use of this command to delete port in Ubuntu.



sudo kill $(sudo lsof -t -i:portno)


The lsof command or list of open files with -i flag will find out all the running process of specific port and gives the corresponding process Id. The kill option will terminate the process with pid given from the lsof command.



So if you want to kill process running on port 3000, you will have to execute the following command



sudo kill $(sudo lsof -t -i:3000)






share|improve this answer












share|improve this answer



share|improve this answer










answered Dec 6 at 8:01









Sachu Shaji Abraham

412




412












  • But first I want to check the port hang by someone, and if it is true then kill it.
    – Janith
    Dec 7 at 6:12










  • Well , this will actually check all the process using the port initially and will kill all process attached to it
    – Sachu Shaji Abraham
    Dec 7 at 6:15


















  • But first I want to check the port hang by someone, and if it is true then kill it.
    – Janith
    Dec 7 at 6:12










  • Well , this will actually check all the process using the port initially and will kill all process attached to it
    – Sachu Shaji Abraham
    Dec 7 at 6:15
















But first I want to check the port hang by someone, and if it is true then kill it.
– Janith
Dec 7 at 6:12




But first I want to check the port hang by someone, and if it is true then kill it.
– Janith
Dec 7 at 6:12












Well , this will actually check all the process using the port initially and will kill all process attached to it
– Sachu Shaji Abraham
Dec 7 at 6:15




Well , this will actually check all the process using the port initially and will kill all process attached to it
– Sachu Shaji Abraham
Dec 7 at 6:15


















draft saved

draft discarded




















































Thanks for contributing an answer to Ask Ubuntu!


  • 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%2faskubuntu.com%2fquestions%2f1098839%2fhow-to-write-a-shell-script-to-check-port-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

Сан-Квентин

8-я гвардейская общевойсковая армия

Алькесар