sudo killall java not eclipse
I would suggest that you use jps
to list the Java processes and then pipe the output through grep -v
to filter out the eclipse process and cut
to extract the pids. Then supply those as command line arguments to kill
.
For example:
jps | egrep -v "eclipse" | egrep -v "Jps" | cut -b1-6 | xargs -t kill
You can use jps -l
for more accurate identification of the java processes to be killed.
Linux Script to selectively kill processes
Consider pgrep
:
pgrep -U 0 java | while read pid; do
pargs "$pid" | grep -qF XYZ && kill "$pid"
done
pgrep
and pkill
are available on many Linux systems and as part of the "proctools" packages for *BSDs and OS X.
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 :-)
Related Topics
How to Route Webcam Video to Virtual Video Device on Linux (Via Opencv)
How to Get the Exit Status of the First Command in a Pipe
Changing the Owner of an Existing Process in Linux
Why Doesn't Set -E Cause a Failure with 'False || False && True'
Curl Command Doesn't Work in Bash Script
Can Libpcap Reassemble Tcp Segments
File Glob Patterns in Linux Terminal
How to Modify Eip's Tracee Forked Procee
How to Cross-Compile a Qt Application for Imx6
Find Files with a Certain Extension That Exceeds a Certain File Size
Why Count Differs Between Ls and Ls -L Linux Command
How Can a Program Detect If It Is Running as a Systemd Daemon
Why Can't Cuda's Examples Makefile Find the Cuda Libraries
Raspberry Pi Refusing Connection to Bottle Server