Closing all processes holding a given port from shell - bash

I am trying to automate one of the jobs I am facing again and again.
There are some ports which are sometimes not closed correctly by some previous jobs..
Lets say port 5000,5001
WHat I want to do is see if these ports are open
and kill these ports if they are open
So right now, I am doing
lsof -i :5000
and
kill -9 pid1
kill -9 pid2
and so on..
Is there a way to pass 5000 and 5001 as arguments and automate this process

If you want to automate it as you use it do something like this.
Create a bash script, for example call it 'killMyPorts' and add the following content:
#!/bin/bash
kill -9 `lsof -i :"${1}" | awk '{if (NR!=1) {print $2}}'`
Once you made the script executable (chmod u+x), you can execute it as follows:
./killMyPorts 5000

fuser can do this job for you.
kill_port_users() {
for port; do
fuser -n tcp -k "$port"
done
}
kill_port_users 5000 5001

Have you tried
kill -9 `lsof -i :5000`
or
kill -9 $(lsof -i :5000)

Related

Passing a port number to lsof in a shell function

I wrote an alias in my bash profile to help me kill rogue rails server processes that don't cleanly close. This alias works well.
alias kill3000="lsof -i tcp: 3000 -t | xargs kill -9 | echo 'killed processes on port 3000'"
I wanted to make it more general purpose, for frameworks that work on other ports. I tried to make a similar function, so I could pass the port number as a variable, but I get an error. The function I wrote looks like this...
function killproc (){
"lsof -i tcp:$1 -t | xargs kill -9 | echo 'killed processes on port $1'"
}
However, when I run "killproc 3000", I get the following error:
lsof: unacceptable port specification in: -i tcp:
I'm struggling to understand the problem, I appreciate any help.
Maybe the double-quotes are involved.
Give a try to this:
function killproc (){
lsof -i tcp:"$1" -t | xargs kill -9
lsof -i tcp:"$1" -t 2>/dev/null >/dev/null || printf "killed processes on port %s\n" "$1"
}
The message is printed only if there is no more process found listening on the port pspecified.
If you get some troubles, follow #shellter instruction.
Run a test with set -vx ; killproc 300 ; set +vxand edit the question to add the output generated.

Bash store command PID in variable and kill process

I would like to use a shell script in order to establish ethernet connection.
I am using a function implemented as :
function connec()
{
ip link set eth0 up
sleep 5
udhcpc -i eth0
pid=$$
echo $pid
ps
kill -9 $pid
}
However, the script returns :
743
743 root 2704 S {script.sh} /bin/bash ./script.sh connect
767 root 2200 S udhcpc -i eth0
Killed
I don't succeed in store 767 rather than 743. I also tried by using $! but in that specific case "echo $pid" returns 0.
$$ is the current process which means the script is killing itself. You can get the process ID of the last process you started in the background, with $! but it appears you're not actually starting one of those.
With your code segment:
udhcpc -i eth0
pid=$$
the pid= line will only be executed when udhcpc exits (or daemonises itself, in which case neither $$ nor $! will work anyway), so there's zero point in trying to kill of the process.
To run it in the background and store its process ID, so you can continue to run in the parent, you could use something like:
udhcpc -f -i eth0 &
pid=$!
and you're using -f to run in foreground in that case, since you're taking over the normal job control.
Or, alternatively, since udhcpc can create its own PID file, you can use something like:
udhcpc -i eth0 -p /tmp/udhcpc.eth0.pid
pid=$(cat /tmp/udhcpc.eth0.pid)

how to assign terminal variable from previous command's output

I'm very very new to the terminal script world. Here's what I want to do:
1) Find out the process that's using a given port (8000 in this case)
2) Kill that process
Pretty simple. I can do it manually using:
lsof -i tcp:8000 -- get the PID of what's using the port
kill -9 $PID -- terminate the app using the port
For reference, here's exactly what gets returned when using lsof -i tcp:8000
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
php 94735 MyUser 5u IPv6 0x9fbd127eb623aacf 0t0 TCP localhost:irdmi (LISTEN)
Here's my problem: how do I capture the PID value from lsof -i tcp:8000 so that I can use that variable for the next command? I know how to create variable that I assign... just not ones that are dynamically made.
The thing you’re looking for is called command substitution. It lets you treat the output of a command as input to the shell.
For example:
$ mydate="$(date)"
$ echo "${mydate}"
Mon 24 Feb 2014 22:45:24 MST
It’s also possible to use `backticks` instead of the dollar sign and parentheses, but most shell style guides recommend avoiding that.
In your case, you probably want to do something like this:
$ PID="$(lsof -i tcp:8000 | grep TCP | awk '{print $2}')"
$ kill $PID
Something along these lines should work:
lsof -i tcp:8000 | grep TCP | read cmd pid restofline
kill -9 $pid
before using the kill command, just echo it to make sure.

How to find processes based on port and kill them all? [duplicate]

This question already has answers here:
How to kill a process running on particular port in Linux?
(34 answers)
Closed 5 years ago.
Find processes based on port number and kill them all.
ps -efl | grep PORT_NUMBER | kill -9 process_found_previously
how to complete the last column?
The problem with ps -efl | grep PORT_NUMBER is that PORT_NUMBER may match other columns in the output of ps as well (date, time, pid, ...). A potential killing spree if run by root!
I would do this instead :
PORT_NUMBER=1234
lsof -i tcp:${PORT_NUMBER} | awk 'NR!=1 {print $2}' | xargs kill
Breakdown of command
(lsof -i tcp:${PORT_NUMBER}) -- list all processes that is listening on that tcp port
(awk 'NR!=1 {print $2}') -- ignore first line, print second column of each line
(xargs kill) -- pass on the results as an argument to kill. There may be several.
1.) lsof -w -n -i tcp:8080
2.) kill -9 processId
kill $( lsof -i:6000 -t )
Or if you need permissions:
sudo kill $( sudo lsof -i:6000 -t )
Propose to use fuser command:
fuser -k -TERM -n tcp ${PORT_NUMBER}
sudo fuser -k 8080/tcp
An easy one to remember.
This syntax is probably much more recent than the date of the question!
... | awk '{ print $4 }' | xargs kill -9
please test with "echo" instead of "kill" before running
To kill all processes listening on a particular port, e.g. port 8864
kill -9 $ \`lsof -i:8864 -t\`
Replace 8864 by the port you want.

How do I kill a backgrounded/detached ssh session?

I am using the program synergy together with an ssh tunnel
It works, i just have to open an console an type these two commands:
ssh -f -N -L localhost:12345:otherHost:12345 otherUser#OtherHost
synergyc localhost
because im lazy i made an Bash-Script which is run with one mouseclick on an icon:
#!/bin/bash
ssh -f -N -L localhost:12345:otherHost:12345 otherUser#OtherHost
synergyc localhost
the Bash-Script above works as well, but now i also want to kill synergy and the ssh tunnel via one mouseclick, so i have to save the PIDs of synergy and ssh into file to kill them later:
#!/bin/bash
mkdir -p /tmp/synergyPIDs || exit 1
rm -f /tmp/synergyPIDs/ssh || exit 1
rm -f /tmp/synergyPIDs/synergy || exit 1
[ ! -e /tmp/synergyPIDs/ssh ] || exit 1
[ ! -e /tmp/synergyPIDs/synergy ] || exit 1
ssh -f -N -L localhost:12345:otherHost:12345 otherUser#OtherHost
echo $! > /tmp/synergyPIDs/ssh
synergyc localhost
echo $! > /tmp/synergyPIDs/synergy
But the files of this script are empty.
How do I get the PIDs of ssh and synergy?
(I try to avoid ps aux | grep ... | awk ... | sed ... combinations, there has to be an easier way.)
With all due respect to the users of pgrep, pkill, ps | awk, etc, there is a much better way.
Consider that if you rely on ps -aux | grep ... to find a process you run the risk of a collision. You may have a use case where that is unlikely, but as a general rule, it's not the way to go.
SSH provides a mechanism for managing and controlling background processes. But like so many SSH things, it's an "advanced" feature, and many people (it seems, from the other answers here) are unaware of its existence.
In my own use case, I have a workstation at home on which I want to leave a tunnel that connects to an HTTP proxy on the internal network at my office, and another one that gives me quick access to management interfaces on co-located servers. This is how you might create the basic tunnels, initiated from home:
$ ssh -fNT -L8888:proxyhost:8888 -R22222:localhost:22 officefirewall
$ ssh -fNT -L4431:www1:443 -L4432:www2:443 colocatedserver
These cause ssh to background itself, leaving the tunnels open. But if the tunnel goes away, I'm stuck, and if I want to find it, I have to parse my process list and home I've got the "right" ssh (in case I've accidentally launched multiple ones that look similar).
Instead, if I want to manage multiple connections, I use SSH's ControlMaster config option, along with the -O command-line option for control. For example, with the following in my ~/.ssh/config file,
host officefirewall colocatedserver
ControlMaster auto
ControlPath ~/.ssh/cm_sockets/%r#%h:%p
the ssh commands above, when run, will leave spoor in ~/.ssh/cm_sockets/ which can then provide access for control, for example:
$ ssh -O check officefirewall
Master running (pid=23980)
$ ssh -O exit officefirewall
Exit request sent.
$ ssh -O check officefirewall
Control socket connect(/home/ghoti/.ssh/cm_socket/ghoti#192.0.2.5:22): No such file or directory
And at this point, the tunnel (and controlling SSH session) is gone, without the need to use a hammer (kill, killall, pkill, etc).
Bringing this back to your use-case...
You're establishing the tunnel through which you want syngergyc to talk to syngergys on TCP port 12345. For that, I'd do something like the following.
Add an entry to your ~/.ssh/config file:
Host otherHosttunnel
HostName otherHost
User otherUser
LocalForward 12345 otherHost:12345
RequestTTY no
ExitOnForwardFailure yes
ControlMaster auto
ControlPath ~/.ssh/cm_sockets/%r#%h:%p
Note that the command line -L option is handled with the LocalForward keyword, and the Control{Master,Path} lines are included to make sure you have control after the tunnel is established.
Then, you might modify your bash script to something like this:
#!/bin/bash
if ! ssh -f -N otherHosttunnel; then
echo "ERROR: couldn't start tunnel." >&2
exit 1
else
synergyc localhost
ssh -O exit otherHosttunnel
fi
The -f option backgrounds the tunnel, leaving a socket on your ControlPath to close the tunnel later. If the ssh fails (which it might due to a network error or ExitOnForwardFailure), there's no need to exit the tunnel, but if it did not fail (else), synergyc is launched and then the tunnel is closed after it exits.
You might also want to look in to whether the SSH option LocalCommand could be used to launch synergyc from right within your ssh config file.
Quick summary: Will not work.
My first idea is that you need to start the processes in the background to get their PIDs with $!.
A pattern like
some_program &
some_pid=$!
wait $some_pid
might do what you need... except that then ssh won't be in the foreground to ask for passphrases any more.
Well then, you might need something different after all. ssh -f probably spawns a new process your shell can never know from invoking it anyway. Ideally, ssh itself would offer a way to write its PID into some file.
just came across this thread and wanted to mention the "pidof" linux utility:
$ pidof init
1
You can use lsof to show the pid of the process listening to port 12345 on localhost:
lsof -t -i #localhost:12345 -sTCP:listen
Examples:
PID=$(lsof -t -i #localhost:12345 -sTCP:listen)
lsof -t -i #localhost:12345 -sTCP:listen >/dev/null && echo "Port in use"
well i dont want to add an & at the end of the commands as the connection will die if the console wintow is closed ... so i ended up with an ps-grep-awk-sed-combo
ssh -f -N -L localhost:12345:otherHost:12345 otherUser#otherHost
echo `ps aux | grep -F 'ssh -f -N -L localhost' | grep -v -F 'grep' | awk '{ print $2 }'` > /tmp/synergyPIDs/ssh
synergyc localhost
echo `ps aux | grep -F 'synergyc localhost' | grep -v -F 'grep' | awk '{ print $2 }'` > /tmp/synergyPIDs/synergy
(you could integrate grep into awk, but im too lazy now)
You can drop the -f, which makes it run it in background, then run it with eval and force it to the background yourself.
You can then grab the pid. Make sure to put the & within the eval statement.
eval "ssh -N -L localhost:12345:otherHost:12345 otherUser#OtherHost & "
tunnelpid=$!
Another option is to use pgrep to find the PID of the newest ssh process
ssh -fNTL 8073:localhost:873 otherUser#OtherHost
tunnelPID=$(pgrep -n -x ssh)
synergyc localhost
kill -HUP $tunnelPID
This is more of a special case for synergyc (and most other programs that try to daemonize themselves). Using $! would work, except that synergyc does a clone() syscall during execution that will give it a new PID other than the one that bash thought it has. If you want to get around this so that you can use $!, then you can tell synergyc to stay in the forground and then background it.
synergyc -f -n mydesktop remoteip &
synergypid=$!
synergyc also does a few other things like autorestart that you may want to turn off if you are trying to manage it.
Based on the very good answer of #ghoti, here is a simpler script (for testing) utilising the SSH control sockets without the need of extra configuration:
#!/bin/bash
if ssh -fN -MS /tmp/mysocket -L localhost:12345:otherHost:12345 otherUser#otherHost; then
synergyc localhost
ssh -S /tmp/mysocket -O exit otherHost
fi
synergyc will be only started if tunnel has been established successfully, which itself will be closed as soon as synergyc returns.
Albeit the solution lacks proper error reporting.
You could look out for the ssh proceess that is bound to your local port, using this line:
netstat -tpln | grep 127\.0\.0\.1:12345 | awk '{print $7}' | sed 's#/.*##'
It returns the PID of the process using port 12345/TCP on localhost. So you don't have to filter all ssh results from ps.
If you just need to check, if that port is bound, use:
netstat -tln | grep 127\.0\.0\.1:12345 >/dev/null 2>&1
Returns 1 if none bound or 0 if someone is listening to this port.
There are many interesting answers here, but nobody mentioned that the manpage of SSH does describe this exact case! (see TCP FORWARDING section). And the solution they offer is much simpler:
ssh -fL 12345:localhost:12345 user#remoteserver sleep 10
synergyc localhost
Now in details:
First we start SSH with a tunnel; thanks to -f it will initiate the connection and only then fork to background (unlike solutions with ssh ... &; pid=$! where ssh is sent to background and next command is executed before the tunnel is created). On the remote machine it will run sleep 10 which will wait 10 seconds and then end.
Within 10 seconds, we should start our desired command, in this case synergyc localhost. It will connect to the tunnel and SSH will then know that the tunnel is in use.
After 10 seconds pass, sleep 10 command will finish. But the tunnel is still in use by synergyc, so SSH will not close the underlying connection until the tunnel is released (i.e. until synergyc closes socket).
When synergyc is closed, it will release the tunnel, and SSH in turn will terminate itself, closing a connection.
The only downside of this approach is that if the program we use will close and re-open connection for some reason then SSH will close the tunnel right after connection is closed, and the program won't be able to reconnect. If this is an issue then you should use an approach described in #doak's answer which uses control socket to properly terminate SSH connection and uses -f to make sure tunnel is created when SSH forks to the background.

Resources