Killing things in Unix-type environments

i

Wannabe Storage Freak
Joined
Feb 10, 2002
Messages
1,080
None of this may be news to any of you, but just in case here are some things in Linux and other UNIX-style environments I find useful.

Feel free to jump in here if I'm understanding any of this stuff incorrectly, or if there's more detail to any of it you think would be good to know about.

-1-
Ctrl+Alt+Backspace kills the current X session.

-2-
Alt+SysRq, followed by particular keystrokes can do a variety of powerful things. See this site for the details. If your systems has this function disabled, this HOWTO may help.

-3-
"ps -aux" is your friend. However, if you're running in a text only mode, chances are your screen won't be able to show all the processes without scrolling. While "Shift+PageUp" is another friend of yours, piping the output from "ps -aux" through grep might be quicker, e.g.:

ps -aux | grep xmms

This will also turn up the very same "ps -aux" command you've just issued though (UNIX is that thorough), which you might find kind of annoying. You can enclose one of the letters of the string you're grepping for in square brackets as a trick to avoid that issue. For example:

ps -aux | grep xm[m]s

The character(s) within the square brackets will get interpretted by your shell, but not by ps. So while the shell will know that you're looking to match "xmms", ps will still be seeing "xm[m]s" and thus won't match the command that called itself up in the first place. Try it a few times with and without the brackets and you'll see what I mean.

What do the square brackets do exactly? See this example and you'll figure it out:

[me@home me]$ ps -aux | grep [aedgiklstu]_applet
me 8234 0.0 0.7 9492 3968 ? S 19:03 0:01 tasklist_applet
me 8236 0.0 0.7 9476 3876 ? S 19:03 0:00 deskguide_applet

-4-
And finally we have the beautiful "kill" command. Kill can send a variety of signals to a currently running process. Based on the name "kill", you can guess what sort of signals are most frequently used. It's worth noting however that kill gives you a fair degree of control over just how brutally you want to end a process. In fact, depending on how the process you're interested in "nudging" has been written, some of the signals you can send using kill will be interpretted as a restart command, or something completely different. Use the "-s" switch to send the signal of your choice to the selected process ID (PID).

The "-l" (list) switch works on all the UNIX variants I've ever tried - which is a good thing because the signal names vary depending on what type of UNIX system you're sitting at. So before you issue a kill command for the first time on a system you haven't used kill with before, take the extra 5 seconds to issue a "kill -l" command first.

This is one reason to avoid using numeric signal definitions. For example, signal #9 may not mean the same thing on different types of UNIX systems. So avoid things like "kill -9" (that specific one is something you should try and avoid anyway because that's a brutal method of killing a process even on a system where -9 does get interpreted as you'd expect).

From a SunOS/Solaris box:
systems% kill -l
HUP INT QUIT ILL TRAP ABRT EMT FPE
KILL BUS SEGV SYS PIPE ALRM TERM USR1
USR2 CLD PWR WINCH URG POLL STOP TSTP
CONT TTIN TTOU VTALRM PROF XCPU XFSZ WAITING
LWP FREEZE THAW CANCEL LOST XRES RTMIN RTMIN+1
RTMIN+2 RTMIN+3 RTMAX-3 RTMAX-2 RTMAX-1 RTMAX

From a RedHat Linux box:
[me@home me]$ kill -l
1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL
5) SIGTRAP 6) SIGABRT 7) SIGBUS 8) SIGFPE
9) SIGKILL 10) SIGUSR1 11) SIGSEGV 12) SIGUSR2
13) SIGPIPE 14) SIGALRM 15) SIGTERM 17) SIGCHLD
18) SIGCONT 19) SIGSTOP 20) SIGTSTP 21) SIGTTIN
22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ
26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO
30) SIGPWR 31) SIGSYS 32) SIGRTMIN 33) SIGRTMIN+1
34) SIGRTMIN+2 35) SIGRTMIN+3 36) SIGRTMIN+4 37) SIGRTMIN+5
38) SIGRTMIN+6 39) SIGRTMIN+7 40) SIGRTMIN+8 41) SIGRTMIN+9
42) SIGRTMIN+10 43) SIGRTMIN+11 44) SIGRTMIN+12 45) SIGRTMIN+13
46) SIGRTMIN+14 47) SIGRTMIN+15 48) SIGRTMAX-15 49) SIGRTMAX-14
50) SIGRTMAX-13 51) SIGRTMAX-12 52) SIGRTMAX-11 53) SIGRTMAX-10
54) SIGRTMAX-9 55) SIGRTMAX-8 56) SIGRTMAX-7 57) SIGRTMAX-6
58) SIGRTMAX-5 59) SIGRTMAX-4 60) SIGRTMAX-3 61) SIGRTMAX-2
62) SIGRTMAX-1 63) SIGRTMAX

From an OpenBSD box:
inferno:hexadm {101} kill -l
HUP INT QUIT ILL TRAP ABRT EMT FPE KILL BUS SEGV SYS PIPE ALRM TERM URG
STOP TSTP CONT CHLD TTIN TTOU IO XCPU XFSZ VTALRM PROF WINCH INFO USR1 USR2


I think I've only ever used HUP, TERM, and KILL. Anyone know what some of the others are for?

And does anyone here want to offer advice on the best way to deal with a misbehaving process using kill? I've heard some people give very strong opinions about this. I usually just send a hang-up signal. That's usually enough for the occasional binds I find myself in. The terminate signal is my next choice (and I rarely use it), followed by the extremely rare KILL signal (can't remember the last time I've used that one). I remember reading a very annoyed sysadmin's comment years ago that you're better off deleting the binary than issuing the KILL signal because only an incredibly badly behaved app would ignore TERM.
 

Mercutio

Fatwah on Western Digital
Joined
Jan 17, 2002
Messages
21,564
Location
I am omnipresent
I have a large book called "Design and Implementation of the 4.3BSD Operating System" that goes into far FAR more detail about process management than anyone but a system developer would ever need. If you're that guy, it's a great thing to read.

Me? I pretty much kill -9 or kill -HUP and don't worry about the rest.
 
Top