Skip to content

Latest commit

 

History

History
56 lines (44 loc) · 1.47 KB

find_and_kill_a_process.md

File metadata and controls

56 lines (44 loc) · 1.47 KB

Find and kill a process

Requirements:

Know what you are looking for

I am going to make a test script

File: example


#!/bin/bash
#Sleep for 10 Minutes in seconds, The ampersand(&) makes it run in the background 
sleep 600 &

make executable

chmod +x example # make it executable

I'll run it a bunch of times

for i in {1..10}; do ./example; done

running ps -e | grep sleep shows all the instances of sleep

ps -e | grep sleep
   67 tty1     00:00:00 sleep
   75 tty1     00:00:00 sleep
   77 tty1     00:00:00 sleep
   79 tty1     00:00:00 sleep
   81 tty1     00:00:00 sleep
   83 tty1     00:00:00 sleep
   85 tty1     00:00:00 sleep
   87 tty1     00:00:00 sleep
   89 tty1     00:00:00 sleep
   91 tty1     00:00:00 sleep
   93 tty1     00:00:00 sleep

NOW LETS KILL THEM ALL USING, kill and awk and some linux awesomeness with a one-liner!

kill `ps -e | awk '/sleep/{print $1}'`

this will print out every PID and Process running, use awk to filter for a string like grep with the //
then the action you want to do in {}
Now we want to print the PID which is the first string before a space (The Field Separator(FS))
Luckily the PID is the first so we just tell it to print the first! with print $1 (variable 1)

having the ps -e command and awk in backticks (prime) ` ` means it will forward the output to the beginning kill command (our end goal)

DONE!