How to extract a column value from a command output in shell script - linux

I am trying to write a shell script where I want to kill a list of processes given by fuser command.
The output of fuser is given. I want to kill the pids listed
kill -9 157909 1504107 1504111 1504112 2690311 3206490
How do I do that?

-k, --kill
Kill processes accessing the file. Unless changed with -SIGNAL,
SIGKILL is sent. An fuser process never kills itself, but may
kill other fuser processes. The effective user ID of the
process executing fuser is set to its real user ID before
attempting to kill.
fuser -k will do it.

ps -u beadm | awk '{print $2}' | while read line; do kill -9 $line; done

Related

how to use kill command only with files name without PID

I wanna kill programs with one command
ps -elf | grep "common_program_name" | grep -v grep | awk '{print $2}' | xargs kill -9
but I got an error
kill: failed to parse argument: 'S
what am I doing wrong ?
Use killall -9 common_program_name
the problem above is that you might have more than one instance of the program running.
Also, you are fetching the second column from the output of ps That column does not contain a PID, so your xargs command effectively runs kill -9 S Perhaps you can adjust your flags to ps or your field captured by awk to make the above work, but there are already purpose built programs which do this job.
Use pkill, in your case pkill -9 common_program_name (for more see man pkill) which gives you the functionality you're looking for. And unlike killall won't kill literally all process on System V UNIX machines. ;)

BASH. Make the result of one command to be an argument of another [duplicate]

This question already has answers here:
Using the result of a command as an argument in bash?
(7 answers)
Closed 5 years ago.
How can I make the result of one command to be an argument of another?
I'm trying to kill child process by pid of parent process and use for it pgrep
Example: pgrep -P <PID>
But after I need to kill the PID which I get from pgrep
pgrep -P <PID> | kill - it doesn't work(
Thank you!
With single pkill command:
pkill -P <PID> --signal SIGTERM
--signal signal
Defines the signal to send to each matched process. Either the numeric or the symbolic signal name can be
used. (pkill only.)
Try this:
VALUE="$(pgrep -P <PID>)"
kill ${VALUE}
You use backticks for that. Like this:
kill `pgrep -P <PID>`
this should work for you
kill -9 `command`
and as far as getting the pid is concerned see examples below
kill -9 `pgrep executable`
kill -9 `pgrep ps`
kill -9 `pgrep bash`
or your command
kill -9 `pgrep -P <PID>`
You probably want "kill -9" as well:
pgrep -P <PID> | xargs -n1 kill -9
To test what it is going to do in advance try:
pgrep -P <PID> | xargs -n1 echo kill -9

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 get the process ID on Linux [duplicate]

This question already has answers here:
How to get pid given the process name
(4 answers)
Closed 5 years ago.
I want to write a shell script (.sh file) to get a given process id. What I'm trying to do here is once I get the process ID, I want to kill that process. I'm running on Ubuntu (Linux).
I was able to do it with a command like
ps -aux|grep ruby
kill -9 <pid>
but I'm not sure how to do it through a shell script.
Using grep on the results of ps is a bad idea in a script, since some proportion of the time it will also match the grep process you've just invoked. The command pgrep avoids this problem, so if you need to know the process ID, that's a better option. (Note that, of course, there may be many processes matched.)
However, in your example, you could just use the similar command pkill to kill all matching processes:
pkill ruby
Incidentally, you should be aware that using -9 is overkill (ho ho) in almost every case - there's some useful advice about that in the text of the "Useless Use of kill -9 form letter ":
No no no. Don't use kill -9.
It doesn't give the process a chance to cleanly:
shut down socket connections
clean up temp files
inform its children that it is going away
reset its terminal characteristics
and so on and so on and so on.
Generally, send 15, and wait a second or two, and if that doesn't
work, send 2, and if that doesn't work, send 1. If that doesn't,
REMOVE THE BINARY because the program is badly behaved!
Don't use kill -9. Don't bring out the combine harvester just to tidy
up the flower pot.
If you are going to use ps and grep then you should do it this way:
ps aux|grep r[u]by
Those square brackets will cause grep to skip the line for the grep command itself. So to use this in a script do:
output=`ps aux|grep r\[u\]by`
set -- $output
pid=$2
kill $pid
sleep 2
kill -9 $pid >/dev/null 2>&1
The backticks allow you to capture the output of a comand in a shell variable. The set -- parses the ps output into words, and $2 is the second word on the line which happens to be the pid. Then you send a TERM signal, wait a couple of seconds for ruby to to shut itself down, then kill it mercilessly if it still exists, but throw away any output because most of the time kill -9 will complain that the process is already dead.
I know that I have used this without the backslashes before the square brackets but just now I checked it on Ubuntu 12 and it seems to require them. This probably has something to do with bash's many options and the default config on different Linux distros. Hopefully the [ and ] will work anywhere but I no longer have access to the servers where I know that it worked without backslash so I cannot be sure.
One comment suggests grep-v and that is what I used to do, but then when I learned of the [] variant, I decided it was better to spawn one fewer process in the pipeline.
As a start there is no need to do a ps -aux | grep... The command pidof is far better to use. And almost never ever do kill -9 see here
to get the output from a command in bash, use something like
pid=$(pidof ruby)
or use pkill directly.
option -v is very important. It can exclude a grep expression itself
e.g.
ps -w | grep sshd | grep -v grep | awk '{print $1}' to get sshd id
This works in Cygwin but it should be effective in Linux as well.
ps -W | awk '/ruby/,NF=1' | xargs kill -f
or
ps -W | awk '$0~z,NF=1' z=ruby | xargs kill -f
Bash Pitfalls
You can use the command killall:
$ killall ruby
Its pretty simple.
Simply Run Any Program like this :- x= gedit & echo $! this will give you PID of this process.
then do this kill -9 $x
To kill the process in shell
getprocess=`ps -ef|grep servername`
#echo $getprocess
set $getprocess
pid=$2
#echo $pid
kill -9 $pid
If you already know the process then this will be useful:
PID=`ps -eaf | grep <process> | grep -v grep | awk '{print $2}'`
if [[ "" != "$PID" ]]; then
echo "killing $PID"
kill -9 $PID
fi

linux shell: How to read command argument from a file?

I have process id in a file "pid"
I'd like to kill it.
Something like:
kill -9 <read pid from file>
I tried:
kill -9 `more pid`
but it does not work. I also tried xargs but can't get my head around it.
Does
kill -9 $(cat pid)
work for you?
Let me summarize all answers
kill -9 $(cat pid)
kill -9 `cat pid`
cat pid | xargs kill -9
my preference is
kill -9 `cat pid`
that will work for any command in the backticks.
kill -9 $(cat pid) or cat pid | xargs kill -9 will both work
You should be starting off gradually and then move up to the heavy stuff to kill the process if it doesn't want to play nicely.
A SIGKILL (-9) signal can't be caught and that will mean that any resources being held by the process won't be cleaned up.
Try using a kill SIGTERM (-15) first and then check for the presence of the process still by doing a kill -0 $(cat pid). If it is still hanging around, then by all means clobber it with -9.
SIGTERM can be caught by a process and any process that has been properly written should have a signal handler to catch the SIGTERM and then clean up its resources before exiting.

Resources