How do I kill a process ` #PID<0.186.0` in iex?

When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cursor to recall the previoius commands. But how do I kill the Plug process to restart it after I recompile.

Is there some command I can use to kill #PID<0.186.0 which I assume if the plug’s process?

Erlang/OTP 19 [erts-8.3] [source-d5c06c6] [64-bit] [smp:2:2] [async-threads:10] [hipe] [kernel-poll:false]

Interactive Elixir (1.4.1) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> c "myfuncs/funcs_router.ex"

[MyRouter]
iex(2)> {:ok, _} = Plug.Adapters.Cowboy.http MyRouter, []
{:ok, #PID<0.186.0>}
iex(3)> c "myfuncs/funcs_router.ex"

...

[MyRouter]
iex(4)> {:ok, _} = Plug.Adapters.Cowboy.http MyRouter, []
** (MatchError) no match of right hand side value: {:error, {:already_started, #PID<0.186.0>}}
4 Likes
Process.exit(pid("0.186.0"), :normal)
4 Likes

When I apply your solution it doesn’t seem to work.

Interactive Elixir (1.4.1) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> c "myfuncs/funcs_router.ex"
[MyRouter]
iex(2)> {:ok, _} = Plug.Adapters.Cowboy.http MyRouter, []
{:ok, #PID<0.185.0>}
iex(3)> Process.exit(pid("0.185.0"), :normal)
true
iex(4)> {:ok, _} = Plug.Adapters.Cowboy.http MyRouter, []
** (MatchError) no match of right hand side value: {:error, {:already_started, #PID<0.185.0>}}
    
iex(4)>

Well, then try to kill it instead of normal exit:

Process.exit(pid, :kill)
1 Like

Process.exit(pid, :normal) doesn’t work for me, either, but Process.exit(pid, :ok) does.

:normal is a normal kill response, which most things will ignore just fine. Sending anything else is not ignored though, but traditionally :kill is sent or :brutalkill depending.

5 Likes

Ah, thanks. So what is the purpose of using Process.exit(pid, :normal) if most things ignore it?

It is used when a process traps exists. Then the :normal message is transformed into an {:'EXIT', from, :normal} message which is delivered to the message queue of the process and it can choose to exit itself when it receives it. But if the process is not trapping exists nothing happens. This is different from :kill and reason explained below.

You can send any exit reason to the process. :kill and :normal are special. Any other reason has the following behaviour:

  • If process is not trapping exists it will exit with reason
  • If process is trapping exists the signal is transformed to {:'EXIT', from, reason} and deliver to the message queue

:kill is an untrappable exit and the process will be killed.

(Mostly stolen from h(Process.exit))

9 Likes

@cmkarlsson is right in his answer, but making it short:

Process.exit(pid, :normal) == please stop my dear process :pray::stop_sign:
Process.exit(pid, :kill) == :boom::gun:

Important: it’s not a threat to kill, it’s killing! So the process has no option to exit gracefully, it just stops processing whatever it was processing and has no time to do the exit routine it has.

12 Likes