Introduction
Sometimes it becomes necessary to stop processes run by a user, whether for troubleshooting or due to overwhelming resource usage. The following guide will assist in finding and stopping multiple processes.
Procedure
First, find the processes you're interested in stopping. The ps utility can list running processes, and grep can be used to filter the output. Here, I am searching for all processes by a user. In this guide, I'll be using the user cptest and the process sleep
# ps aux | grep cptest
cptest 1058485 0.0 0.0 108056 356 pts/0 S 01:58 0:00 sleep 10000000
cptest 1058487 0.0 0.0 108056 356 pts/0 S 01:58 0:00 sleep 10000000
cptest 1058489 0.0 0.0 108056 356 pts/0 S 01:58 0:00 sleep 10000000
cptest 1058490 0.0 0.0 108056 356 pts/0 S 01:58 0:00 sleep 10000000
cptest 1058492 0.0 0.0 108056 352 pts/0 S 01:58 0:00 sleep 10000000
cptest 1058494 0.0 0.0 108056 352 pts/0 S 01:58 0:00 sleep 10000000
cptest 1058497 0.0 0.0 108056 352 pts/0 S 01:58 0:00 sleep 10000000
cptest 1058498 0.0 0.0 108056 356 pts/0 S 01:58 0:00 sleep 10000000
cptest 1058500 0.0 0.0 108056 356 pts/0 S 01:58 0:00 sleep 10000000
cptest 1058501 0.0 0.0 108056 356 pts/0 S 01:58 0:00 sleep 10000000
cptest 1058503 0.0 0.0 108056 352 pts/0 S 01:58 0:00 sleep 10000000
cptest 1058504 0.0 0.0 108056 352 pts/0 S 01:58 0:00 sleep 10000000
cptest 1058506 0.0 0.0 108056 356 pts/0 S 01:58 0:00 sleep 10000000
cptest 1058507 0.0 0.0 108056 352 pts/0 S 01:58 0:00 sleep 10000000
root 1058519 0.0 0.0 112816 972 pts/0 S+ 01:58 0:00 grep --color=auto cptest
But, if you notice, there is an extra process in that list that is not from that user. This is the search query you've just made. If you leave it in, you'll get a warning about that process not existing further in the guide. It is simply a warning, but it is also easy to remove.
root 1058519 0.0 0.0 112816 972 pts/0 S+ 01:58 0:00 grep --color=auto cptest
To remove this entry, format your first search like this, surrounding the query in single quotes, and the first letter in square brackets:
# ps aux | grep '[c]ptest'
cptest 1058485 0.0 0.0 108056 356 pts/0 S 01:58 0:00 sleep 10000000
cptest 1058487 0.0 0.0 108056 356 pts/0 S 01:58 0:00 sleep 10000000
cptest 1058489 0.0 0.0 108056 356 pts/0 S 01:58 0:00 sleep 10000000
cptest 1058490 0.0 0.0 108056 356 pts/0 S 01:58 0:00 sleep 10000000
cptest 1058492 0.0 0.0 108056 352 pts/0 S 01:58 0:00 sleep 10000000
cptest 1058494 0.0 0.0 108056 352 pts/0 S 01:58 0:00 sleep 10000000
cptest 1058497 0.0 0.0 108056 352 pts/0 S 01:58 0:00 sleep 10000000
cptest 1058498 0.0 0.0 108056 356 pts/0 S 01:58 0:00 sleep 10000000
cptest 1058500 0.0 0.0 108056 356 pts/0 S 01:58 0:00 sleep 10000000
cptest 1058501 0.0 0.0 108056 356 pts/0 S 01:58 0:00 sleep 10000000
cptest 1058503 0.0 0.0 108056 352 pts/0 S 01:58 0:00 sleep 10000000
cptest 1058504 0.0 0.0 108056 352 pts/0 S 01:58 0:00 sleep 10000000
cptest 1058506 0.0 0.0 108056 356 pts/0 S 01:58 0:00 sleep 10000000
cptest 1058507 0.0 0.0 108056 352 pts/0 S 01:58 0:00 sleep 10000000
You can then filter this further. In my case, I only have one type of process running under this user, but the syntax is as follows:
# ps aux | grep '[c]ptest' | grep sleep
cptest 1058485 0.0 0.0 108056 356 pts/0 S 01:58 0:00 sleep 10000000
cptest 1058487 0.0 0.0 108056 356 pts/0 S 01:58 0:00 sleep 10000000
cptest 1058489 0.0 0.0 108056 356 pts/0 S 01:58 0:00 sleep 10000000
cptest 1058490 0.0 0.0 108056 356 pts/0 S 01:58 0:00 sleep 10000000
cptest 1058492 0.0 0.0 108056 352 pts/0 S 01:58 0:00 sleep 10000000
cptest 1058494 0.0 0.0 108056 352 pts/0 S 01:58 0:00 sleep 10000000
cptest 1058497 0.0 0.0 108056 352 pts/0 S 01:58 0:00 sleep 10000000
cptest 1058498 0.0 0.0 108056 356 pts/0 S 01:58 0:00 sleep 10000000
cptest 1058500 0.0 0.0 108056 356 pts/0 S 01:58 0:00 sleep 10000000
cptest 1058501 0.0 0.0 108056 356 pts/0 S 01:58 0:00 sleep 10000000
cptest 1058503 0.0 0.0 108056 352 pts/0 S 01:58 0:00 sleep 10000000
cptest 1058504 0.0 0.0 108056 352 pts/0 S 01:58 0:00 sleep 10000000
cptest 1058506 0.0 0.0 108056 356 pts/0 S 01:58 0:00 sleep 10000000
cptest 1058507 0.0 0.0 108056 352 pts/0 S 01:58 0:00 sleep 10000000
As you can see, the special formatting is not needed for subsequent filters.
The kill utility uses the Process Identifier or PID to identify what processes it is going to stop. You can use awk to filter for it from the second column:
# ps aux | grep '[c]ptest' | grep sleep | awk '{ print $2 }'
1058485
1058487
1058489
1058490
1058492
1058494
1058497
1058498
1058500
1058501
1058503
1058504
1058506
1058507
You can use the following syntax to place the output of a command in-line with the arguments for another command:
# command1 $(command2)
We're going to use this feature to feed the PID list we found to the kill utility:
# kill $(ps aux | grep '[c]ptest' | grep sleep | awk '{ print $2 }')
You should now see the processes are no longer running:
# ps aux | grep '[c]ptest' | grep sleep
#