Geek Stuff
| Become a serial killer at the linux command line |
|
|
|
| Written by Frank Emmons |
| Friday, 07 August 2009 21:04 |
|
You will be faced with a choice: A hard reboot or hunt down those processes and kill them. I don't know about the rest of you, but I got away from MS Windows because I hate the idea of hard rebooting everytime the OS goes nuts. So, I learned how to mass murder those run away apps that I let suck up all my system resources from the command line. Personally, I don't think any Linux users toybox is complete without the following bash script:
I put those lines in a file saved as /usr/bin/killthatbitch. Then I made it executable by it's permissions to 755. So I can be working anywhere in my filesystem and type 'killthatbitch <insert process name here>' and it gets whacked .. FAST! There could be a thousand instances of that process running and it kills them all. Sweet, eh? Some of you Linux newbies may be wondering how exactly that line works. Well, is pretty straight forward. I'll break it down into parts.
'ps' reports a snapshot of the current processes. The 'axww' tells ps to show all show all with and without controlling terminals (inclusive of user processes) and in a wide format report. The '-u $USER' defines that you only want to list processes that are owned by the user that you are currently logged in as.
'grep' is the utility that prints lines matching a given pattern. The '$1' is us telling it to match the first parameter typed after the command. So if we typed 'killthatbitch firefox', it would grep for the string 'firefox' in the list of processes returned from the 'ps'.
'awk' or 'gawk' is defined as a pattern scanning and processing language. It takes the full output of the lines handed to it from the 'grep' portion of the statement and returns or echoes the first string of each line. In this instance, that would be the actual process ID (PID) of the process itself.
It is the job of 'xargs' to build and execute command lines from standard input. The standard input it receives is the run away PID it got from the 'awk' portion. It takes that ID and hands it to 'kill'. If you have not figured it out by now, 'kill' terminates (kills off, ends, fini, etc) any process that matches the PID it has been given. I showed this trick to a friend of mine a couple weeks ago and he asked me 'Why the -u $USER?'. That's simple. Because I did not want to try to mass murder processes I did not start in the first place. If the $USER was not in that statement, our simple serial killer script would go after any process running on the box that matched the string we give it when we run the command and that could be very very bad on a box where multiple users are working at once. Let the serial killing begin!
|
| Last Updated on Friday, 07 August 2009 21:51 |