How to write a shell script to check port and kill it?
I have a command to check the port and kill specific pid running on that port. But I want to modify it to,
- Check port is busy or available
- 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
add a comment |
I have a command to check the port and kill specific pid running on that port. But I want to modify it to,
- Check port is busy or available
- 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
2
savesed
,awk
andsort
with:kill -kill $(sudo netstat -nlpt | grep -oPm1 '2020.*/K.*')
– dessert
Dec 6 '18 at 7:05
add a comment |
I have a command to check the port and kill specific pid running on that port. But I want to modify it to,
- Check port is busy or available
- 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
I have a command to check the port and kill specific pid running on that port. But I want to modify it to,
- Check port is busy or available
- 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
bash scripts
asked Dec 6 '18 at 4:40
JanithJanith
748
748
2
savesed
,awk
andsort
with:kill -kill $(sudo netstat -nlpt | grep -oPm1 '2020.*/K.*')
– dessert
Dec 6 '18 at 7:05
add a comment |
2
savesed
,awk
andsort
with:kill -kill $(sudo netstat -nlpt | grep -oPm1 '2020.*/K.*')
– dessert
Dec 6 '18 at 7:05
2
2
save
sed
, awk
and sort
with: kill -kill $(sudo netstat -nlpt | grep -oPm1 '2020.*/K.*')
– dessert
Dec 6 '18 at 7:05
save
sed
, awk
and sort
with: kill -kill $(sudo netstat -nlpt | grep -oPm1 '2020.*/K.*')
– dessert
Dec 6 '18 at 7:05
add a comment |
1 Answer
1
active
oldest
votes
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)
But first I want to check the port hang by someone, and if it is true then kill it.
– Janith
Dec 7 '18 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 '18 at 6:15
add a comment |
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',
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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)
But first I want to check the port hang by someone, and if it is true then kill it.
– Janith
Dec 7 '18 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 '18 at 6:15
add a comment |
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)
But first I want to check the port hang by someone, and if it is true then kill it.
– Janith
Dec 7 '18 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 '18 at 6:15
add a comment |
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)
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)
answered Dec 6 '18 at 8:01
Sachu Shaji AbrahamSachu 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 '18 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 '18 at 6:15
add a comment |
But first I want to check the port hang by someone, and if it is true then kill it.
– Janith
Dec 7 '18 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 '18 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 '18 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 '18 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 '18 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 '18 at 6:15
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
2
save
sed
,awk
andsort
with:kill -kill $(sudo netstat -nlpt | grep -oPm1 '2020.*/K.*')
– dessert
Dec 6 '18 at 7:05