How to give arguments to kill via pipe [duplicate] - linux

This question already has answers here:
How to pass command output as multiple arguments to another command
(5 answers)
Closed 1 year ago.
I need to search for a certain process and kill that process. I wrote a command like this:
ps -e | grep dmn | awk '{print $1}' | kill
Where the process name is dmn. But it is not working. How can I find processes by name and kill them.

kill $(ps -e | grep dmn | awk '{print $1}')

In case there are multiple processes that you want to remove you can use this:
ps -efw | grep dmn | grep -v grep | awk '{print $2}' | xargs kill
Note: You need to remove grep process itself from the output, that's why grep -v grep is used.

You could use
pkill dmn
if your system has the pkill command.

Just adding on others, but I like using awk's regex features capacity:
kill $(ps | awk '/dmn/{print $1}')

If you have the pidof command on your system ( I know shells such as ZSH come with this by default, unless I'm mistaken), you could do something like.
kill -9 $(pidof dmn)

You might not need pipe for this, if you have pidof command and know the image name, I did it like this:
kill $(pidof synergyc)
$() I understand this as it converts that output to a variable that kill can use, essentially like pipe would do. Shorter and easier to understand than some other options but also maybe less flexible and more direct.

for procid in $(ps -aux | grep "some search" | awk '{print $2}'); do kill -9 $procid; done
hello friends .. we can do it using for loop .
"Some search" is here any process name you want to search, for example "java" so let say count of java process is 200+ so killing one by one will be too typical .
so you can use above command.
Thanks.

You can also use killall:
killall dmn

Use pgrep with -f option.
kill $(pgrep -f dmn)

Related

kill multiple process at once works only manually not from inside script

I'm trying to kill multiple process at the same time.
Im using this simple for loop to kill the process by PID number.
for i in $(ps -ejH | grep omn_bdxtrc|awk '{print $1}'); do kill ${i}; done
The loop works fine if I enter it manually in the terminal.
but if I want to us it from inside an file (*.sh) it returns this output.
/functions.sh: line 231: kill: 25211
25698
27930
8477
5018
16383
13488
2403
10963 18796: arguments must be process or job IDs
have tried multiple ways that works manually, but not from the file.
Any ideas why this is happening?
Thanks in advance.
It looks like the PIDs are being passed as a single argument delimited by line breaks, which kill does not seem to like.
I would simplify the approach by removing the loop completely and just passing the PIDs to kill via xargs:
ps -ejH | grep omn_bdxtrc | awk '{print $1}' | xargs kill
Alternatively (if you don't have or don't want to use xargs for some reason), you can keep your current loop and just sanitize the output from awk by changing all possible line breaks to spaces using tr:
for i in $(ps -ejH | grep omn_bdxtrc | awk '{print $1}' | tr '\n' ' '); do kill ${i}; done
But this is not that elegant.
Probably the most elegant solution would be to use killall, assuming you know the exact name of the process:
killall omn_bdxtrc
Or if you don't know the exact name and need to match a part of it:
killall --regexp '.*omn_bdxtrc.*'

shell script gives output in 2 lines I need only 1

I have the following commands saved in a .sh file
prog=$1
ps axf | grep $prog | grep -v grep | awk '{print "kill -9 " $1}'
I get the following output when I execute it
kill -9 3184
kill -9 20359
But I just need the first line of it as that is the only valid pid. How can I remove the 2nd line from the output.
There are a few issues with what you want to do:
You're building a chain of 4 commands for something relatively simple
You're going to get as a result only the first line of a list of processes containing $prog (excluding the grep $prog which you filtered out); how can you be sure that's the process you want?
The correct command to use is
pkill $prog`
as suggested in the comments, which probably will do what you want.
Just for information, and to answer your question, you can pipe an output to head -n 1 to return only the first line:
<list of commands> | head -n 1
However, in your case this would add a fifth command to the chain, so I recommend you don't do it this way.

How to capture the output of a bash command into a variable when using pipes and apostrophe? [duplicate]

This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 6 years ago.
I am not sure how to save the output of a command via bash into a variable:
PID = 'ps -ef | grep -v color=auto | grep raspivid | awk '{print $2}''
Do I have to use a special character for the apostrophe or for the pipes?
Thanks!
To capture the output of a command in shell, use command substitution: $(...). Thus:
pid=$(ps -ef | grep -v color=auto | grep raspivid | awk '{print $2}')
Notes
When making an assignment in shell, there must be no spaces around the equal sign.
When defining shell variables for local use, it is best practice to use lower case or mixed case. Variables that are important to the system are defined in upper case and you don't want to accidentally overwrite one of them.
Simplification
If the goal is to get the PID of the raspivid process, then the grep and awk can be combined into a single process:
pid=$(ps -ef | awk '/[r]aspivid/{print $2}')
Note the simple trick that excludes the current process from the output: instead of searching for raspivid we search for [r]aspivid. The string [r]aspivid does not match the regular expression [r]aspivid. Hence the current process is removed from the output.
The Flexibility of awk
For the purpose of showing how awk can replace multiple calls to grep, consider this scenario: suppose that we want to find lines that contain raspivid but that do not contain color=auto. With awk, both conditions can be combined logically:
pid=$(ps -ef | awk '/raspivid/ && !/color=auto/{print $2}')
Here, /raspivid/ requires a match with raspivid. The && symbol means logical "and". The ! before the regex /color=auto/ means logical "not". Thus, /raspivid/ && !/color=auto/ matches only on lines that contain raspivid but not color=auto.
A more straightforward approach:
pid=$(pgrep raspivid)
... or a little different
echo pgrep [t]eleport

Get pid of last started instance of a certain process

I have several instances of a certain process running and I want to determine the process id of the one that has been started last.
So far I came to this code:
ps -aef | grep myProcess | grep -v grep | awk -F" " '{print $2}' |
while read line; do
echo $line
done
This gets me all process ids of myProcess. Somehow I need to compare now the running times of this pids and find out the one with the smallest running time. But I don't know how to do that...
An easier way would be to use pgrep with its -n, --newest switch.
Select only the newest (most recently started) of the matching
processes.
Alternatively, if you don't want to use pgrep, you can use ps and sort by start time:
ps -ef kbsdstart
Use pgrep. It has a -n (newest) option for that. So just try
pgrep -n myProcess

How to list and kill threads/processes in Perl without using external command(s)?

I need to kill some processes and threads (if exist) in Perl application, but I don't want to use external command(s) such as ps, grep, awk, cut, uniq or kill.
My current code is:
my $appName = $0;
$appName =~ s/^.*\/([^\/]*)$/$1/;
$_ = qx(kill -9 `ps -eLao pid,command | grep '$appName\[ 0-9\]*\$' |
grep -v grep | awk '\$1 != $$' | cut -d' ' -f1 | uniq` 2>&1);
I am using VPS, so my memory is limited. The code above sometimes returns undef, as system cannot allocate memory for call of external command(s). I am looking for alternative solution without using external command(s).
Use internal kill commands like kill and the /proc fs or some modules like Win32::Process::List or anything in the Proc:: namespace.

Resources