How to kill all processes with a given partial name?
Use pkill -f
, which matches the pattern for any part of the command line
pkill -f my_pattern
Just in case it doesn't work, try to use this one as well:
pkill -9 -f my_pattern
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 kill all processes matching a name?
From man 1 pkill
-f The pattern is normally only matched against the process name.
When -f is set, the full command line is used.
Which means, for example, if we see these lines in ps aux
:
apache 24268 0.0 2.6 388152 27116 ? S Jun13 0:10 /usr/sbin/httpd
apache 24272 0.0 2.6 387944 27104 ? S Jun13 0:09 /usr/sbin/httpd
apache 24319 0.0 2.6 387884 27316 ? S Jun15 0:04 /usr/sbin/httpd
We can kill them all using the pkill -f
option:
pkill -f httpd
How to run two process independently with their own thread pool and other configuration?
There is a clear violation of DRY (Don't Repeat Yourself) principle in your code.
In other words, there is lots of Boilerplate code in your Process
and Main
classes which can be eliminated by using the abstract classes (or use interfaces if you use Java8 with default
methods).
So I have created two Process
and ProcessHandler
abstract classes to reuse the code which is common across each process and process handling.
So now, you can define ProcessA
, ProcessB
classes which extend Process and ProcessHandlerA
, ProcesshandlerB
which extend ProcessHandler class.
The key point is this solution can be extended to any number of Process
**, i.e., this follows **Open/Closed principle of OOP.
You can refer the below code with comments:
Process class (abstract):
public abstract Process implements Runnable {
private final Properties props;
private final String processName;
public Process(String processName, Properties props) {
this.processName = processName;
this.props = props;
}
//this can also be a non abstract (reusable) method
// to eliminate boiler plate code (if any)
public abstract void shutdown();
}
ProcessA class:
public class ProcessA extends Process {
public ProcessA(String processName, Properties props) {
super(processName, props);
}
@Override
public void run() {
//add run code here
}
@Override
public void shutdown() {
//shut down code
}
}
Process B class:
//Similar to ProcessA
with specific details of B
ProcessHandler class (abstract):
public abstract class ProcessHandler {
private final ExecutorService executorServiceProcess;
private final List<Process> processList;
private int poolSize;
protected ProcessHandler(int poolSize) {
executorServiceProcess = Executors.newFixedThreadPool(poolSize);
processList = new ArrayList<>();
this.poolSize = poolSize;
}
public void postInit(Process process) {
for (int i = 0; i < poolSize; i++) {
processList.add(process);
executorServiceProcess.submit(process);
}
}
public void shutdown() {
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
for (Process process : processList) {
process.shutdown();
}
executorServiceProcess.shutdown();
try {
executorServiceProcess.
awaitTermination(1000, TimeUnit.MILLISECONDS);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
});
}
}
ProcessHandlerA class:
public class ProcessHandlerA extends ProcessHandler {
public ProcessHandlerA() {
super(3);//configure pool size properly w.r.to ProcessA requirements
}
public void postInit() {
ProcessA processA = new ProcessA("processA", properties);
super(processA);
}
public void shutdown() {
super.shutdown();
}
}
ProcessHandlerB class:
//Similar to ProcessHandlerA with specific details for B
Related Topics
Use "Git Revert" to Back-Out a Change Adding a Line
What Is The Side Effect of Setting Tcp_Max_Tw_Buckets to a Very Small Value
How Does Iwlist() Command Scans The Wireless Networks
Create and Test X86-64 Elf Executable Shellcode on a Linux Machine
What Algorithm How to Use to Generate a 48-Bit Hash for Unique MAC Addresses
Inspect Network Traffic from Simple Linux Cli App
A Mess with Different Perl Installs
Get Canonical Path from Pathname
Avoid Copying of Data Between User and Kernel Space and Vice-Versa
Lkm: Last Block Written to Device
Process Scheduling from Processor Point of View
How to Reset Tty After Exec-Ed Program Crashes
How to Delete X Number of Files in a Directory
Interpreting Openssl Speed Output for Rsa with Multi Option
Raspberry Pi: Spi Not Working, Spi_Bcm2835 Not Showing with Lsmod