0

I have found a way to logout any user by using the command line. By executing the command pkill -KILL -u <username>, I can now log myself out from the session I have entered.
My question is that why there is no description about -KILL switch or option available neither in man pkill nor in pkill --help.
I am using Ubuntu Mate 16.04. Thanck you very much in advance.

Tower
  • 479
  • 3
    That's an extraordinarily bad way to log out. See e.g. https://unix.stackexchange.com/questions/281439/why-should-i-not-use-kill-9-sigkill – Kusalananda Jan 31 '18 at 16:47

1 Answers1

6

The -KILL argument is telling pkill which signal to send to all of the processes identified by the other arguments. So you are sending SIGKILL which is a very very unclean way do do what you appear to be trying to do. You probably want to send SIGHUP (with -HUP) or SIGTERM (with -TERM or omitting it, as SIGTERM is the default signal for kill/pkill).

To simplify, when a process receives SIGTERM from the kernel, it is being told: "you need to go away now. Clean up after yourself and self-terminate". Most well-designed programs will upon receiving this signal promptly and sanely close themselves.

When a process receives SIGHUP, it is being told: "Hey, whoever you were talking to just hung up". While this was historically used for serial connections which were prone to unexpected disconnections, this is often used as a signal to have a shell's job control to spin down all pending processes (or ofttimes for a daemon to reinitialize itself or re-parse its configuration file).

When a process receives SIGKILL, the kernel (metaphorically) walks up behind it and shoots it in the back of the head. On one hand: no fuss, no muss. On the other hand: there may be a huge mess to clean up.

A KILLed process has no chance to clean up any open temporary files; any unflushed cached data are likely to be lost, and so forth. Therefore, do not send SIGKILL unless you really have to.

Manuel Jordan
  • 1,728
  • 2
  • 16
  • 40
DopeGhoti
  • 76,081
  • 2
    Note that SIGTERM is not handled by commands by default. The stdio buffers for instance are not flushed by default upon SIGTERM. The commands will have to actively install a handler on SIGTERM for that. On GNU/Linux systems see ps -eo caught,pid,args | gawk -v t="$(kill -l TERM)" 'and(strtonum("0x"$1), 2**(t-1))' for a list of processes that do have a handler on SIGTERM. – Stéphane Chazelas Jan 31 '18 at 16:31
  • note kill and pkill will send SIGTERM by default. try pkill foo before sending pkill -KILL foo. – quixotic Jan 31 '18 at 16:51
  • While it is true that SIGTERM is the default, I prefer to explicitly state which signal I wish to send when using KILL as a self-imposed sanity check. It also makes editing the command line / script to send non-TERM signals easier on later iterations if needed. – DopeGhoti Jan 31 '18 at 17:15
  • 1
    If the goal is logging out users, then really a SIGHUP should be sent before sending either of those. – JdeBP Jan 31 '18 at 17:40