Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

While it's not in Perl (it uses grep, sed and awk), I like this Unix one-liner (mine) to kill a hanging Firefox process, not so much for the code but for the interesting comment thread that it resulted in on my blog - about Unix processes, zombies, etc.

UNIX one-liner to kill a hanging Firefox process:

http://jugad2.blogspot.in/2008/09/unix-one-liner-to-kill-han...



  kill `ps -opid OC -C chrome | tail -n1`
Kills only the highest-CPU-use Chrome pid.

You can get a lot of useful information out of PS, like blocked, caught, ignored & pending signals, control group, scheduling class & policy, CPU in tenths of a percent, instruction & stack pointers, elapsed time since it started, security label, thread id, number of threads, kernel function that the process is currently sleeping in, process state, current CPU, number of kernel threads, controlling tty, etc.

Also, never use killall. On Solaris it reboots the machine. Fun to find out when logged in as root.


Thanks for the tip. Didn't know you could get so much info from ps. Will re-check its man page.

Do you have any recommendation on books or online sites for learning about the more advanced topics you mention above (the many Unix topics that you say ps gives info on, not just ps itself)? I know about the Stevens [1] series of Unix and networking books, and have read parts of some, but that was a while ago. Things must have changed a good amount since then. Should still read Stevens again, I guess.

[1] https://en.wikipedia.org/wiki/W._Richard_Stevens


Some of those topics are basic UNIX programming things (signals, threads, process state, controlling tty). Others are features of the operating system, so you would want something Linux-specific that tells you about kernel programming or kernel features you can use in your applications (security labels, scheduling classes, control groups, kernel threads, etc). Still others are features of the OS & cpu's execution of programs (instruction/stack pointers), you'll want to learn Assembly for that.

Sorry I don't have reading recommendations. I don't really read as much as just diving into man pages and looking up guides online. Hacker habits...


No problem. I do know what some of the points mean (signals, threads, control groups, instruction/stack pointers, etc.), just did not know some of the others (like security labels, threads vs. kernel threads, etc.). I guess some of those topics might not be found in one or a few books, more like scattered across books, articles, man pages etc. ...


I see you got this comment already, but still: Make a habit of using pkill instead of stuffing ps output to kill when you can. It's not everywhere, but it's on Linux/BSD/Solaris. (Avoid killall; that's unportable is a special way.) That's readable and avoids things like filtering out your greps.


Interesting, didn't know of that reason for using pkill - thanks.


Thanks for the introduction to 'sed 1d', which is something I've somehow never come across and resorted to horrible head|tail hacks to strip fixed header lines off things.


NP. You may know this, but there is also 'sed nq' where n is a number.

E.g.:

sed 15q file.txt

which is like the 'head' command - prints the 1st 15 lines of file.txt and then stops.


Use `xkill` (if you're using X). `xkill` in a terminal, then click on the window you want to kill. Boom. No need to type out a convoluted command.


Sadly there are situations when a hanging Firefox doesn't have a window to place the xkill skull-and-crossbones cursor if death on.


IIRC, I was using Linux on my PC and from a text console, not a console in the GUI env., when I wrote that one-liner, and once written, it can be put in a script with a short name. But didn't know of xkill, thanks.




Consider applying for YC's Fall 2026 batch! Applications are open till July 27.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: