26

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

2 Answers2

25

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.

ephsmith
  • 1,006
Adam Zalcman
  • 4,331
  • 1
    Excellent answer. Do you have a reference for the kill -SIGTERM $pid; sleep 5; kill -SIGKILL $pid; recommendation? – l0b0 Jan 09 '12 at 16:20
  • +1 kind of answer. And what about SIGHUP? – Alois Mahdal May 13 '12 at 22:02
  • 2
    @AloisMahdal: SIGHUP comes from the day when dial-up lines were common. That signal was sent to indicate that the line hung-up. Later, when that was less common SIGHUP was sometimes used by daemons to re-read the config file without restarting the process. In these modern times SIGHUP is rarely used. Many daemons will have their own way of reinitializing (i.e., apachectl graceful or rndc reconfig. For things that don't you should use [init.d|smf|upstart|launchd] controls where possible. – bahamat Jun 27 '12 at 18:54
15

You should start with the gentlest one and escalate from there. This means, SIGINT, SIGTERM, SIGQUIT, SIGKILL. Although most people skip SIGINT and SIGQUIT.

  • Additionally some applications (such as the JDK) handle SIGQUIT differently. I probably wouldn't use SIGQUIT without knowing how the specific application handles it. – dimo414 Mar 12 '18 at 23:59