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.
- 3That's an extraordinarily bad way to log out. See e.g. unix.stackexchange.com/questions/281439/…Kusalananda– Kusalananda ♦2018-01-31 16:47:47 +00:00Commented Jan 31, 2018 at 16:47
1 Answer
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.
- 2Note 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– Stéphane Chazelas2018-01-31 16:31:32 +00:00Commented Jan 31, 2018 at 16:31 - note
killandpkillwill sendSIGTERMby default. trypkill foobefore sendingpkill -KILL foo.quixotic– quixotic2018-01-31 16:51:22 +00:00Commented Jan 31, 2018 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– DopeGhoti2018-01-31 17:15:12 +00:00Commented Jan 31, 2018 at 17:15
- 1If the goal is logging out users, then really a
SIGHUPshould be sent before sending either of those.JdeBP– JdeBP2018-01-31 17:40:43 +00:00Commented Jan 31, 2018 at 17:40