top. Then after finding the process ID you'd use the
kill [process id]command to kill it. This is usually an annoying task if you have to repeat it alot, an example is shown below:
jake@jakswa-laptop:~$ ps -A
14063 ? 00:00:03 notification-da
17215 ? 00:00:03 pidgin
19981 ? 00:22:14 rhythmbox
24794 ? 00:03:40 firefox
26655 ? 00:00:00 sh
26656 ? 00:00:00 gnome-terminal
26658 ? 00:00:00 gnome-pty-helpe
26659 pts/0 00:00:00 bash
26768 pts/0 00:00:00 ps
jake@jakswa-laptop:~$ kill 24794
However, wouldn't it be nice to just say "killname firefox" and have something automate that process? You CAN! This n00b is excited, because he just made a script to do it! If you want to understand what I did, you should know about piping outputs of commands, and maybe something about regular expressions.
ps -A | grep $1 | sed -e "s/ *\([0-9]*\) .*/\1/" | xargs kill
What does this do? I'll explain it step by step.
ps -Alists every process with PID and name.
- the first vertical bar sends every line that
ps -Aprints (like
24794 ? 00:03:40 firefox) as the input to the
grep $1 [input]command.
So, grep ends up being something like:
24794 ? 00:03:40 firefox" |
grep firefox(search "24794..." for "firefox")
where $1 is replaced by whatever process we want to kill — 'firefox' in this case.
grepfinds every line that has the search term, 'firefox', and 'pipes' it to the Stream EDitor (sed) command (this is done by the second vertical bar).
- The sed command filters out only the PID of the line that was sent to it by grep, and passes that PID onto the
- 'xargs' takes the given PID and uses it as the argument in a "kill" command
So maybe you'd like to run this script as any other command in the terminal. Well all we have to do is copy the script to the '/bin' folder. Just make sure you're root:
Now you can run it in the terminal using the name of the script file.
sudo cp [scriptfile] /bin
Example (to kill 'firefox' with script as "killname"):
Voila! This might not be the best way to do things, and I doubt the script is very error proof (don't run it as root, or else you might kill some important processes). Mainly, this was made to become more comfortable with bash scripting.
More info on pipelining: Pipeline (Unix) - Wikipedia
More info on regular expressions: Regular Expressions - Wikipedia