shell script to kill the process listening on port 3000? [duplicate] - linux

This question already has answers here:
How to kill a process running on particular port in Linux?
(34 answers)
Closed 4 years ago.
I want to define a bash alias named kill3000 to automate the following task:
$ lsof -i:3000
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
ruby 13402 zero 4u IPv4 2847851 0t0 TCP *:3000 (LISTEN)
$ kill -9 13402

alias kill3000="fuser -k -n tcp 3000"

Try this:
kill -9 $(lsof -i:3000 -t)
The -t flag is what you want: it displays PID, and nothing else.
Update
In case the process is not found and you don't want to see error message:
kill -9 $(lsof -i:3000 -t) 2> /dev/null
Assuming you are running bash.
Update
Basile's suggestion is excellent: we should first try to terminate the process normally will kill -TERM, if failed, then kill -KILL (AKA kill -9):
pid=$(lsof -i:3000 -t); kill -TERM $pid || kill -KILL $pid
You might want to make this a bash function.

Another option using using the original lsof command:
lsof -n -i:3000 | grep LISTEN | awk '{ print $2 }' | uniq | xargs kill -9
If you want to use this in a shell script, you could add the -r flag to xargs to handle the case where no process is listening:
... | xargs -r kill -9

fuser -k 3000/tcp should also work

How about
alias kill3000="lsof -i:3000 | grep LISTEN | awk '{print $2}' | xargs kill -9"

fuser -n tcp 3000
Will yield the output of
3000/tcp: <$pid>
So you could do:
fuser -n tcp 3000 | awk '{ print $2 }' | xargs -r kill

Related

how to find network connections from a PID

I have a PID and i am trying to find the network connections that are attached to that PID.
i have placed the pid into a variable $PID.
So far I have tried using netstat to do it. I have tried the following
netstat -p | grep $PID
netstat | grep $PID
but these do not seem to grep anything or find what im looking for. What would be the best way to find these?
strace will do the job:
strace -p $PID -f -e trace=network -s 10000
-s 1000 increases the maximum string size to print, which is 32 by default.
Check out this question on unix.stackexchange.com for alternatives.
You can also use netstat. Just replace -p with -nap.
netstat -nap | grep {CMD-of-PID}
its from cmd of pid actually.

grepping the PID of a process - Unix

I'm trying to execute the following command:
ps aux | grep com.scheduler.app.workermain | kill -15 [pid]
How can I obtain the [pid] (or list of PID) using ps aux | grep "expression" and pipe that to kill? There may be zero or many processes running the machine. This is part of an automated job, to ensure all the processes spun will be terminated.
A sample line from the command line, when ps aux | grep com.scheduler.app.workermain is executed is:
jenkins 12373 1.1 4.2 2905440 173628 ? Sl 19:28 0:05 java -Xmx600m -Dlog4j.configurationFile=log4j2-trace.xml -Dpid=foobar -Dipaddr=127.0.0.1 -cp build/classes:build/dependency/* com.scheduler.app.workermain testing.properties
pkill is used for exactly this purpose. How about:
pkill -15 -f com.scheduler.app.workermain
Also if you just want to grep for a PID you can use pgrep:
pgrep -f com.scheduler.app.workermain
pkill man page
kill -15 $(ps aux | grep -i com.scheduler.app.workermain | awk -F' ' '{ print $2 }')
One of possible solutions is to use the pidof command:
kill $( pidof com.scheduler.app.workermain )
PS. You don't need to pass -15 (or -TERM) to the kill command, as SIGTERM is the default signal sent.

Get a command output in a shell script

I am trying to make a shell script that automatically kills the process that is running in a given port, on a Ubuntu 14.10 dedicated server.
As far as I know, using fuser -n tcp {port-number} gives the PID of the process running in that port, example: http://prntscr.com/81whib
Also, I know that kill -9 {PID} kills the process with such PID.
How could I make a shell script that gets the output of the command fuser -n tcp {port-number} and uses it in the command kill -9 {PID} ?
Thanks for the help.
You can manipulate the output of fuser using awk (sample using port 139):
pid=$(fuser -n tcp 139 | awk '{print $1}')
echo $pid
# kill -9 $pid , etc
The following script will allow you to pass in a port (e.g. ./killProcess 25565) and will kill the process so long as a PID exists.
#!/bin/bash
PID=$(fuser -n tcp $1 | awk '{print $2}')
if [ ! -z "$PID" ]; then
kill -9 $PID
else
echo "No process is running on port $1"
fi

Killing a PID which is using a port

I am using below code to kill a process which is using a port number
port = sudo lsof -n -i4TCP:3030 | grep LISTEN | awk '{print $2;}'
if [ ! -z "$port" -a "$port" != " " ]; then
sudo kill "$port"
fi
But it is saying port: command not found. What is causing the issue and how can I fix it.
As it stands,
port = sudo lsof -n -i4TCP:3030 | grep LISTEN | awk '{print $2;}'
attempts to run a command port with parameters = sudo lsof -n -i4TCP:3030 and pipe its output through grep LISTEN and then awk '{print $2;}'.
Use
port=$(sudo lsof -n -i4TCP:3030 | grep LISTEN | awk '{print $2;}')
There's no reason to roll this yourself: fuser on Linux will do it for you in a single command, and much more efficiently:
sudo fuser -n tcp -k 3030
With just one line!
sudo kill `sudo lsof -t -i:3030`

how to get value of a column using linux [duplicate]

Say I want to kill every process containing the word amarok. I can print out the commands I want to execute. But how do I actually make the shell execute them. ie.
ps aux | grep -ie amarok | awk '{print "kill -9 " $2}'
Output:
kill -9 3052
kill -9 3071
kill -9 3076
kill -9 3077
kill -9 3079
kill -9 3080
kill -9 3082
kill -9 3083
kill -9 3084
kill -9 3085
kill -9 3086
kill -9 3087
kill -9 3088
kill -9 3089
kill -9 4031
From man 1 pkill
-f The pattern is normally only matched against the process name.
When -f is set, the full command line is used.
Which means, for example, if we see these lines in ps aux:
apache 24268 0.0 2.6 388152 27116 ? S Jun13 0:10 /usr/sbin/httpd
apache 24272 0.0 2.6 387944 27104 ? S Jun13 0:09 /usr/sbin/httpd
apache 24319 0.0 2.6 387884 27316 ? S Jun15 0:04 /usr/sbin/httpd
We can kill them all using the pkill -f option:
pkill -f httpd
ps aux | grep -ie amarok | awk '{print $2}' | xargs kill -9
xargs(1): xargs -- construct argument list(s) and execute utility. Helpful when you want to pipe in arguments to something like kill or ls or so on.
use pgrep
kill -9 $(pgrep amarok)
The safe way to do this is:
pkill -f amarok
I think this command killall is exactly what you need.
The command is described as "kill processes by name".It's easy to use.For example
killall chrome
This command will kill all process of Chrome.Here is a link about killall command
http://linux.about.com/library/cmd/blcmdl1_killall.htm
Hope this command could help you.
pkill -x matches the process name exactly.
pkill -x amarok
pkill -f is similar but allows a regular expression pattern.
Note that pkill with no other parameters (e.g. -x, -f) will allow partial matches on process names. So "pkill amarok" would kill amarok, amarokBanana, bananaamarok, etc.
I wish -x was the default behavior!
try kill -s 9 `ps -ef |grep "Nov 11" |grep -v grep | awk '{print $2}'` To kill processes of November 11
or
kill -s 9 `ps -ef |grep amarok|grep -v grep | awk '{print $2}'`
To kill processes that contain the word amarok
If you want to execute the output of a command, you can put it inside $(...), however for your specific task take a look at the killall and pkill commands.
You can also evaluate your output as a sub-process, by surrounding everything with back ticks or with putting it inside $():
`ps aux | grep -ie amarok | awk '{print "kill -9 " $2}'`
$(ps aux | grep -ie amarok | awk '{print "kill -9 " $2}')
Maybe adding the commands to executable file, setting +x permission and then executing?
ps aux | grep -ie amarok | awk '{print "kill -9 " $2}' > pk;chmod +x pk;./pk;rm pk
If you're using cygwin or some minimal shell that lacks killall you can just use this script:
killall.sh - Kill by process name.
#/bin/bash
ps -W | grep "$1" | awk '{print $1}' | xargs kill --
Usage:
$ killall <process name>

Resources