Shell Script CentOS - Killing process by reading it's config file and getting the port - linux

I am looping through folders with Java applications and getting the config file for each.
app1/config.yml
app2/config.yml
etc.
I then pull the port from this config file by using:
port= cat app1/config.yml | grep 90 | cut -d: -f2
I want to use the port to kill the application, I did find this code that does half of what I want it to do:
kill $(sudo lsof -t -i:4990)
I want to use the variable stored in port to execute the kill command, but I can't get it to work, what is the correct way to use the command, I have tried multiple ways:
kill $(sudo lsof -t -i:$port)
kill $(sudo lsof -t -i:port)
kill $(sudo lsof -t -i:"$port")
kill $(sudo lsof -t -i:'$port')
But none of these work, I keep getting errors.
Any help would be appreciated

You're not setting port correctly, you left out the $(...) around the command.
port=$(cat app1/config.yml | grep 90 | cut -d: -f2)
kill $(sudo lsof -t -i:$port)

Related

To which port is a process attached in Linux

I want to know which port is my Jonas, on which a Java project has been deployed, is attached to in a Linux server. I have the pid of the Jonas and tried netstat -lnp but I found no port attached to that PID.
Any idea of how to do this.
Open a terminal application i.e. shell prompt.
Run any one of the following command:
sudo lsof -i -P -n | grep LISTEN
sudo netstat -tulpn | grep LISTEN
sudo nmap -sTU -O IP-address-Here
lsof command
The syntax is:
$ sudo lsof -i -P -n
$ sudo lsof -i -P -n | grep LISTEN
$ doas lsof -i -P -n | grep LISTEN ### [OpenBSD] ###
There are many ways to do, I prefer this
sudo netstat -pan |grep pid
Also, you can use
sudo lsof -Pan -p pid -i
pid should be actual "pid" number that you have

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.

how can I kill a process in a shell script

My bash script has:
ps aux | grep foo.jar | grep -v grep | awk '{print $2}' | xargs kill
However, I get the following when running:
usage: kill [ -s signal | -p ] [ -a ] pid ...
kill -l [ signal ]
Any ideas, how to fix this line?
In general, your command is correct. If a foo.jar process is running, its PID will be passed to kill and (should) terminate.
Since you're getting kill's usage as output, it means you're actually calling kill with no arguments (try just running kill on its own, you'll see the same message). That means that there's no output in the pipeline actually reaching xargs, which in turn means foo.jar is not running.
Try running ps aux | grep foo.jar | grep -v grep and see if you're actually seeing results.
As much as you may enjoy a half dozen pipes in your commands, you may want to look at the pkill command!
DESCRIPTION
The pkill command searches the process table on the running system and signals all processes that match the criteria
given on the command line.
i.e.
pkill foo.jar
Untested and a guess at best (be careful)
kill -9 $(ps -aux | grep foo.jar | grep -v grep | awk '{print $2}')
I re-iterate UNTESTED as I'm not at work and have no access to putty or Unix.
My theory is to send the kill -9 command and get the process id from a sub shell command call.

How to combine "lsof -i :port" and "kill pid" in bash

How do i combine these two commands in the bash:
lsof -i :port
kill pid
The first one returns the PID i want to kill to release the port. The second one kills the returned PID.
I am doing this because I donĀ“t know of any way to kill a jetty webserver within the Netbeans IDE on OSX. Is there a way?
You can use $():
kill $(lsof -t -i:port)
You can use
kill -9 `lsof -t -i:port`

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

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

Resources