When you try to terminate a process for good, which option for “kill” should you use

killprocesssignals

I am getting a lot of mixed messages from people, and was wondering if it there really is not much of a difference in what you use.

kill 'x'
killall 'x'
kill -9 'x'

These are some options I've been told to use so far, but some people say that kill -9 is just overkill, kill by itself doesn't work, etc.

Does anyone have any advice on which one should be used in the most standard case of just terminating a process (and thus its state as well)?

[edit] I am looking for what to use when you want to stop the execution of your process so that you can run a new one, fresh from the start

Best Answer

There are a number of signals whose default disposition is to terminate the process. The ultimate termination signal is SIGKILL since it cannot be handled and the process has no choice but to die. This however also means that if you send it, the process is deprived of an opportunity to clean up. Therefore, good manners require to send a signal like SIGTERM that can be handled first and only if the process does not exit after some time send it SIGKILL.

Note that SIGINT and SIGQUIT are not good candidates for arbitrary process termination. Due to the fact that they can be generated from terminal's keyboard, many applications use them for special purposes. For example, python interpreter uses SIGINT to generate KeyboardInterrupt exception (also in interactive python sessions where it simply returns to the prompt) and JVM uses SIGQUIT to dump stack traces. SIGINT and SIGQUIT do remain effective for most standard command-line utilities like find or cat.

During system shutdown, most UNIX and Linux systems send SIGTERM to all process, followed by 5 seconds wait, followed by SIGKILL. This is the recommended way to safely shut down an arbitrary process.

Note also that even SIGKILL may not terminate a process stuck in an uninterruptible wait until the process wakes up.