Getting pids from ps -ef |grep keyword
You can use pgrep
as long as you include the -f
options. That makes pgrep
match keywords in the whole command (including arguments) instead of just the process name.
pgrep -f keyword
From the man page:
-f
The pattern is normally only matched against the process name. When-f
is set, the full command line is used.
If you really want to avoid pgrep, try:
ps -ef | awk '/[k]eyword/{print $2}'
Note the []
around the first letter of the keyword. That's a useful trick to avoid matching the awk
command itself.
how to get process id of specific process?
ps -ef | grep -w abc | grep -v grep
^
`--- match whole words only
How to get only process ID in specify process name in Linux?
You can use:
ps -ef | grep '[j]ava'
Or if pgrep
is available then better to use:
pgrep -f java
Find and kill a process in one line using bash and regex
In bash
, you should be able to do:
kill $(ps aux | grep '[p]ython csp_build.py' | awk '{print $2}')
Details on its workings are as follows:
- The
ps
gives you the list of all the processes. - The
grep
filters that based on your search string,[p]
is a trick to stop you picking up the actualgrep
process itself. - The
awk
just gives you the second field of each line, which is the PID. - The
$(x)
construct means to executex
then take its output and put it on the command line. The output of thatps
pipeline inside that construct above is the list of process IDs so you end up with a command likekill 1234 1122 7654
.
Here's a transcript showing it in action:
pax> sleep 3600 &
[1] 2225
pax> sleep 3600 &
[2] 2226
pax> sleep 3600 &
[3] 2227
pax> sleep 3600 &
[4] 2228
pax> sleep 3600 &
[5] 2229
pax> kill $(ps aux | grep '[s]leep' | awk '{print $2}')
[5]+ Terminated sleep 3600
[1] Terminated sleep 3600
[2] Terminated sleep 3600
[3]- Terminated sleep 3600
[4]+ Terminated sleep 3600
and you can see it terminating all the sleepers.
Explaining the grep '[p]ython csp_build.py'
bit in a bit more detail:
When you do sleep 3600 &
followed by ps -ef | grep sleep
, you tend to get two processes with sleep
in it, the sleep 3600
and the grep sleep
(because they both have sleep
in them, that's not rocket science).
However, ps -ef | grep '[s]leep'
won't create a process with sleep
in it, it instead creates grep '[s]leep'
and here's the tricky bit: the grep
doesn't find it because it's looking for the regular expression "any character from the character class [s]
(which is s
) followed by leep
.
In other words, it's looking for sleep
but the grep process is grep '[s]leep'
which doesn't have sleep
in it.
When I was shown this (by someone here on SO), I immediately started using it because
- it's one less process than adding
| grep -v grep
; and - it's elegant and sneaky, a rare combination :-)
How to display PID 100 processes by using ps and grep?
Using awk:
$ ps -ef | awk '$2<100'
Extract PID from top in UNIX
Here is the baseline for it, you can add more grep and pipe if you want specific user
top -p $(ps -fea|grep -i java |grep -v grep|cut -d' ' -f3 | tr '\n' ','| head --bytes -1 )
How to always cut the PID from `ps aux` command?
-d ' '
means using a single space as delimiter. Since there're 1 space before 2049 and 2 spaces before 12290, your command get them by -f 2
and -f 3
.
I recommend using ps aux | awk '{print $2}'
to get those pids.
Or you can use tr
to squeeze those spaces first
ps aux | tr -s ' ' | cut -d ' ' -f 2
Related Topics
How Would One Disable Nagle's Algorithm in Linux
Error on Execution -Version 'Qt_5' Not Found Required By
Ioctl VS Netlink VS Memmap to Communicate Between Kernel Space and User Space
How to Read Single Character Input from Keyboard Using Nasm (Assembly) Under Ubuntu
How to Check If Sed Has Changed a File
How to Decode /Proc/Pid/Pagemap Entries in Linux
Sorting on the Last Field of a Line
How to Export the Variable Through Script File
How to Copy the Output of a Command Directly into My Clipboard
How to Search Contents of Multiple PDF Files
How Find Out Which Process Is Using a File in Linux
How to Kill All Linux Processes That Are Older Than a Certain Age
How to Find Directory of Some Command
Why Is Rcx Not Used for Passing Parameters to System Calls, Being Replaced with R10
Perl Command Line Multi-Line Replace
Openshift: "Failed to Execute Control Start" on Node Application