Sometimes we acidentally spawn a series of processes, and we want to kill them. We can look up their pid's through ps -aux | grep <process_name>
(as shown below) and manually run the kill
command to kill each process by providing its pid, but how can we automate this tedious task ?
1 2 3 4 5 6 7 8 9 10 $ ps aux | grep pipreqs jifengwu 58180 0.0 0.1 46440 27332 pts/0 T 13:38 0:00 /home/jifengwu/miniconda3/envs/dataset_investigation/bin/python /home/jifengwu/miniconda3/envs/dataset_investigation/bin/pipreqs repos/spectria/tildes/tildes --mode no-pin jifengwu 58205 0.1 0.1 48140 29392 pts/0 T 13:38 0:00 /home/jifengwu/miniconda3/envs/dataset_investigation/bin/python /home/jifengwu/miniconda3/envs/dataset_investigation/bin/pipreqs repos/PnX-SI/GeoNature --mode no-pin jifengwu 58224 5.7 0.2 51856 33108 pts/0 T 13:38 0:23 /home/jifengwu/miniconda3/envs/dataset_investigation/bin/python /home/jifengwu/miniconda3/envs/dataset_investigation/bin/pipreqs repos/fabiandevia/home --mode no-pin jifengwu 58267 4.4 0.2 57880 38204 pts/0 T 13:39 0:17 /home/jifengwu/miniconda3/envs/dataset_investigation/bin/python /home/jifengwu/miniconda3/envs/dataset_investigation/bin/pipreqs repos/377312117/gitproject --mode no-pin jifengwu 58272 2.3 0.2 53756 34252 pts/0 T 13:39 0:08 /home/jifengwu/miniconda3/envs/dataset_investigation/bin/python /home/jifengwu/miniconda3/envs/dataset_investigation/bin/pipreqs repos/crazyfish1111/home --mode no-pin jifengwu 58282 0.1 0.1 47840 28132 pts/0 T 13:39 0:00 /home/jifengwu/miniconda3/envs/dataset_investigation/bin/python /home/jifengwu/miniconda3/envs/dataset_investigation/bin/pipreqs repos/Piratenpartei/ekklesia-portal --mode no-pin jifengwu 58295 0.1 0.1 48220 28492 pts/0 T 13:39 0:00 /home/jifengwu/miniconda3/envs/dataset_investigation/bin/python /home/jifengwu/miniconda3/envs/dataset_investigation/bin/pipreqs repos/jauhararifin/ugrade/server --mode no-pin jifengwu 58659 0.3 0.1 48608 29324 pts/0 T 13:41 0:00 /home/jifengwu/miniconda3/envs/dataset_investigation/bin/python /home/jifengwu/miniconda3/envs/dataset_investigation/bin/pipreqs repos/klen/pylama --mode no-pin jifengwu 59564 0.0 0.0 19612 2516 pts/2 S+ 13:45 0:00 grep --color=auto pipreqs
First, we can add grep -v grep
to the pipe to hide the grep processes from the output :
1 2 3 4 5 6 7 8 9 $ ps aux | grep pipreqs | grep -v grep jifengwu 58180 0.0 0.1 46440 27332 pts/0 T 13:38 0:00 /home/jifengwu/miniconda3/envs/dataset_investigation/bin/python /home/jifengwu/miniconda3/envs/dataset_investigation/bin/pipreqs repos/spectria/tildes/tildes --mode no-pin jifengwu 58205 0.1 0.1 48140 29392 pts/0 T 13:38 0:00 /home/jifengwu/miniconda3/envs/dataset_investigation/bin/python /home/jifengwu/miniconda3/envs/dataset_investigation/bin/pipreqs repos/PnX-SI/GeoNature --mode no-pin jifengwu 58224 5.6 0.2 51856 33108 pts/0 T 13:38 0:23 /home/jifengwu/miniconda3/envs/dataset_investigation/bin/python /home/jifengwu/miniconda3/envs/dataset_investigation/bin/pipreqs repos/fabiandevia/home --mode no-pin jifengwu 58267 4.4 0.2 57880 38204 pts/0 T 13:39 0:17 /home/jifengwu/miniconda3/envs/dataset_investigation/bin/python /home/jifengwu/miniconda3/envs/dataset_investigation/bin/pipreqs repos/377312117/gitproject --mode no-pin jifengwu 58272 2.2 0.2 53756 34252 pts/0 T 13:39 0:08 /home/jifengwu/miniconda3/envs/dataset_investigation/bin/python /home/jifengwu/miniconda3/envs/dataset_investigation/bin/pipreqs repos/crazyfish1111/home --mode no-pin jifengwu 58282 0.1 0.1 47840 28132 pts/0 T 13:39 0:00 /home/jifengwu/miniconda3/envs/dataset_investigation/bin/python /home/jifengwu/miniconda3/envs/dataset_investigation/bin/pipreqs repos/Piratenpartei/ekklesia-portal --mode no-pin jifengwu 58295 0.1 0.1 48220 28492 pts/0 T 13:39 0:00 /home/jifengwu/miniconda3/envs/dataset_investigation/bin/python /home/jifengwu/miniconda3/envs/dataset_investigation/bin/pipreqs repos/jauhararifin/ugrade/server --mode no-pin jifengwu 58659 0.3 0.1 48608 29324 pts/0 T 13:41 0:00 /home/jifengwu/miniconda3/envs/dataset_investigation/bin/python /home/jifengwu/miniconda3/envs/dataset_investigation/bin/pipreqs repos/klen/pylama --mode no-pin
Then, we can add awk '{print $2}'
to the pipe to invoke awk
to trim the second space-delimited component (which in this case is the pid). Now we have a list of the pid's of the processes we want to kill:
1 2 3 4 5 6 7 8 9 $ ps aux | grep pipreqs | grep -v grep | awk '{print $2}' 58180 58205 58224 58267 58272 58282 58295 58659
Finally, we can iterate over the pid's in a for-loop to kill them.
1 2 3 4 $ for pid in $(ps aux | grep pipreqs | grep -v grep | awk '{print $2}' ) > do > kill -15 $pid > done
References
https://www.baeldung.com/linux/grep-exclude-ps-results https://stackoverflow.com/questions/46008880/how-to-always-cut-the-pid-from-ps-aux-command